新建Class继承view
2.新建xml文件,设置控件属性
`<resources>
<declare-styleable name="Flwp">
<attr name="outcolor" format="color"></attr>
<attr name="intcolor" format="color"></attr>
<attr name="border_width" format="dimension"></attr>
<attr name="step_text" format="dimension"></attr>
<attr name="text_color" format="color"></attr>
<attr name="step_size" format="dimension"></attr>
</declare-styleable>
</resources>`
引用属性
TypedArray array= context.obtainStyledAttributes(attrs,R.styleable.Flwp);
moutcolor=array.getColor(R.styleable.Flwp_outcolor,moutcolor);
minnercolor=array.getColor(R.styleable.Flwp_intcolor,minnercolor);
mbwidth= (int) array.getDimension(R.styleable.Flwp_border_width,mbwidth);
mtcolor=array.getColor(R.styleable.Flwp_text_color,mtcolor);
mtext= (int) array.getDimension(R.styleable.Flwp_step_text,0);
mSize=array.getDimensionPixelSize(R.styleable.Flwp_step_size,mSize);
array.recycle();//使用结束后回收
设置框体
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width=MeasureSpec.getSize(widthMeasureSpec);
int height=MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width<height?width:height,width<height?width:height);
}
重绘
int center=getWidth()/2;
int radios=(getWidth()-mbwidth)/2;
RectF rectF=new RectF(mbwidth/2,mbwidth/2,center+radios,center+radios);
canvas.drawArc(rectF,135,275,false,mpaint);
float wight=(float)mtext/mj;
canvas.drawArc(rectF,135,(float)275*wight,false,mpaint_2);
String mcontext=mtext+"°";
Rect rec=new Rect();
mtextpaint.getTextBounds(mcontext,0,mcontext.length(),rec);
int dx=getWidth()/2-rec.width()/2;
Paint.FontMetricsInt fontMetricsInt=mtextpaint.getFontMetricsInt();
int dy=fontMetricsInt.bottom-fontMetricsInt.top;
int baseLine=getHeight()/2+dy/2;
canvas.drawText(mcontext,dx,baseLine,mtextpaint);
}