Skip to content Skip to sidebar Skip to footer

Animate Change Width Of Android Relativelayout

I need to change the width of a RelativeLayout from 0 to 600px programmatically, in an animated way. I'm using the following code: Animation a = new Animation() { @Override

Solution 1:

I just typed this up and works perfect here

Animation

publicclassResizeWidthAnimationextendsAnimation {
    privateint mWidth;
    privateint mStartWidth;
    private View mView;
    
    publicResizeWidthAnimation(View view, int width) {
        mView = view;
        mWidth = width;
        mStartWidth = view.getWidth();
    }
    
    @OverrideprotectedvoidapplyTransformation(float interpolatedTime, Transformation t) {
        intnewWidth= mStartWidth + (int) ((mWidth - mStartWidth) * interpolatedTime);

        mView.getLayoutParams().width = newWidth;
        mView.requestLayout();
    }
    
    @Overridepublicvoidinitialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }
    
    @OverridepublicbooleanwillChangeBounds() {
        returntrue;
    }
}

usage

if (animate) {
    ResizeWidthAnimationanim=newResizeWidthAnimation(leftFrame, leftFragmentWidthPx);
    anim.setDuration(500);
    leftFrame.startAnimation(anim);
} else {
    this.leftFragmentWidthPx = leftFragmentWidthPx;
    LayoutParamslp= (LayoutParams) leftFrame.getLayoutParams();
    lp.width = leftFragmentWidthPx;
    leftFrame.setLayoutParams(lp);
}

UPDATE: Kotlin Version

classResizeWidthAnimation(privateval mView: View, privateval mWidth: Int) : Animation() {
    privateval mStartWidth: Int = mView.width

    overridefunapplyTransformation(interpolatedTime: Float, t: Transformation) {
        mView.layoutParams.width = mStartWidth + ((mWidth - mStartWidth) * interpolatedTime).toInt()
        mView.requestLayout()
    }

    overridefunwillChangeBounds(): Boolean {
        returntrue
    }
}

Solution 2:

A lot of times the easiest way to animate layout changes in Android is to set the animateLayoutChanges attribute to true in the layout xml. For more details, see http://developer.android.com/training/animation/layout.html.

Solution 3:

Instead of setLayoutParams(), call requestLayout() after the width change.

Solution 4:

This is what worked for me:

publicclassCloseImageFromRightToLeftextendsAnimation {
    privatefinalint newWidth;
    privatefinalint newHeight;
    privatefinalint originalWidth;
    private ImageView mView;

    publicCloseImageFromRightToLeft(ImageView v, int[] widthAndHeight) {
        mView = v;
        this.newWidth = widthAndHeight[0] / 3;
        this.originalWidth = widthAndHeight[0];
        this.newHeight = widthAndHeight[1];
    }

    @Overridepublicvoidinitialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    @OverridepublicbooleanwillChangeBounds() {
        returntrue;
    }

    @OverridepublicvoidapplyTransformation(float interpolatedTime, Transformation t) {
        intnewWidth= originalWidth + (int) ((this.newWidth - originalWidth) * interpolatedTime);


        LinearLayout.LayoutParamsparms=newLinearLayout.LayoutParams(newWidth, this.newHeight);
        mView.setLayoutParams(parms);
        mView.requestLayout();
    }
}

Post a Comment for "Animate Change Width Of Android Relativelayout"