一、了解自定義進度條
Android提供了ProgressBar控制項,可以用於顯示進度條,在進行長時間操作或載入資源時,可以通過進度條讓用戶感知到操作的進展。但是ProgressBar本身的樣式有限,不能滿足特殊的設計需求,此時可以考慮自定義進度條。
自定義進度條可以靈活控制進度的樣式、顏色、大小等,同時也可以進行動態的渲染和交互。自定義進度條一般使用Canvas和Paint來繪製進度效果,通過實現自定義View或繼承ProgressBar來實現。
二、自定義View實現進度條
1、新建一個自定義View,繼承View類,並實現onDraw()方法。onDraw()方法是用來繪製自定義View的主要方法。這裡我們使用Canvas和Paint來進行繪製,繪製的內容是一個矩形,矩形的長度和寬度都是根據當前進度值來計算得出。
public class CustomProgressBar extends View { private Paint mPaint; private RectF mRectF; private int mProgressBarColor; private int mProgressBarHeight; private int mMaxValue; private int mCurrentValue; public CustomProgressBar(Context context) { super(context); initView(); } public CustomProgressBar(Context context, AttributeSet attrs) { super(context, attrs); initView(); parseAttrs(attrs); } public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); parseAttrs(attrs); } private void initView() { mProgressBarColor = Color.BLUE; mProgressBarHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics()); mMaxValue = 100; mCurrentValue = 0; mPaint = new Paint(); mPaint.setStyle(Paint.Style.FILL); mPaint.setAntiAlias(true); mRectF = new RectF(); } private void parseAttrs(AttributeSet attrs) { TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar); mProgressBarColor = typedArray.getColor(R.styleable.CustomProgressBar_progressBarColor, Color.BLUE); mProgressBarHeight = typedArray.getDimensionPixelSize(R.styleable.CustomProgressBar_progressBarHeight, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics())); mMaxValue = typedArray.getInt(R.styleable.CustomProgressBar_maxValue, 100); mCurrentValue = typedArray.getInt(R.styleable.CustomProgressBar_currentValue, 0); typedArray.recycle(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setColor(mProgressBarColor); mRectF.set(0, 0, getWidth() * mCurrentValue / mMaxValue, mProgressBarHeight); canvas.drawRect(mRectF, mPaint); } public void setMaxValue(int maxValue) { this.mMaxValue = maxValue; } public void setCurrentValue(int currentValue) { this.mCurrentValue = currentValue; invalidate(); } }
2、在attrs.xml文件中定義自定義屬性,這些屬性將用於在XML布局文件中設置進度條的樣式和屬性。
3、在XML布局文件中使用自定義進度條。
4、在Activity中設置自定義進度條的最大值和當前值,調用setCurrentValue()方法更新進度條的進度。
CustomProgressBar progressBar = findViewById(R.id.progress_bar); progressBar.setMaxValue(100); progressBar.setCurrentValue(50);
三、繼承ProgressBar實現進度條
1、新建一個自定義ProgressBar,繼承ProgressBar類,重寫onDraw()方法。onDraw()方法是用來繪製進度條的主要方法。這裡我們使用Canvas和Paint來進行繪製,繪製的內容是一個矩形。
public class CustomProgressBar extends ProgressBar { private Paint mPaint; private RectF mRectF; private int mProgressBarColor; private int mProgressBarHeight; public CustomProgressBar(Context context) { super(context); initView(); } public CustomProgressBar(Context context, AttributeSet attrs) { super(context, attrs); initView(); parseAttrs(attrs); } public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); parseAttrs(attrs); } private void initView() { mProgressBarColor = Color.BLUE; mProgressBarHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics()); mPaint = new Paint(); mPaint.setStyle(Paint.Style.FILL); mPaint.setAntiAlias(true); mRectF = new RectF(); } private void parseAttrs(AttributeSet attrs) { TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar); mProgressBarColor = typedArray.getColor(R.styleable.CustomProgressBar_progressBarColor, Color.BLUE); mProgressBarHeight = typedArray.getDimensionPixelSize(R.styleable.CustomProgressBar_progressBarHeight, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics())); typedArray.recycle(); } @Override protected synchronized void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setColor(mProgressBarColor); mRectF.set(getPaddingLeft(), getHeight() - mProgressBarHeight - getPaddingBottom(), getWidth() * getProgress() / getMax() - getPaddingRight(), getHeight() - getPaddingBottom()); canvas.drawRect(mRectF, mPaint); } }
2、在attrs.xml文件中定義自定義屬性,這些屬性將用於在XML布局文件中設置進度條的樣式和屬性。
3、在XML布局文件中使用自定義進度條。
4、在Activity中設置自定義進度條的最大值和當前值,調用setProgress()方法更新進度條的進度。
CustomProgressBar progressBar = findViewById(R.id.progress_bar); progressBar.setMax(100); progressBar.setProgress(50);
綜上所述,通過自定義View或繼承ProgressBar可以實現自定義進度條,可以根據需求來選擇實現方式,並進行樣式和屬性的自定義。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/252088.html