//自定义进度圆圈
package com.bw.20171104;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by user on 2017/11/4.
*/
public class ViewBar 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 static final int STROKE = 0;
public static final int FILL = 1;
public ViewBar(Context context) {
this(context, null);
init();
}
public ViewBar(Context context, AttributeSet attrs) {
super(context, attrs);
getCustomAttr(context,attrs);//attrs为控件中的属性集合
init();
}
public ViewBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void getCustomAttr(Context context, AttributeSet attrs) {
TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.ViewBar);
//第一个参数为控件中输入的值,第二个参数为默认值
mRadiuSize=array.getDimension(R.styleable.ViewBar_radiuSize,100);
mRingSize=array.getDimension(R.styleable.ViewBar_ringSize,10);
mTextSize=array.getDimension(R.styleable.ViewBar_textSize,10);
mProgressColor=array.getColor(R.styleable.ViewBar_progressColor,Color.BLACK);
}
private void init() {
paint=new Paint();//创建画笔对象
paint.setAntiAlias(true);//抗锯齿
}
//warpcontent类型 MeasureSpec.AT_MOST
//matchparent类型 或者具体的长度 100dp 200dp MeasureSpec.EXACTLY
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode=MeasureSpec.getMode(widthMeasureSpec);//获取宽的模式
int heightMode=MeasureSpec.getMode(heightMeasureSpec);//获取高的模式
int widthSize=MeasureSpec.getSize(widthMeasureSpec);//获取输入的宽的值
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));
}
//给自定义控件设置宽高