public class MyProgressRound extends View{
Paint paint;
private int mProgress = 0;
private int mCountProgress = 0;
private float mRadiuSize = 0;
private float mRingSize = 0;
private float mTextSize = 0;
private int mProgressColor = 0;
public MyProgressRound(Context context) {
super(context);
init();
}
/**
* 所有在xml佈局文件中 標籤裏面聲明的屬性 都可以在AttributeSet類的對象中獲取出來
* @param context
* @param attrs
*/
public MyProgressRound(Context context, @Nullable AttributeSet attrs) {
//在該構造方法中可以獲取到 所有參數
//把參數傳出去 在onDraw方法中可以操作 onMeasure中也可以操作
super(context, attrs);
getCustomAttr(context, attrs);
init();
}
private void getCustomAttr(Context context, AttributeSet attrs) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyProgressRound);
mRadiuSize = array.getDimension(R.styleable.MyProgressRound_radiuSize, 100);
mRingSize = array.getDimension(R.styleable.MyProgressRound_ringSize, 10);
mTextSize = array.getDimension(R.styleable.MyProgressRound_textSize, 10);
mProgressColor = array.getColor(R.styleable.MyProgressRound_progressColor, Color.BLACK);
}
public MyProgressRound(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void init(){
paint = new Paint();
paint.setAntiAlias(true);
}
//widthMeasureSpec/heightMeasureSpec 是一個32爲的int類型
//01000000 00000000 00000000 00000000
//高兩位 代表類型
//warpcontent類型 MeasureSpec.AT_MOST
//matchparent類型 MeasureSpec.EXACTLY 或者具體的長度 100dp 200dp
// 其他類型
//
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width = 0;
int height = 0;
if(widthMode == MeasureSpec.AT_MOST){
width = (int) (mRadiuSize * 2);
}else{
width = Math.max(widthSize, (int) (mRadiuSize * 2));
}
if(heightMode == MeasureSpec.AT_MOST){
height = (int) (mRadiuSize * 2);
}else{
height = Math.max(heightSize, (int) (mRadiuSize * 2));
}
setMeasuredDimension(width, height);
// switch (widthMode){
// case MeasureSpec.AT_MOST:
// //如果寬度使用的是warp_content那麼 我們需要手動設置控件的寬值, 標準是寬是半徑的2倍
// Log.i("=============widthMode", "onMeasure: " + "AT_MOST---> warp_content");
// //確定了寬高後,修改控件的寬高
// setMeasuredDimension((int)(mRadiuSize * 2), (int)(mRadiuSize * 2));
// break;
// case MeasureSpec.EXACTLY:
// Log.i("=============widthMode", "onMeasure: " + "Exactly---> match_parent");
// break;
// }
//
// switch (heightMode){
// case MeasureSpec.AT_MOST:
// Log.i("=============heightMode", "onMeasure: " + "AT_MOST---> warp_content");
// break;
// case MeasureSpec.EXACTLY:
// Log.i("=============heightMode", "onMeasure: " + "Exactly---> match_parent");
// break;
// }
}
@Override
protected void onDraw(Canvas canvas) {
//在佈局文件中設置的圓環半徑大小就可以不用寫死
paint.setStrokeWidth(0);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, mRadiuSize, paint);
canvas.drawCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, mRadiuSize - mRingSize, paint);
paint.setTextSize(mTextSize);
String text = mCountProgress + "%";
float textWidth = paint.measureText(text);
canvas.drawText(text, getMeasuredWidth()/2-textWidth/2 , getMeasuredWidth()/2 + mTextSize/2, paint);
RectF rectF = new RectF(getMeasuredWidth()/2 - mRadiuSize + mRingSize/2,getMeasuredHeight()/2 - mRadiuSize + mRingSize/2,getMeasuredWidth()/2 + mRadiuSize - mRingSize/2,getMeasuredHeight()/2 + mRadiuSize - mRingSize/2);
paint.setStrokeWidth(mRingSize);
paint.setColor(mProgressColor);
canvas.drawArc(rectF, 0, mProgress, false, paint);
}
public void setProgress(int progress){
mProgress = progress;
mCountProgress = progress * 100 / 360;
invalidate();
}
}
======================================================================
public class MainActivity extends AppCompatActivity {
MyProgressRound mpr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mpr = (MyProgressRound) findViewById(R.id.mpr);
new AsyncTask<String, Integer, String>(){
@Override
protected String doInBackground(String... params) {
for (int i = 0; i <= 360; i++) {
SystemClock.sleep(10);
publishProgress(i);
}
return null;
}
//該方法的調用條件是 publishProgress
@Override
protected void onProgressUpdate(Integer... values) {
mpr.setProgress(values[0]);
}
}.execute();
}
}