最近项目需要整一个清理内存的圆圈pro,要求中间还要带个文本展示
样式如下:
然而仅仅只是这样一个样式怎么能够满足一个码农的发散性思维
于是就有了下面的样式
然后来看一下xml的使用
emm……
有点简陋哈哈哈哈
好了重要的部分来了
上源码
package com.hwj.gui.ui;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.hwj.gui.R;
/**
* CreateBy: MR.LEE
* date: 2020/6/24_14:14
*/
public class LoadingProgressBar extends View {
private int progress;
private float progressWidth;
private float textMaxWidth;
private RectF progressRectF;
private Paint progressPaint;
private float width;
private float height;
private Paint textPaint;
private int progressColor;
private int progressBackground;
private Rect rect;
public LoadingProgressBar(Context context) {
this(context, null);
}
public LoadingProgressBar(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public LoadingProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
if (context == null)
return;
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LoadingProgressBar);
float textSize = array.getDimension(R.styleable.LoadingProgressBar_android_textSize, 0);
progressColor = array.getColor(R.styleable.LoadingProgressBar_progressColor, Color.GRAY);
int textColor = array.getColor(R.styleable.LoadingProgressBar_android_textColor, progressColor);
progress = array.getInteger(R.styleable.LoadingProgressBar_android_progress, 0);
progressBackground = array.getColor(R.styleable.LoadingProgressBar_progressBackground, Color.TRANSPARENT);
progressWidth = array.getDimension(R.styleable.LoadingProgressBar_progressWidth, 1);
array.recycle();
TextView textView = new TextView(context);
textView.setTextSize(textSize);
textMaxWidth = textView.getPaint().measureText("100%");
progressRectF = new RectF();
rect = new Rect();
progressPaint = new Paint();
progressPaint.setColor(progressColor); //设置画笔颜色
progressPaint.setStyle(Paint.Style.STROKE); //设置填充样式
progressPaint.setStrokeWidth(progressWidth); //设置画笔宽度
progressPaint.setAntiAlias(true);
textPaint = new Paint();
textPaint.setColor(textColor); //设置画笔颜色
textPaint.setStyle(Paint.Style.FILL); //设置填充样式
textPaint.setAntiAlias(true);
textPaint.setTextSize(textSize);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (widthMode == MeasureSpec.UNSPECIFIED || widthMode == MeasureSpec.AT_MOST) {
width = getPaddingLeft() + getPaddingRight() + textMaxWidth + progressWidth * 2;
} else {
width = MeasureSpec.getSize(widthMeasureSpec);
}
if (heightMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.AT_MOST) {
height = getPaddingTop() + getPaddingBottom() + textMaxWidth + progressWidth * 2;
} else {
height = MeasureSpec.getSize(heightMeasureSpec);
}
setMeasuredDimension((int) width, (int) height);
}
public void setProgress(int progress) {
this.progress = progress;
postInvalidate();
}
public int getProgress() {
return progress;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
progressRectF.left = getPaddingLeft() + progressWidth / 2f;
progressRectF.top = getPaddingTop() + progressWidth / 2f;
progressRectF.right = width - getPaddingRight() - progressWidth / 2f;
progressRectF.bottom = height - getPaddingBottom() - progressWidth / 2f;
if (progress > 100)
progress = 100;
if (progress < 0) {
progress = 0;
}
if (progress == 0)
return;
double sweepAngle = 360d * progress / 100;
progressPaint.setColor(progressBackground);
canvas.drawArc(progressRectF, 0, 360, false, progressPaint);
progressPaint.setColor(progressColor);
canvas.drawArc(progressRectF, 90, (float) sweepAngle, false, progressPaint);
String str = progress + "%";
textPaint.getTextBounds(str, 0, str.length(), rect);
int w = rect.width(); //获取宽度
int h = rect.height();//获取高度
// 真实宽高
width = progressRectF.right - progressRectF.left;
height = progressRectF.bottom - progressRectF.top;
float startX = width - w < 0 ? 0f : width - w;
float startY = height + h > height * 2 ? height * 2 : width + h;
canvas.drawText(str, (startX + getPaddingLeft()) / 2f + progressRectF.left,
(startY + getPaddingTop()) / 2f + progressRectF.top, textPaint);
}
}
接下来就是attrs.xml了
打完收工
揍是这么easy
留下以备不时之需,有需要可以拿去耍