自定义圆形进度条
2016-12-30 17:03
176 查看
自定义圆形进度条
*/
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"
whq:progress="86"/>
values下新建attrs文件
<declare-styleable name="RoundProgress">
<attr name="roundColor" format="color" />
<attr name="roundProgressColor" format="color" />
<attr name=
a738
"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>
*/
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"
whq:progress="86"/>
values下新建attrs文件
<declare-styleable name="RoundProgress">
<attr name="roundColor" format="color" />
<attr name="roundProgressColor" format="color" />
<attr name=
a738
"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>
相关文章推荐
- jquery mobile 实现自定义confirm确认框效果的简单实例
- C#实现自定义双击事件
- WinForm实现自定义右下角提示效果的方法
- Android 自定义View的构造函数详细介绍
- 一款超酷的Android自定义加载控件
- Android自定义View实现照片裁剪框与照片裁剪功能
- sql server 自定义分割月功能详解及实现代码
- 初学C++之自定义类型名简化详解
- MFC自定义消息的实现方法
- C#实现ProperTyGrid自定义属性的方法
- Android控件之ProgressBar用法实例分析
- php自定义错误处理用法实例
- ThinkPHP中自定义目录结构的设置方法
- C#自定义事件监听实现方法
- C#自定义事件及用法实例
- C#自定义签名章实现方法
- C#中使用IFormattable实现自定义格式化字符串输出示例
- javascript 自定义常用方法第1/2页
- BootStrap Progressbar 实现大文件上传的进度条的实例代码
- 谈谈JavaScript自定义回调函数