package com.example.administrator.sildslipyang.View;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ProgressBar;
import com.example.administrator.sildslipyang.R;
/**
* Created by Administrator on 2017/7/19.
* 自定义绘制圆形和弧形进度条 杨钊
*/
public class MyCircleProgress extends View{
public int progress = 0; //初始化进度值,进度实际值
//自定义控件属性,包括颜色,大小,类型
private int mr ; //半径
private int bgColor;//背景颜色
private int fgColor;//描绘的时候的颜色
private int drawstyle;//绘制类型,Fill是实心圆,stroke是空心圆
private int strokeWidth;//绘制线弧形的宽度
private int max;//最大值
private Paint paint;//绘制的dian
private boolean opt;
public synchronized void setProgress(int progress){
if (progress<0){
this.progress = 0;
}else if (progress>max){
this.progress = max;
}else {
this.progress = progress;
}
}
public int getMax(){
return max;
}
private void initProgress(Context context,AttributeSet attrs){
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyCircleProgress);
this.bgColor = typedArray.getColor(R.styleable.MyCircleProgress_bgColor, Color.GRAY);
this.fgColor = typedArray.getColor(R.styleable.MyCircleProgress_fgColor, Color.RED);
this.mr = typedArray.getInteger(R.styleable.MyCircleProgress_r,50);
this.strokeWidth = typedArray.getInteger(R.styleable.MyCircleProgress_strokeWidth,10);
this.max = typedArray.getInteger(R.styleable.MyCircleProgress_max,100);
this.drawstyle = typedArray.getInteger(R.styleable.MyCircleProgress_drawStyle,0);
}
public MyCircleProgress(Context context) {
super(context);
}
public MyCircleProgress(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.paint = new Paint();
paint.setAntiAlias(true);//去锯齿
paint.setStyle(Paint.Style.STROKE);
//初始化数据
initProgress(context,attrs);
}
public MyCircleProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int center = getWidth()/2;
paint.setStrokeWidth(strokeWidth);
paint.setColor(bgColor);//一开始的颜色
canvas.drawCircle(center,center,mr,paint);//开始绘制
paint.setColor(fgColor);//绘制时的颜色,必须写在drawCircle 方法后面
//默认画弧线 opt=false
if (drawstyle==0){
paint.setStyle(Paint.Style.STROKE);
opt = false;
}else {
paint.setStyle(Paint.Style.FILL);
opt = true;
}
int top = center-mr;
int bottom = center+mr;
RectF rectF = new RectF(top,top,bottom,bottom);
canvas.drawArc(rectF,270,360*progress/max,opt,paint);
}
}
<declare-styleable name="MyCircleProgress">
<attr name="bgColor" format="color"/>
<attr name="r" format="integer"/>
<attr name="fgColor" format="color"/>
<attr name="strokeWidth" format="integer"/>
<attr name="drawStyle" >
<enum name="FIll" value="1"/>
<enum name="STROKE" value="0"/>
</attr>
<attr name="max" format="integer"/>
</declare-styleable>
<com.example.administrator.sildslipyang.View.MyCircleProgress
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myProgress"
app:max="100"
app:bgColor="#feaaac"
app:fgColor="#0000ff"
app:drawStyle="FIll"
app:r="220"/>
在类中
max = myCircleProgress.getMax(); //xml里面填写的是100
new ProgressAnim().execute();//开始执行
class ProgressAnim extends AsyncTask<Void,Integer,Void> {
@Override
protected Void doInBackground(Void... voids) {
for (int i =0;i<=max;i++){
publishProgress(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
myCircleProgress.setProgress(values[0]);
myCircleProgress.invalidate();
}
}
![]()