Animate color of Paint object

2019-05-11 16:29发布

I am trying to animate a Paint object in my custom view between colors. But the animation is not working.

ObjectAnimator colorFade = ObjectAnimator.ofObject(mCirclePaint, "color", new ArgbEvaluator(), getColor(), 0xff000000);
              colorFade.setDuration(1500);
              colorFade.start();

            invalidate();

I have previously set the paints color like this:

mCirclePaint.setColor(Color.RED);

UPDATE I don't think the Handler makes a difference to whether it animated or not. Even without the Handler I cannot animate the paint object.

2条回答
放荡不羁爱自由
2楼-- · 2019-05-11 16:58

This is the solution I found:

ObjectAnimator    colorFade = ObjectAnimator.ofObject(mCirclePaint, "color", new ArgbEvaluator(), getColor(), mColors[randomNum]);
                  colorFade.setDuration(1500);
                colorFade.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        // TODO Auto-generated method stub
                        invalidate();

                    }


                });

                  colorFade.start();
查看更多
趁早两清
3楼-- · 2019-05-11 17:04

try this:

class MyView extends View {
    private Paint mPaint;
    public MyView(Context context) {
        super(context);
        mPaint = new Paint();

        ArgbEvaluator evaluator = new ArgbEvaluator();
        ObjectAnimator animator = ObjectAnimator.ofObject(this, "color", evaluator, 0xffff0000, 0xff00ff00, 0xff0000ff);
        animator.setDuration(6000).start();
    }

    public void setColor(int color) {
        mPaint.setColor(color);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPaint(mPaint);
    }
}
查看更多
登录 后发表回答