自定义圆形进度条
*/
public class RoundProgress extends View {
// // 属性
// private int roundColor = Color.GRAY;
// private int roundProgressColor = Color.RED;
// private int textColor = Color.BLUE;
//
// private int roundWidth = UIUtils.dp2px(10);// 线的宽度
// private int textSize = UIUtils.dp2px(20);// 字体的大小
//
// private int width;
// private Paint paint;
//
// // 提供当前进度和最大值
// private int progress = 60;
// private int max = 100;
// 自定义属性的声明
private int roundColor;
private int roundProgressColor;
private int textColor;
private int roundWidth; // 线的宽度
private int textSize; // 字体的大小
private int width;
private Paint paint;
// 提供当前进度和最大值
private int progress;
private int max;
public RoundProgress(Context context) {
this(context, null);
}
public RoundProgress(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RoundProgress(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//1. 获取 typedArray 对象
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable. RoundProgress);
//2. 获取布局文件中声明的自定义属性的值
roundColor = typedArray.getColor(R.styleable. RoundProgress_roundColor, Color. GRAY);
roundProgressColor = typedArray.getColor(R.styleable. RoundProgress_roundProgressColor, Color. RED);
textColor = typedArray.getColor(R.styleable. RoundProgress_textColor, Color. GREEN);
roundWidth = ( int) typedArray.getDimension(R.styleable. RoundProgress_roundWidth, UIUtils. dp2px( 10));
textSize = ( int) typedArray.getDimension(R.styleable. RoundProgress_textSize, UIUtils. dp2px( 20));
progress = typedArray.getInteger(R.styleable. RoundProgress_progress, 40);
max = typedArray.getInteger(R.styleable. RoundProgress_max, 100);
//3. 回收
typedArray.recycle();
paint = new Paint(); // 初始化画笔
paint.setAntiAlias( true); // 去除毛边
}
public int getRoundColor() {
return roundColor;
}
public void setRoundColor( int roundColor) {
this. roundColor = roundColor;
}
public int getMax() {
return max;
}
public void setMax( int max) {
this. max = max;
}
public int getProgress() {
return progress;
}
public void setProgress( int progress) {
this. progress = progress;
}
@Override
protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = this.getMeasuredWidth(); // 获取当前视图的宽度
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 绘制圆环
// 设置圆环的中心点
int dx = width / 2;
int dy = width / 2;
// 设置半径
int radius = width / 2 - roundWidth / 2;
paint.setColor( roundColor);
paint.setStyle(Paint.Style. STROKE); // 设置画笔风格,空心 ( 画一个圆里面是空的 )
paint.setStrokeWidth( roundWidth); // 设置画笔的宽度,也就是线宽
canvas.drawCircle(dx, dy, radius, paint); // 画的圆的中心点,半径,取内圆与外圆的平均值
// 绘制圆弧
// 理解为包裹圆环中心线的圆的矩形
RectF rectF = new RectF( roundWidth / 2, roundWidth / 2, width - roundWidth / 2, width - roundWidth / 2);
paint.setColor( roundProgressColor);
canvas.drawArc(rectF, 0, progress * 360 / max, false, paint); //false 为不关联中心点
// 绘制文本
// 设置画笔
paint.setColor( textColor);
paint.setTextSize( textSize);
paint.setStrokeWidth( 0);
String text = progress* 100 / max + "%";
Rect bound= new Rect(); // 此时包裹文本的矩形框没有宽度和高度
paint.getTextBounds(text, 0,text.length(),bound); // 使其宽度和高度正好包裹文本内容
// 提供文本区域的左,下
int left= width/ 2-bound.width()/ 2;
int bottom= width/ 2+ bound.height()/ 2;
canvas.drawText(text,left,bottom, paint);
}
public class RoundProgress extends View {
// // 属性
// private int roundColor = Color.GRAY;
// private int roundProgressColor = Color.RED;
// private int textColor = Color.BLUE;
//
// private int roundWidth = UIUtils.dp2px(10);// 线的宽度
// private int textSize = UIUtils.dp2px(20);// 字体的大小
//
// private int width;
// private Paint paint;
//
// // 提供当前进度和最大值
// private int progress = 60;
// private int max = 100;
// 自定义属性的声明
private int roundColor;
private int roundProgressColor;
private int textColor;
private int roundWidth; // 线的宽度
private int textSize; // 字体的大小
private int width;
private Paint paint;
// 提供当前进度和最大值
private int progress;
private int max;
public RoundProgress(Context context) {
this(context, null);
}
public RoundProgress(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RoundProgress(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//1. 获取 typedArray 对象
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable. RoundProgress);
//2. 获取布局文件中声明的自定义属性的值
roundColor = typedArray.getColor(R.styleable. RoundProgress_roundColor, Color. GRAY);
roundProgressColor = typedArray.getColor(R.styleable. RoundProgress_roundProgressColor, Color. RED);
textColor = typedArray.getColor(R.styleable. RoundProgress_textColor, Color. GREEN);
roundWidth = ( int) typedArray.getDimension(R.styleable. RoundProgress_roundWidth, UIUtils. dp2px( 10));
textSize = ( int) typedArray.getDimension(R.styleable. RoundProgress_textSize, UIUtils. dp2px( 20));
progress = typedArray.getInteger(R.styleable. RoundProgress_progress, 40);
max = typedArray.getInteger(R.styleable. RoundProgress_max, 100);
//3. 回收
typedArray.recycle();
paint = new Paint(); // 初始化画笔
paint.setAntiAlias( true); // 去除毛边
}
public int getRoundColor() {
return roundColor;
}
public void setRoundColor( int roundColor) {
this. roundColor = roundColor;
}
public int getMax() {
return max;
}
public void setMax( int max) {
this. max = max;
}
public int getProgress() {
return progress;
}
public void setProgress( int progress) {
this. progress = progress;
}
@Override
protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = this.getMeasuredWidth(); // 获取当前视图的宽度
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 绘制圆环
// 设置圆环的中心点
int dx = width / 2;
int dy = width / 2;
// 设置半径
int radius = width / 2 - roundWidth / 2;
paint.setColor( roundColor);
paint.setStyle(Paint.Style. STROKE); // 设置画笔风格,空心 ( 画一个圆里面是空的 )
paint.setStrokeWidth( roundWidth); // 设置画笔的宽度,也就是线宽
canvas.drawCircle(dx, dy, radius, paint); // 画的圆的中心点,半径,取内圆与外圆的平均值
// 绘制圆弧
// 理解为包裹圆环中心线的圆的矩形
RectF rectF = new RectF( roundWidth / 2, roundWidth / 2, width - roundWidth / 2, width - roundWidth / 2);
paint.setColor( roundProgressColor);
canvas.drawArc(rectF, 0, progress * 360 / max, false, paint); //false 为不关联中心点
// 绘制文本
// 设置画笔
paint.setColor( textColor);
paint.setTextSize( textSize);
paint.setStrokeWidth( 0);
String text = progress* 100 / max + "%";
Rect bound= new Rect(); // 此时包裹文本的矩形框没有宽度和高度
paint.getTextBounds(text, 0,text.length(),bound); // 使其宽度和高度正好包裹文本内容
// 提供文本区域的左,下
int left= width/ 2-bound.width()/ 2;
int bottom= width/ 2+ bound.height()/ 2;
canvas.drawText(text,left,bottom, paint);
}
}
布局
<
com.example.administrator.p2p.ui.RoundProgress
android :id= "@+id/roundp_home"
android :layout_width= "120dp"
android :layout_height= "120dp"
android :layout_marginTop= "20dp"
whq :roundColor= "@color/product_detail_common"
whq :roundProgressColor= "@color/round_red_common"
whq :textColor= "@color/title_text"
whq :roundWidth= "10dp"
whq :textSize= "20sp"
whq :max= "100"
android :id= "@+id/roundp_home"
android :layout_width= "120dp"
android :layout_height= "120dp"
android :layout_marginTop= "20dp"
whq :roundColor= "@color/product_detail_common"
whq :roundProgressColor= "@color/round_red_common"
whq :textColor= "@color/title_text"
whq :roundWidth= "10dp"
whq :textSize= "20sp"
whq :max= "100"
whq:progress="86"/>
values下新建attrs文件
<
declare-styleable
name=
"RoundProgress">
< attr name= "roundColor" format= "color" />
< attr name= "roundProgressColor" format= "color" />
< attr name= "textColor" format= "color" />
< attr name= "roundWidth" format= "dimension" />
< attr name= "textSize" format= "dimension" />
< attr name= "progress" format= "integer" />
< attr name= "max" format= "integer" />
</ declare-styleable>
< attr name= "roundColor" format= "color" />
< attr name= "roundProgressColor" format= "color" />
< attr name= "textColor" format= "color" />
< attr name= "roundWidth" format= "dimension" />
< attr name= "textSize" format= "dimension" />
< attr name= "progress" format= "integer" />
< attr name= "max" format= "integer" />
</ declare-styleable>