今天公司用到一个这东西就写了一个,
/**
* Created by T410-2 on 2018/1/18.
* 我们选取的位置都是已0,0点开始,
* 然后来画图,最后画的图的位置是由我们布局,
* 或者自身在把他移动到任何位置上;
*/
public class BroudProgress extends View {
private Paint mPaint=new Paint();
private int mRoundColor;
private int mRoundPross;
private int mRoundSize;
private int mTextColor;
private int mTextsize;
//画字体
private int mMax=100;
private int mProgress=50;
public BroudProgress(Context context) {
this(context, null);
}
public BroudProgress(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public BroudProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.BroudProgress);
mRoundColor = mTypedArray.getColor(R.styleable.BroudProgress_roundcolor, Color.RED);
mRoundPross = mTypedArray.getColor(R.styleable.BroudProgress_roundProssColor, Color.BLUE);
mRoundSize=mTypedArray.getDimensionPixelSize(R.styleable.BroudProgress_roundSize, 5);
mTextColor = mTypedArray.getColor(R.styleable.BroudProgress_textColor, Color.BLUE);
mTextsize= mTypedArray.getDimensionPixelSize(R.styleable.BroudProgress_textsize, 15);
mTypedArray.recycle();
}
/**
* getMeasuredWidth()获取的是view原始的大小,也就是这个view在XML文件中配置或者是代码中设置的大小。
* getWidth(相对于屏幕来说)获取的是这个view最终显示的大小,
* 这个大小有可能等于原始的大小也有可能不等于原始大小
*
* 第二说法:getMeasuredHeight()与getHeight()的区别:当屏幕可以包裹内容的时候,
* 他们的值相等当view的高度超出屏幕时,getMeasuredHeight()是实际View的大小,
* 与屏幕无关,getHeight的大小此时则是屏幕的大小。此时,getMeasuredHeight() = getHeight+超出部分。
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(mRoundColor);
mPaint.setStrokeWidth(mRoundSize);
mPaint.setStyle(Paint.Style.STROKE);
float with=getWidth()/2;
float radius=with-mRoundSize/2;
canvas.drawCircle(with,with,radius,mPaint);
//画字体
float measureText = mPaint.measureText(mProgress + "%");//测量字体的大小
//center - textWidth / 2, center + textSize / 2
float cxtext=with-measureText;
float cytext=with+mTextsize/2;
mPaint.setStrokeWidth(0);
mPaint.setColor(mTextColor);
mPaint.setTextSize(mTextsize);
canvas.drawText(mProgress + "%",cxtext,cytext,mPaint);
//画弧线
RectF mRectF=new RectF(with-radius,with-radius,with+radius,with+radius);
mPaint.setStrokeWidth(mRoundSize);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(mRoundPross);
canvas.drawArc(mRectF,0,360*mProgress/100,false,mPaint);
}
/**
* 设置进度
* @param progress
*/
public void setProgress(int progress){
this.mProgress=progress;
if (progress>100){
progress=100;
}
postInvalidate();
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章