ITPub博客

首页 > IT职业 > IT图书 > 自定义圆环

自定义圆环

IT图书 作者:kboypkb 时间:2021-09-09 09:48:08 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();

}


@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);


}


public void setProgress(int progress){
this.mProgress=progress;
if (progress>100){
progress=100;
}
postInvalidate();
}
}

原文链接:http://www.apkbus.com/blog-340477-76909.html

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1916/viewspace-2812645/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-07-24

  • 博文量
    103
  • 访问量
    8754