自定义圆形进度条,支持设置初始进度,更改进度条颜色 背景色 进度条中间可以设置图片。
先上效果图
效果图
废话不多说直接上代码
public class ProgressBarView extends View {
private Paint mPaintBack;
private Paint mPaint;
private Paint mPaintTextCurrentProgressTextColor;
private Paint mPaintTextAggregateSchedulingTextColor;
private int strokeWidth;//进度条宽度
private int textSize;//文字大小
private int drawBack;//进度背景颜色
private int ProgressColor;//进度颜色
private int currentProgressTextColor;//当前进度文字颜色
private int aggregateSchedulingTextColor;//总进度文字颜色
private int ImageTop;
private int ImageLeft;
private int centreImage;//中间图片
private OnProgressChangedListener mListener;
private ValueAnimator mValueAnimator;
// 当前进度
private float mProgress;
private float mDefaultProgress;//默认进度
private int mGoldAmount;//金币总数
// 总进度
private int mTotalProgress = 100;
private Bitmap mBitmap;
public ProgressBarView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initAttrs(context, attrs);
init();
}
@SuppressLint("ResourceAsColor")
private void initAttrs(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ProgressBarView);
textSize = (int) typedArray.getDimension(R.styleable.ProgressBarView_textSize, 15);
strokeWidth = (int) typedArray.getDimension(R.styleable.ProgressBarView_strokeWidth, 8);
centreImage = typedArray.getResourceId(R.styleable.ProgressBarView_centreImage, R.mipmap.progress_bar_view_image);
drawBack = typedArray.getColor(R.styleable.ProgressBarView_drawBack, R.color.color_f1f1f1);
ProgressColor = typedArray.getColor(R.styleable.ProgressBarView_ProgressColor, R.color.color_d19ef4);
currentProgressTextColor = typedArray.getColor(R.styleable.ProgressBarView_currentProgressTextColor, R.color.color_d19ef4);
aggregateSchedulingTextColor = typedArray.getColor(R.styleable.ProgressBarView_aggregateSchedulingTextColor, R.color.color_706A6A);
ImageTop = (int) typedArray.getDimension(R.styleable.ProgressBarView_ImageTop, 25);
typedArray.recycle();
}
private void init() {
mBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), centreImage);
mPaintBack = new Paint();
mPaintBack.setColor(drawBack);
mPaintBack.setStyle(Paint.Style.STROKE);
mPaintBack.setAntiAlias(true);
mPaintBack.setStrokeCap(Paint.Cap.ROUND);
mPaintBack.setStrokeWidth(strokeWidth);
mPaint = new Paint();
mPaint.setColor(ProgressColor);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setAntiAlias(true);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(strokeWidth);
mPaintTextCurrentProgressTextColor = new Paint();
mPaintTextCurrentProgressTextColor.setAntiAlias(true);
mPaintTextCurrentProgressTextColor.setStyle(Paint.Style.FILL);
mPaintTextCurrentProgressTextColor.setColor(currentProgressTextColor);
mPaintTextCurrentProgressTextColor.setTextSize(sp2px(textSize));
mPaintTextAggregateSchedulingTextColor = new Paint();
mPaintTextAggregateSchedulingTextColor.setAntiAlias(true);
mPaintTextAggregateSchedulingTextColor.setStyle(Paint.Style.FILL);
mPaintTextAggregateSchedulingTextColor.setColor(aggregateSchedulingTextColor);
mPaintTextAggregateSchedulingTextColor.setTextSize(sp2px(textSize));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//绘制背景矩形
drawBack(canvas);
//绘制进度
drawProgress(canvas);//动画进度
drawProgressDefault(canvas);//默认进度
//绘制文字
drawText(canvas);
drawImage(canvas);
}
//背景矩形
private void drawBack(Canvas canvas) {
//创建圆环矩形
RectF rectF = new RectF(strokeWidth, strokeWidth, getWidth() - strokeWidth, getHeight() - strokeWidth);
//画出灰色进度条作为背景
canvas.drawArc(rectF, 0, 360, false, mPaintBack);
}
//创建圆环矩形
private void drawProgress(Canvas canvas) {
RectF rectF = new RectF(strokeWidth, strokeWidth, getWidth() - strokeWidth, getHeight() - strokeWidth);
canvas.drawArc(rectF, -90, ((float) mProgress / mTotalProgress) * 360, false, mPaint);
}
//创建圆环矩形
private void drawProgressDefault(Canvas canvas) {
RectF rectF = new RectF(strokeWidth, strokeWidth, getWidth() - strokeWidth, getHeight() - strokeWidth);
canvas.drawArc(rectF, -90, ((float) mDefaultProgress / mTotalProgress) * 360, false, mPaint);
}
private void drawImage(Canvas canvas) {
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
ImageLeft = getWidth() / 2 - mBitmap.getWidth() / 2;
// 画出原图像
canvas.drawBitmap(mBitmap, ImageLeft, ImageTop, paint);
}
//绘制文字
private void drawText(Canvas canvas) {
int mProgressInt = (int) mProgress;
int mProgressIntDefault = (int) mDefaultProgress;
int mPaintTextCurrentProgressTextWidth;
int mPaintTextAggregateSchedulingTextWidth = (int) mPaintTextCurrentProgressTextColor.measureText("/", 0, "/".length());
int mTxtHeight = getTextHeight();
int x = getWidth() / 2 - mPaintTextAggregateSchedulingTextWidth / 2;
int y = getHeight() / 2 + mBitmap.getHeight() / 2 + mTxtHeight / 2;
canvas.drawText("/", x, y, mPaintTextCurrentProgressTextColor);
if (mProgressInt > mProgressIntDefault) {
mPaintTextCurrentProgressTextWidth = (int) mPaintTextAggregateSchedulingTextColor.measureText(String.valueOf(mProgressInt), 0, String.valueOf(mProgressInt).length());
canvas.drawText(mProgressInt + "", x - mPaintTextCurrentProgressTextWidth, y, mPaintTextAggregateSchedulingTextColor);
} else {
mPaintTextCurrentProgressTextWidth = (int) mPaintTextAggregateSchedulingTextColor.measureText(String.valueOf(mProgressIntDefault), 0, String.valueOf(mProgressIntDefault).length());
canvas.drawText(mProgressIntDefault + "", x - mPaintTextCurrentProgressTextWidth, y, mPaintTextAggregateSchedulingTextColor);
}
canvas.drawText("100", x + mPaintTextAggregateSchedulingTextWidth, y, mPaintTextCurrentProgressTextColor);
}
private int getTextHeight() {
Paint.FontMetrics fm = mPaintTextCurrentProgressTextColor.getFontMetrics();
return (int) Math.ceil(fm.descent - fm.ascent);
}
private int sp2px(int sp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
getResources().getDisplayMetrics());
}
public void setProgress(int progress) {
if (mValueAnimator != null) {
mValueAnimator.cancel();
}
mValueAnimator = ValueAnimator.ofFloat(progress);
mValueAnimator.setDuration(870);
mValueAnimator.setInterpolator(new LinearInterpolator());
mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mProgress = (float) animation.getAnimatedValue();
Log.i("TAG", "onAnimationUpdate: " + mProgress);
if (mListener != null) {
mListener.onProgressChanged(mProgress, mProgress * 100 / mTotalProgress);
}
invalidate();
}
});
mValueAnimator.start();
}
public void setProgressNum(int progress) {
mDefaultProgress = progress;
mProgress = progress;
invalidate();
}
/**
* 设置进度监听
*/
public void setOnProgressChangedListener(OnProgressChangedListener listener) {
mListener = listener;
}
public interface OnProgressChangedListener {
void onProgressChanged(float mProgress, float currentProgress);
}
}
主要代码在这里
自定义属性
<declare-styleable name="ProgressBarView">
<attr name="textSize" format="dimension"></attr>
<attr name="strokeWidth" format="dimension"></attr>
<attr name="centreImage" format="reference"></attr>
<attr name="drawBack" format="reference"></attr>
<attr name="ProgressColor" format="reference"></attr>
<attr name="currentProgressTextColor" format="reference"></attr>
<attr name="aggregateSchedulingTextColor" format="reference"></attr>
<attr name="ImageTop" format="dimension"></attr>
</declare-styleable>
布局使用
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/execute"
android:layout_width="60dp"
android:layout_height="30dp"
android:background="@color/teal_700"
android:gravity="center"
android:text="开始" />
<com.snd.progressdemo.ProgressBarView
android:id="@+id/progress"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:ImageTop="45dp"
app:ProgressColor="@color/color_d19ef4"
app:aggregateSchedulingTextColor="@color/color_d19ef4"
app:centreImage="@mipmap/progress_bar_view_image"
app:currentProgressTextColor="@color/teal_200"
app:drawBack="@color/color_f1f1f1"
app:strokeWidth="8dp"
app:textSize="5sp" />
</RelativeLayout>
基本使用
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ProgressBarView progress = findViewById(R.id.progress);
TextView execute = findViewById(R.id.execute);
progress.setProgressNum(40);
progress.setOnProgressChangedListener(new ProgressBarView.OnProgressChangedListener() {
@Override
public void onProgressChanged(float mProgress, float currentProgress) {
Log.i(TAG, "onProgressChanged: mProgress " + mProgress + " currentProgress:" + currentProgress);
}
});
execute.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progress.setProgress(80);
}
});
}
}
散会