您好,登錄后才能下訂單哦!
要實(shí)現(xiàn)環(huán)形ProgressBar的動(dòng)畫效果,可以借助ValueAnimator和Canvas來(lái)實(shí)現(xiàn)。以下是一個(gè)簡(jiǎn)單的示例代碼:
public class CircleProgressBar extends View {
private Paint mPaint;
private int mProgress;
private int mMaxProgress;
private int mBackgroundColor;
private int mProgressColor;
private ValueAnimator mAnimator;
public CircleProgressBar(Context context) {
this(context, null);
}
public CircleProgressBar(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CircleProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mProgress = 0;
mMaxProgress = 100;
mBackgroundColor = Color.GRAY;
mProgressColor = Color.BLUE;
mAnimator = ValueAnimator.ofInt(0, mMaxProgress);
mAnimator.setDuration(1000);
mAnimator.addUpdateListener(animation -> {
mProgress = (int) animation.getAnimatedValue();
invalidate();
});
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getWidth();
int height = getHeight();
float centerX = width / 2;
float centerY = height / 2;
float radius = Math.min(width, height) / 2 - 10;
// 繪制背景圓環(huán)
mPaint.setColor(mBackgroundColor);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(20);
canvas.drawCircle(centerX, centerY, radius, mPaint);
// 繪制進(jìn)度圓環(huán)
mPaint.setColor(mProgressColor);
float sweepAngle = 360f * mProgress / mMaxProgress;
canvas.drawArc(centerX - radius, centerY - radius, centerX + radius, centerY + radius, -90, sweepAngle, false, mPaint);
}
public void setProgress(int progress) {
if (progress >= 0 && progress <= mMaxProgress) {
mProgress = progress;
invalidate();
}
}
public void startAnimation() {
mAnimator.start();
}
public void setMaxProgress(int maxProgress) {
mMaxProgress = maxProgress;
}
public void setBackgroundColor(int color) {
mBackgroundColor = color;
}
public void setProgressColor(int color) {
mProgressColor = color;
}
}
在布局文件中添加CircleProgressBar,并設(shè)置屬性:
<com.example.CircleProgressBar
android:id="@+id/circleProgressBar"
android:layout_width="100dp"
android:layout_height="100dp"
app:backgroundColor="@color/gray"
app:progressColor="@color/blue" />
在代碼中可以通過(guò)調(diào)用setProgress方法更新進(jìn)度,通過(guò)調(diào)用startAnimation方法啟動(dòng)動(dòng)畫效果??梢愿鶕?jù)需要調(diào)整畫筆樣式、動(dòng)畫時(shí)長(zhǎng)等屬性來(lái)實(shí)現(xiàn)不同的環(huán)形ProgressBar動(dòng)畫效果。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。