自定義圓環

kboypkb發表於2021-09-09

今天公司用到一個這東西就寫了一個,



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部落格 ” ,連結:http://blog.itpub.net/1916/viewspace-2812645/,如需轉載,請註明出處,否則將追究法律責任。

相關文章