自訂View實現Android圓形進度條,支援自訂顯示的樣式

來源:互聯網
上載者:User

自訂View實現Android圓形進度條,支援自訂顯示的樣式

我是完全根據這裡仿製了一個作為備忘,可以點擊這裡查看原始版本

代碼如下:

1、res/values/attrs.xml

 


2、具體實現

 

 

public class RoundProgressBar extends View {private Paint mPaint;private int mRoundColor;private int mProgressColor;private int mTextColor;private int mTextSize;private float mRounWidth;private int mMaxProgress;private int mCurrentProgress = 45;private boolean mTextIsDisplayable;private int mStyle = 1;private static final int STROKE = 0;private static final int FILL =1;public RoundProgressBar(Context context) {this(context, null);}public RoundProgressBar(Context context, AttributeSet attrs) {this(context, attrs, 0);}public RoundProgressBar(Context context, AttributeSet attrs,int defStyleAttr) {super(context, attrs, defStyleAttr);mPaint = new Paint();TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);mRoundColor = typedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);mProgressColor = typedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);mTextColor = typedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.BLUE);mTextSize = typedArray.getDimensionPixelSize(R.styleable.RoundProgressBar_textSize, 15);mRounWidth = typedArray.getDimensionPixelSize(R.styleable.RoundProgressBar_roundWidth, 5);mMaxProgress = typedArray.getInt(R.styleable.RoundProgressBar_max, 100);mTextIsDisplayable = typedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);mStyle = typedArray.getInt(R.styleable.RoundProgressBar_style, STROKE);typedArray.recycle();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);// Draw outer circleint center = getWidth() / 2;int radius = (int) (center - mRounWidth / 2);mPaint.setColor(mRoundColor);mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(mRounWidth);mPaint.setAntiAlias(true);canvas.drawCircle(center, center, radius, mPaint);// Draw progressmPaint.setStrokeWidth(mRounWidth);mPaint.setColor(mProgressColor);RectF oval = new RectF(center - radius, center - radius,center + radius, center + radius);switch (mStyle) {case STROKE:mPaint.setStyle(Paint.Style.STROKE);canvas.drawArc(oval, 0, 360 * mCurrentProgress / mMaxProgress, false, mPaint);break;case FILL:mPaint.setStyle(Paint.Style.FILL_AND_STROKE);if (mCurrentProgress != 0) {canvas.drawArc(oval, 0, 360 * mCurrentProgress / mMaxProgress, true, mPaint);}break;default:break;}mPaint.setStrokeWidth(0);mPaint.setColor(mTextColor);mPaint.setTextSize(mTextSize);mPaint.setTypeface(Typeface.DEFAULT);int percent = (int) (((float) mCurrentProgress / (float) mMaxProgress) * 100);float textWidth = mPaint.measureText(percent + "%");if (percent != 0 && mTextIsDisplayable) {canvas.drawText(percent + "%", center - textWidth / 2, center + mTextSize / 2, mPaint);}}public int getRoundColor() {return mRoundColor;}public void setRoundColor(int roundColor) {this.mRoundColor = roundColor;}public int getProgressColor() {return mProgressColor;}public void setProgressColor(int progressColor) {this.mProgressColor = progressColor;}public int getTextColor() {return mTextColor;}public void setTextColor(int textColor) {this.mTextColor = textColor;}public int getTextSize() {return mTextSize;}public void setTextSize(int textSize) {this.mTextSize = textSize;}public float getRounWidth() {return mRounWidth;}public void setmRounWidth(float mRounWidth) {this.mRounWidth = mRounWidth;}public synchronized int getMaxProgress() {return mMaxProgress;}public synchronized void setMaxProgress(int maxProgress) {if (maxProgress < 0) {throw new IllegalArgumentException("Max Progress not less than 0");} this.mMaxProgress = maxProgress;}public synchronized int getCurrentProgress() {return mCurrentProgress;}public synchronized void setCurrentProgress(int currentProgress) {if (currentProgress < 0) {throw new IllegalArgumentException("Progress not less than 0");}if (currentProgress > mMaxProgress) {currentProgress = mMaxProgress;}if (currentProgress <= mMaxProgress) {this.mCurrentProgress = currentProgress;postInvalidate();}}public boolean isTextIsDisplayable() {return mTextIsDisplayable;}public void setTextDisplayable(boolean displayable) {this.mTextIsDisplayable = displayable;}public int getStyle() {return mStyle;}public void setStyle(int style) {this.mStyle = style;}}

 

 

3、布局檔案中使用

 

<framelayout android:layout_height="match_parent" android:layout_width="match_parent" tools:context="com.example.gaussianblur.MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android_custom="http://schemas.android.com/apk/res/com.example.demo" xmlns:tools="http://schemas.android.com/tools">    </framelayout>

 

 

4、




 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.