自定義圓環
今天公司用到一個這東西就寫了一個,
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();
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1916/viewspace-2812645/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ios自定義圓環進度條iOS
- 自定義view實現半圓環View
- Android 實現自定義圓環Android
- Anroid自定義View-繪製圓環View
- 自定義圓環,跟隨手指旋轉角度加減layer
- 3個自定義view佈局:矩形TextView,圓形進度條,圓環viewTextView
- 分享一個Kotlin 寫的超級 簡單的自定義View,圓環統計KotlinView
- 自定義View:自定義屬性(自定義按鈕實現)View
- 08.Django自定義模板,自定義標籤和自定義過濾器Django過濾器
- 自定義ImageView完成圓形頭像自定義View
- 自定義VIEWView
- 自定義SnackBar
- 自定義useState
- 自定義_ajax
- 自定義Annotation
- 自定義OrderedMap
- 自定義 Drawable
- 自定義UICollectionViewLayoutUIView
- 自定義UITabBarUItabBar
- 自定義scrollbar
- 自定義Drawable
- 自定義ToastAST
- 自定義吐司
- android自定義view(自定義數字鍵盤)AndroidView
- vue自定義全域性元件(或自定義外掛)Vue元件
- android自定義View&自定義ViewGroup(下)AndroidView
- android自定義View&自定義ViewGroup(上)AndroidView
- Android自定義控制元件——自定義屬性Android控制元件
- CSS 繪製圓環CSS
- netty自定義Decoder用於自定義協議Netty協議
- 4. 自定義控制元件(4) --- 自定義屬性控制元件
- android自定義鍵盤 自定義身份證鍵盤Android
- Android自定義控制元件之自定義屬性Android控制元件
- GridView 自定義模版自定義修改,刪除,分頁View
- 自定義 Artisan 命令
- 自定義ClassLoader
- 自定義QTimeEditQT
- JavaScript自定義事件JavaScript事件