18

我需要以动画方式以编程方式将 RelativeLayout 的宽度从 0 更改为 600px。我正在使用以下代码:

Animation a = new Animation() {
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        RelativeLayout.LayoutParams drawerParams = (LayoutParams) drawer.getLayoutParams();
        drawerParams.width = 600;
        drawer.setLayoutParams(drawerParams);
    }
};

a.setDuration(1000); //Animate for 1s
layout.startAnimation(a);

但是,由于某种原因,这不起作用。谁能指出我正确的方向?

4

4 回答 4

52

我刚刚输入了这个并且在这里完美运行

动画

public class ResizeWidthAnimation extends Animation {
    private int mWidth;
    private int mStartWidth;
    private View mView;
    
    public ResizeWidthAnimation(View view, int width) {
        mView = view;
        mWidth = width;
        mStartWidth = view.getWidth();
    }
    
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        int newWidth = mStartWidth + (int) ((mWidth - mStartWidth) * interpolatedTime);

        mView.getLayoutParams().width = newWidth;
        mView.requestLayout();
    }
    
    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }
    
    @Override
    public boolean willChangeBounds() {
        return true;
    }
}

用法

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

更新:科特林版本

class ResizeWidthAnimation(private val mView: View, private val mWidth: Int) : Animation() {
    private val mStartWidth: Int = mView.width

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

    override fun willChangeBounds(): Boolean {
        return true
    }
}
于 2013-06-19T20:24:07.107 回答
4

很多时候,在 Android 中动画布局更改的最简单方法是在布局 xml 中设置animateLayoutChanges属性。true有关更多详细信息,请参阅http://developer.android.com/training/animation/layout.html

于 2015-08-14T10:39:49.567 回答
0

而不是,在宽度更改后setLayoutParams()调用。requestLayout()

于 2013-05-20T02:09:58.567 回答
0

这对我有用:

public class CloseImageFromRightToLeft extends Animation {
    private final int newWidth;
    private final int newHeight;
    private final int originalWidth;
    private ImageView mView;

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

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }

    @Override
    public void applyTransformation(float interpolatedTime, Transformation t) {
        int newWidth = originalWidth + (int) ((this.newWidth - originalWidth) * interpolatedTime);


        LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(newWidth, this.newHeight);
        mView.setLayoutParams(parms);
        mView.requestLayout();
    }
}
于 2015-03-16T22:53:49.563 回答