首页 > 解决方案 > 如何在Android中实现循环检查/勾选动画?

问题描述

我正在尝试为自定义视图设置动画

我实现了一个圆形进度条自定义视图,与这个完全相似

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

       if (totalCount == 0 || progressCount == 0)
       {
           // No progress, set a different background color and draw a arc and set a icon at the center
           progressBackgroundPaint.setColor(outerNoProgressBackgroundColor);
           canvas.drawArc(viewRect, PROGRESS_START_DEGREE, PROGRESS_END_DEGREE, false, progressBackgroundPaint);
           drawInnerBackgroundWithState(canvas, ProgressState.NO_PROGRESS);
           drawCenterIconWithState(centerIconEmptyBitmap, canvas, ProgressState.NO_PROGRESS);
       }
       else
       {
           // Change the color first for a progress bar
           progressBackgroundPaint.setColor(outerProgressBackgroundColor);
           canvas.drawArc(viewRect, PROGRESS_START_DEGREE, PROGRESS_END_DEGREE, false, progressBackgroundPaint);

           // Then draw an arc on top of it marking progress
           canvas.drawArc(viewRect, PROGRESS_START_DEGREE, getAngle(), false, progressPaint);

           // set inner background color and tint center icon with state-appropriate color and draw it in the center
           // of the progress circle
           if (progressCount < totalCount)
           {
               canvas.drawOval(viewRect, innerBackgroundPaint);
               drawCenterIconWithState(centerIconProgressBitmap, canvas, ProgressState.IN_PROGRESS); // draw bit map function
           }
           else
           {
               canvas.drawOval(viewRect, completeBackgroundPaint);
               drawCenterIconWithState(centerIconProgressBitmap, canvas, ProgressState.COMPLETE); // draw bitmap function
           }
       }
   }

我称之为onDraw使用ValueAnimator

public void drawProgressAnimation(float progress)
   {
ValueAnimator animator;
       if (animator != null)
           animator.cancel();
       animator = ValueAnimator.ofFloat(0f, progress);
       animator.setDuration(600);

       progressAnimator.addUpdateListener(animation ->
       {
           float progress1 = (float)animation.getAnimatedValue();
           setProgress(progress1, true);
       });
       progressAnimator.start();
   } 

setProgress并在调用的方法中失效onDraw

但是,我需要实现这样的目标,

我尝试将 Animator set 与 一起使用ValueAnimator,但它不起作用。我试图onDraw通过在 lambda 中设置一个变量来按顺序调用,但徒劳无功。我不确定是否可以使用AnimatedVectorDrawable. 有人可以帮忙吗?

标签: androidandroid-animationlottie

解决方案


我使用这个 repo 作为参考并实现了我想要的。而不是使用随机计数器来设置速度。我使用值动画并使用每一帧的进度值来设置弧率。并使用计数器绘制从零到半径的圆。

if (isComplete) {
    canvas.drawArc(mRect, START_DEGREE, getSweepAngle(), false, progressPaint);

    // animate circle progress when arc reaches partial sweep angle
    if (getSweepAngle() >= partialSweepAngle)
    {
        innerBackground.setColor(innerCompleteColor);
        circleCounter += circleProgressRate;

        if (circleCounter <= radius)
            canvas.drawCircle(mRect.centerX(), mRect.centerY(), circleCounter,
                innerBackground);
        else
            canvas.drawCircle(mRect.centerX(), mRect.centerY(), radius, innerBackground);
    }
    if (circleCounter >= radius)
        // draw the icon
}

推荐阅读