android - 如何在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
. 有人可以帮忙吗?
解决方案
我使用这个 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
}
推荐阅读
- javascript - 如果实际提交了表单,则路由到另一个组件
- oop - 在 ui 自动化测试用例期间让多个用户同时登录
- google-apps-script - 取消合并电子表格单元格并用数据填充前合并的空单元格
- c++ - consteval vs constexpr 变量
- python - 调试关于点距离的数学代码
- javascript - 在firebase实时数据库中安全地设置新用户的组字段
- css - 当我在 div 溢出的高度动态添加 svg 时,如何防止它?
- neo4j - 无法使用已创建的 neo4j 数据库
- reactjs - 如何在反应原生中使用样式表嵌套打字稿
- javascript - How to use conditional dropdown in react-select