您好,登錄后才能下訂單哦!
要實現環形ProgressBar的動畫效果,可以借助ValueAnimator和Canvas來實現。以下是一個簡單的示例代碼:
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;
// 繪制背景圓環
mPaint.setColor(mBackgroundColor);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(20);
canvas.drawCircle(centerX, centerY, radius, mPaint);
// 繪制進度圓環
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,并設置屬性:
<com.example.CircleProgressBar
android:id="@+id/circleProgressBar"
android:layout_width="100dp"
android:layout_height="100dp"
app:backgroundColor="@color/gray"
app:progressColor="@color/blue" />
在代碼中可以通過調用setProgress方法更新進度,通過調用startAnimation方法啟動動畫效果。可以根據需要調整畫筆樣式、動畫時長等屬性來實現不同的環形ProgressBar動畫效果。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。