如何在自定义视图中为 Drawable 设置动画?

How to animate a Drawable in a custom view?

我目前有一个带有 Drawable 资源的自定义视图,我想将其从一个位置移动到另一个位置。我知道有两种实现 PropertyAnimation 的方法,您可以使用 ValueAnimationObjectAnimation 来实现。但是我没有在 Google 文档中找到关于如何将它们与 Drawable 而不是 View 一起使用的任何信息。 This 是我发现的唯一与我的问题相似的例子,所以我尝试在我的代码中实现它:

MainActivity

public class MainActivity extends AppCompatActivity {

    private Button animate;
    private CustomView mView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mView = new CustomView(MainActivity.this);

        animate = (Button) findViewById(R.id.animate);
        animate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mView.startAnimation();
            }
        });

    }
}

自定义视图

public class CustomView extends View {

    private Drawable drawable;
    private Rect drawableRect;
    private int newPos;

    public CustomView(Context context) {
        super(context);

        drawable = ContextCompat.getDrawable(context, R.drawable.my_drawable);
        drawableRect= new Rect();
        newPos = 0;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        ...
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawableRect.left = 0;
        drawableRect.top = newPos;
        drawableRect.right = drawable.getIntrinsicWidth();
        drawableRect.bottom = newPos + drawable.getIntrinsicHeight();

        drawable.setBounds(drawableRect);
        drawable.draw(canvas);

    }

    public void startAnimation() {

        ValueAnimator animator = ValueAnimator.ofFloat(0, 200);
        animator.setDuration(1500);

        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                newPos = ((Float) valueAnimator.getAnimatedValue()).intValue();
                CustomView.this.invalidate();
            }
        });

        animator.start();
        invalidate();

    }

}

但是没有效果。 newPos 的值正确更改(通过日志检查)并且屏幕得到更新(通过表面更新检查)但 Drawable 不移动。我究竟做错了什么?希望得到任何帮助。

我尝试 运行 你的代码 CustomView,它很高效,有动画。

您确定执行方法startAnimation吗?

我将startAnimation()添加到构造NoteCustomView中,我可以在UI完成加载时看到动画。

下面的代码,我使用 ic_launcher 作为可绘制对象。我将 NoteCustomView 添加到 activity 的布局 xml.

public class NoteCustomView extends View {

    private Drawable drawable;
    private Rect drawableRect;
    private int newPos;

    public NoteCustomView(Context context) {
        this(context, null);

    }

    public NoteCustomView(final Context context, @Nullable final AttributeSet attrs) {
        super(context, attrs);
        drawable = ContextCompat.getDrawable(context, R.mipmap.ic_launcher_round);
        drawableRect= new Rect();
        newPos = 0;
        startAnimation();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawableRect.left = 0;
        drawableRect.top = newPos;
        drawableRect.right = drawable.getIntrinsicWidth();
        drawableRect.bottom = newPos + drawable.getIntrinsicHeight();

        drawable.setBounds(drawableRect);
        drawable.draw(canvas);

    }

    public void startAnimation() {

        ValueAnimator animator = ValueAnimator.ofFloat(0, 200);
        animator.setDuration(1500);

        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                newPos = ((Float) valueAnimator.getAnimatedValue()).intValue();
                NoteCustomView.this.invalidate();
            }
        });

        animator.start();
        invalidate();

    }

}

周恩旭 最终带我找到了解决方案。我在 MainActivity 中犯了一个愚蠢的错误,因为我正在创建对 CustomView:

的新引用
mView = new CustomView(MainActivity.this);

并尝试使用该对象引用制作动画。通过将其替换为:

解决了这个问题
mView = (CustomView) findViewById(R.id.custom_view);