繁体   English   中英

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

[英]How to animate a Drawable in a custom view?

我目前有一个带有Drawable资源的自定义视图,我希望对其从一个位置移动到另一个位置进行动画处理。 我知道有两种方法可以实现PropertyAnimation ,可以使用ValueAnimationObjectAnimation 但是,在这两者上,我都没有在Google文档上找到有关如何通过Drawable而不是View使用它们的任何信息。 是我发现的唯一与我的问题类似的示例,因此我尝试在代码中实现它:

主要活动

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的值正确更改(用Logs检查),屏幕更新(用Surface更新检查),但是Drawable不会移动。 我究竟做错了什么? 希望有帮助。

我尝试在您的代码中运行CustomView ,它高效,有动画。

您确定执行方法startAnimation吗?

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

在下面的代码中,我将ic_launcher用作可绘制对象。 然后将NoteCustomView添加到活动的布局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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM