[英]How to implement circular to check / tick animation in 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
}
}
}
我使用ValueAnimator
稱為onDraw
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();
}
並在調用onDraw
setProgress
方法中失效。
但是,我需要實現這樣的目標,
我嘗試將 Animator 與ValueAnimator
一起使用,但沒有用。 我試圖通過在 lambda 內部設置一個變量來按順序調用onDraw
但徒勞無功。 我不確定是否可以使用AnimatedVectorDrawable
做到這一點。 有人可以幫忙嗎?
您是否嘗試過查看此源代碼:
https://github.com/cdflynn/checkview
另請參閱此庫: https : //github.com/airbnb/lottie-android可以幫助您使用動畫。
我認為此視圖可以由 AnimatedStateListDrawable 完成,這是此類動畫的最佳實踐,您可以在保留部分查看https://medium.com/androiddevelopers/animating-on-a-schedule-8a90d812ae4 。
我使用這個 repo 作為參考並實現了我想要的。 而不是使用隨機計數器來設置速度。 我使用值動畫並使用每一幀的進度值來設置弧率。 並使用計數器繪制從零增長到半徑的圓。
{
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.