Android一个自定义的进度环:ProgressChart
生活随笔
收集整理的這篇文章主要介紹了
Android一个自定义的进度环:ProgressChart
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
源代碼及可執行文件下載地址:http://files.cnblogs.com/rainboy2010/ProgressChart.zip
因項目需要,自己嘗試定義了一個進度環,用于顯示進度,實現效果如下:
主要代碼如下:
public class ProgressChart extends View {private Context context;//圓環背景畫筆private Paint paintBg;//圓環進度畫筆private Paint paintProgress;//文字畫筆1private Paint paintText1;//文字畫筆2private Paint paintText2;// 圓環的寬度 private float progressWidth; //圓環的區域private RectF roundRect;//文字的區域private Rect textRect;//單個字符的區域private Rect charRect;//繪制時每次增加的度數private float rotateDegree = 1.0F;//繪制開始的度數private float startDegree = 0.0F;//結束的度數private float endDegree;//背景顏色private int bgColor;//進度顏色private int progressColor;// 中間進度百分比的字符串的顏色 private int textColor; //字符串的文字大小private float text1Size;//字符串的文字大小private float text2Size;//繪制的字符串private String text="0%";public ProgressChart(Context context) {this(context, null);}public ProgressChart(Context context, AttributeSet attrs) {this(context, attrs, 0);}public ProgressChart(Context context, AttributeSet attrs,int defStyleAttr) {super(context, attrs, defStyleAttr);this.context=context;init(attrs);}private void init(AttributeSet attrs) {TypedArray mTypedArray = context.obtainStyledAttributes(attrs,R.styleable.ProgressChart);this.progressWidth = mTypedArray.getDimension(R.styleable.ProgressChart_progressWidth, 8);this.text1Size = mTypedArray.getDimension(R.styleable.ProgressChart_textSize1, 32);this.text2Size = mTypedArray.getDimension(R.styleable.ProgressChart_textSize2, 20);this.bgColor = mTypedArray.getColor(R.styleable.ProgressChart_bgColor,Color.parseColor("#fff2f2f2"));this.progressColor = mTypedArray.getColor(R.styleable.ProgressChart_progressColor,Color.parseColor("#fffd0000"));this.textColor = mTypedArray.getColor(R.styleable.ProgressChart_txtColor,Color.parseColor("#fffd0000"));mTypedArray.recycle();this.textRect = new Rect();this.charRect = new Rect();this.paintBg = new Paint();this.paintBg.setStyle(Paint.Style.STROKE);this.paintBg.setStrokeWidth(this.progressWidth);this.paintBg.setColor(this.bgColor);this.paintProgress = new Paint();this.paintProgress.setStyle(Paint.Style.STROKE);this.paintProgress.setStrokeWidth(this.progressWidth);this.paintProgress.setColor(this.progressColor);this.paintText1 = new Paint();this.paintText1.setTextSize(this.text1Size);this.paintText1.setTextAlign(Paint.Align.CENTER);this.paintText1.setColor(this.textColor);this.paintText2 = new Paint();this.paintText2.setTextSize(this.text2Size);this.paintText2.setTextAlign(Paint.Align.CENTER);this.paintText2.setColor(this.textColor);}public boolean setProgress(String progress){this.text = DecimalFormat.getPercentInstance().format(Double.valueOf(progress));this.startDegree = 0.0F;this.endDegree = (360.0F * Float.valueOf(progress).floatValue());this.rotateDegree = (this.endDegree / 40.0F);invalidate();return true;}@Overrideprotected void onDraw(Canvas canvas){//繪制圓環背景canvas.drawArc(this.roundRect, 0.0F, 360.0F, false, this.paintBg);//繪制進度if (this.startDegree < this.endDegree){canvas.drawArc(this.roundRect, -90.0F, this.startDegree, false, this.paintProgress);this.startDegree += this.rotateDegree;invalidate();}else{canvas.drawArc(this.roundRect, -90.0F, this.endDegree, false, this.paintProgress);}if(!TextUtils.isEmpty(this.text)){//繪制文字this.paintText1.getTextBounds(this.text, 0, this.text.length(), this.textRect);this.paintText2.getTextBounds("%", 0, 1, this.charRect);FontMetricsInt fontMetricsInt = this.paintText1.getFontMetricsInt();float y = this.roundRect.top + (this.roundRect.bottom - this.roundRect.top - fontMetricsInt.bottom + fontMetricsInt.top) / 2.0F - 5 * fontMetricsInt.top / 5;canvas.drawText(this.text.replace("%", ""), this.roundRect.centerX() - this.charRect.width() / 2, y, this.paintText1);canvas.drawText("%", this.roundRect.centerX() + this.textRect.width() / 2 - this.charRect.width() / 2, y, this.paintText2);}}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {this.roundRect = new RectF(this.progressWidth, this.progressWidth, w - this.progressWidth, h - this.progressWidth);}}?
轉載于:https://www.cnblogs.com/rainboy2010/p/5391643.html
總結
以上是生活随笔為你收集整理的Android一个自定义的进度环:ProgressChart的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机结构原理初步教案板书设计,(计算机
- 下一篇: 江苏计算机信息录入技师选拔考试题,201