Как анимировать вид, нарисованный с помощью Canvas в Android? - PullRequest
0 голосов
/ 23 января 2019

Я рисую вертикальный шаговый вид, используя Canvas в методе onDraw.Это представление рисуется динамически в зависимости от количества шагов.Мой вид выглядит следующим образом: enter image description here Так что, если мой текущий шаг - 2, я хочу нарисовать анимацию из круга 1 в круг 2 с цветом в круге step1.и затем непрерывно светите вторым кругом, чтобы пользователь обратил внимание.Как мне добиться этого анимационного эффекта?

1 Ответ

0 голосов
/ 23 января 2019

Чтобы завершить мой комментарий, вот решение для постепенного перемещения круга в onDraw(), используя ValueAnimator.

class CircleWithMotion extends View implements ValueAnimator.AnimatorUpdateListener {

    private final int CIRCLE_RADIUS = 30;
    private final int STEP_Y = 200;
    private final PointF circlePosition = new PointF(100, 100);

    private ValueAnimator animator;
    private Paint paint;

    public CircleWithMotion(Context context) {
        super(context);
        paint = new Paint();
        paint.setColor(Color.BLUE);

        // HERE you can change the type and duration of the animation
        animator = new ValueAnimator();
        animator.setDuration(1000);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.addUpdateListener(this);
    }

    // Call this to start the animation
    public void movingToNextStep() {
        // Sets the START and END values for the animation (FROM current y position TO next position)
        animator.setFloatValues(circlePosition.y, circlePosition.y + STEP_Y);
        animator.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawCircle(circlePosition.x, circlePosition.y, CIRCLE_RADIUS, paint);
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        // Update the Y circle position with the calculated value from animator
        circlePosition.y = (float) animator.getAnimatedValue();
        // say the view must be redraw
        this.invalidate();
    }
}
...