前言
1.主要用來展示進度,因爲有特殊的ui要求
效果
代碼
幾個自定義屬性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--圓形進度條-->
<declare-styleable name="RoundProgressBar">
<!--背景圓顏色-->
<attr name="roundColor" format="color"/>
<!--進度圓顏色-->
<attr name="roundProgressColor" format="color"/>
<!--背景圓/進度圓 線寬-->
<attr name="roundWidth" format="dimension"></attr>
<!--進度拇指圓半徑-->
<attr name="thumbRadius" format="dimension"></attr>
<!--拇指圓顏色-->
<attr name="thumbColor" format="color"></attr>
<!--最大進度-->
<attr name="maxProgress" format="integer"></attr>
<!--是否實心 當前只支持實心-->
<attr name="style">
<enum name="STROKE" value="0"></enum>
<enum name="FILL" value="1"></enum>
</attr>
</declare-styleable>
</resources>
主程序代碼
public class MyRoundProgressBar extends View {
/**
* 畫筆對象的引用
*/
private Paint paint;
/**
* 背景圓環的顏色
*/
private int roundColor;
/**
* 背景圓環線寬度
*/
private float roundWidth;
/**
* 進度圓的顏色
*/
private int roundProgressColor;
/**
* 拇指圓半徑
*/
private float thumbRadius;
/**
* 拇指圓顏色
*/
private float thumbColor;
/**
* 最大進度
*/
private int max;
/**
* 當前進度
*/
private int progress;
/**
* 進度的風格,實心或者空心
*/
private int style;
public static final int STROKE = 0;
public static final int FILL = 1;
public MyRoundProgressBar(Context context) {
this(context, null);
}
public MyRoundProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyRoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
paint = new Paint();
TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
R.styleable.RoundProgressBar);
//獲取自定義屬性和默認值
max = mTypedArray.getInteger(R.styleable.RoundProgressBar_maxProgress, 100);
/**
* 背景圓
*/
roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.GRAY);
roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 14);
/**
* 進度圓
*/
roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.RED);
/**
* 拇指圓
*/
thumbRadius = mTypedArray.getDimension(R.styleable.RoundProgressBar_thumbRadius, 28);
thumbColor = mTypedArray.getColor(R.styleable.RoundProgressBar_thumbColor, Color.RED);
style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);
mTypedArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
* 畫最外層的大圓環
*/
int centre = getWidth() / 2; //獲取圓心的x座標
int radius = (int) (centre - thumbRadius); //圓環的半徑
paint.setColor(roundColor); //設置圓環的顏色
paint.setStyle(Paint.Style.STROKE); //設置空心
paint.setStrokeWidth(roundWidth); //設置圓環的寬度
paint.setAntiAlias(true); //消除鋸齒
canvas.drawCircle(centre, centre, radius, paint); //畫出圓環
/**
* 畫圓弧 ,畫圓環的進度
*/
BlurMaskFilter maskFilter = new BlurMaskFilter(10, BlurMaskFilter.Blur.SOLID);
paint.setMaskFilter(maskFilter);
//設置進度是實心還是空心
paint.setStrokeWidth(roundWidth); //設置圓環的寬度
paint.setColor(roundProgressColor); //設置進度的顏色
RectF oval = new RectF(centre - radius, centre - radius, centre
+ radius, centre + radius); //用於定義的圓弧的形狀和大小的界限
paint.setStyle(Paint.Style.STROKE);
//範圍
//起點
//總進度
canvas.drawArc(oval, -90, -360 * progress / max, false, paint); //根據進度畫圓弧
// if (progress > 0 && progress < max) {
// /**
// * 畫一個小圓
// */
// paint.setStrokeWidth(1);
// paint.setStyle(Paint.Style.FILL);
//
// float littleCentreX = centre;
// float littleCentreY = centre - radius;
// float lRadius = roundWidth * 0.5f;
// RectF ovalLittle = new RectF(littleCentreX - lRadius, littleCentreY - lRadius, littleCentreX
// + lRadius, littleCentreY + lRadius); //用於定義的圓弧的形狀和大小的界限
// canvas.drawArc(ovalLittle, 90, -180, false, paint);
// /**
// *畫一個大圓
// * */
// float centreX = (float) (centre - radius * (Math.sin((float) progress / (float) max * Math.PI * 2)));
// float centreY = (float) (centre - radius * (Math.cos((float) progress / (float) max * Math.PI * 2)));
// canvas.drawCircle(centreX, centreY, thumbRadius, paint);
// } else if (progress == 0) { //沒電 進度爲0
// /**
// * 畫一個小圓
// */
// paint.setStrokeWidth(1);
// paint.setStyle(Paint.Style.FILL);
//
// float littleCentreX = centre;
// float littleCentreY = centre - radius;
// float lRadius = roundWidth * 0.5f;
// RectF ovalLittle = new RectF(littleCentreX - lRadius, littleCentreY - lRadius, littleCentreX
// + lRadius, littleCentreY + lRadius); //用於定義的圓弧的形狀和大小的界限
// canvas.drawArc(ovalLittle, 90, -360, false, paint);
// }
switch (style) {
case STROKE: {
paint.setStyle(Paint.Style.STROKE);
break;
}
case FILL: {
paint.setStyle(Paint.Style.FILL_AND_STROKE);
if (progress != 0)
canvas.drawArc(oval, 0, 360 * progress / max, true, paint); //根據進度畫圓弧
break;
}
}
}
public synchronized int getMax() {
return max;
}
/**
* 設置進度的最大值
*
* @param max
*/
public synchronized void setMax(int max) {
if (max < 0) {
throw new IllegalArgumentException("max not less than 0");
}
this.max = max;
}
/**
* 獲取進度.需要同步
*
* @return
*/
public synchronized int getProgress() {
return progress;
}
/**
* 設置進度,此爲線程安全控件,由於考慮多線的問題,需要同步
* 刷新界面調用postInvalidate()能在非UI線程刷新
*
* @param progress
*/
public synchronized void setProgress(int progress) {
if (progress < 0) {
throw new IllegalArgumentException("progress not less than 0");
}
if (progress > max) {
progress = max;
}
if (progress <= max) {
this.progress = progress;
postInvalidate();
}
}
public int getRoundColor() {
return roundColor;
}
public void setRoundColor(int roundColor) {
this.roundColor = roundColor;
}
public float getRoundWidth() {
return roundWidth;
}
public void setRoundWidth(float roundWidth) {
this.roundWidth = roundWidth;
}
public int getRoundProgressColor() {
return roundProgressColor;
}
public void setRoundProgressColor(int roundProgressColor) {
this.roundProgressColor = roundProgressColor;
}
public float getThumbRadius() {
return thumbRadius;
}
public void setThumbRadius(float thumbRadius) {
this.thumbRadius = thumbRadius;
}
public float getThumbColor() {
return thumbColor;
}
public void setThumbColor(float thumbColor) {
this.thumbColor = thumbColor;
}
}
註釋部分實際上是 外加的 進度頭部和尾部的小圓點 這裏不做使用
xml引用
具體屬性在上面的attr文件中有註釋
<com.example.caixingcun.testactivity.MyRoundProgressBar
android:id="@+id/roundProgressBar"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android_custom:maxProgress="100"
android_custom:roundColor="#000"
android_custom:roundProgressColor="#cc28e1d9"
android_custom:thumbColor="#cc28e1d9"
android_custom:roundWidth="7dp"
android_custom:style="STROKE"
android_custom:thumbRadius="3.5dp"
/>
主程序設置進度
mProgressBar = findViewById(R.id.roundProgressBar);
mProgressBar.setProgress(20);