簡體   English   中英

如何在Android中使用Canvas繪制視圖動畫?

[英]how to animate view drawn using Canvas in android?

我在onDraw方法中使用Canvas繪制垂直步進視圖。 該視圖是根據步驟數動態繪制的。 我的看法如下 在此處輸入圖片說明 因此,如果我當前的步驟是2,我想用在step1圓圈中的顏色從圓1繪制動畫到cirle 2。 然后連續發光第二個圓圈,以便引起用戶的注意。 如何實現這種動畫效果?

為了補充我的評論,這是一種使用ValueAnimatoronDraw()逐步移動圓的解決方案。

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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM