Android自定義View實(shí)現(xiàn)圓形進(jìn)度條
本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)圓形進(jìn)度條的具體代碼,供大家參考,具體內(nèi)容如下
效果如下:
主要代碼
CircularProgressView.java
public class CircularProgressView extends View { private Paint mBackPaint, mProgPaint; // 繪制畫筆 private RectF mRectF; // 繪制區(qū)域 private int[] mColorArray; // 圓環(huán)漸變色 private int mProgress; // 圓環(huán)進(jìn)度(0-100) /** * 繪制弧線的畫筆 */ private Paint progressPaint; /** * 圓弧圓心位置 */ private int centerX, centerY; /** * 圓弧的半徑 */ private int circleRadius; public CircularProgressView(Context context) { this(context, null); } public CircularProgressView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public CircularProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); @SuppressLint('Recycle') TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircularProgressView); // 初始化背景圓環(huán)畫筆 mBackPaint = new Paint(); mBackPaint.setStyle(Paint.Style.STROKE); // 只描邊,不填充 mBackPaint.setStrokeCap(Paint.Cap.ROUND); // 設(shè)置圓角 mBackPaint.setAntiAlias(true); // 設(shè)置抗鋸齒 mBackPaint.setDither(true); // 設(shè)置抖動(dòng) mBackPaint.setStrokeWidth(typedArray.getDimension(R.styleable.CircularProgressView_backWidth, 5)); mBackPaint.setColor(typedArray.getColor(R.styleable.CircularProgressView_progbgColor, Color.LTGRAY)); // 初始化進(jìn)度圓環(huán)畫筆 mProgPaint = new Paint(); mProgPaint.setStyle(Paint.Style.STROKE); // 只描邊,不填充 mProgPaint.setStrokeCap(Paint.Cap.ROUND); // 設(shè)置圓角 mProgPaint.setAntiAlias(true); // 設(shè)置抗鋸齒 mProgPaint.setDither(true); // 設(shè)置抖動(dòng) mProgPaint.setStrokeWidth(typedArray.getDimension(R.styleable.CircularProgressView_progWidth, 10)); mProgPaint.setColor(typedArray.getColor(R.styleable.CircularProgressView_progColor, Color.BLUE)); //初始化結(jié)束位置小圓點(diǎn) progressPaint = new Paint(); progressPaint.setStyle(Paint.Style.FILL); // 填充 progressPaint.setStrokeCap(Paint.Cap.ROUND); // 設(shè)置圓角 progressPaint.setAntiAlias(true); // 設(shè)置抗鋸齒 progressPaint.setDither(true); // 設(shè)置抖動(dòng) progressPaint.setStrokeWidth(typedArray.getDimension(R.styleable.CircularProgressView_progWidth, 10)); progressPaint.setColor(Color.WHITE); // 初始化進(jìn)度圓環(huán)漸變色 int startColor = typedArray.getColor(R.styleable.CircularProgressView_progStartColor, -1); int firstColor = typedArray.getColor(R.styleable.CircularProgressView_progFirstColor, -1); if (startColor != -1 && firstColor != -1) mColorArray = new int[]{startColor, firstColor}; else mColorArray = null; // 初始化進(jìn)度 mProgress = typedArray.getInteger(R.styleable.CircularProgressView_progress, 0); typedArray.recycle(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int viewWide = getMeasuredWidth() - getPaddingLeft() - getPaddingRight(); int viewHigh = getMeasuredHeight() - getPaddingTop() - getPaddingBottom(); int mRectLength = (int) ((viewWide > viewHigh ? viewHigh : viewWide) - (mBackPaint.getStrokeWidth() > mProgPaint.getStrokeWidth() ? mBackPaint.getStrokeWidth() : mProgPaint.getStrokeWidth())); int mRectL = getPaddingLeft() + (viewWide - mRectLength) / 2; int mRectT = getPaddingTop() + (viewHigh - mRectLength) / 2; mRectF = new RectF(mRectL, mRectT, mRectL + mRectLength, mRectT + mRectLength); centerX = getMeasuredWidth() / 2; centerY = getMeasuredHeight() / 2; //計(jì)算圓弧半徑和圓心點(diǎn) circleRadius = Math.min(getMeasuredWidth(), getMeasuredHeight()) / 2; circleRadius-=8; // 設(shè)置進(jìn)度圓環(huán)漸變色 if (mColorArray != null && mColorArray.length > 1) mProgPaint.setShader(new LinearGradient(0, 0, 0, getMeasuredWidth(), mColorArray, null, Shader.TileMode.MIRROR)); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawArc(mRectF, 0, 360, false, mBackPaint); canvas.drawArc(mRectF, 270, 360 * mProgress / 100, false, mProgPaint); //繪制結(jié)束位置小圓形 progressPaint.setStrokeWidth(10); progressPaint.setStyle(Paint.Style.FILL); float swipe = 360 * mProgress / 100; Log.d('=================', swipe + ' mProgress'); float radians = (float) (((swipe - 90) / 2) / 180 * 2 * Math.PI); float endX; float endY; endX = centerX + circleRadius * (float) Math.cos(radians); endY = centerY + circleRadius * (float) Math.sin(radians); if (mProgress!=0) { canvas.drawCircle(endX, endY, 8, progressPaint); } } /** * 獲取當(dāng)前進(jìn)度 * * @return 當(dāng)前進(jìn)度(0-100) */ public int getProgress() { return mProgress; } /** * 設(shè)置當(dāng)前進(jìn)度 * * @param progress 當(dāng)前進(jìn)度(0-100) */ public void setProgress(int progress) { this.mProgress = progress; invalidate(); } /** * 設(shè)置當(dāng)前進(jìn)度,并展示進(jìn)度動(dòng)畫。如果動(dòng)畫時(shí)間小于等于0,則不展示動(dòng)畫 * * @param progress 當(dāng)前進(jìn)度(0-100) * @param animTime 動(dòng)畫時(shí)間(毫秒) */ public void setProgress(int progress, long animTime) { if (animTime <= 0) setProgress(progress); else { ValueAnimator animator = ValueAnimator.ofInt(mProgress, progress); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) { mProgress = (int) animation.getAnimatedValue(); invalidate();} }); animator.setInterpolator(new OvershootInterpolator()); animator.setDuration(animTime); animator.start(); } } /** * 設(shè)置背景圓環(huán)寬度 * * @param width 背景圓環(huán)寬度 */ public void setBackWidth(int width) { mBackPaint.setStrokeWidth(width); invalidate(); } /** * 設(shè)置背景圓環(huán)顏色 * * @param color 背景圓環(huán)顏色 */ public void setBackColor(/*@ColorRes int color*/String color) { mBackPaint.setColor(Color.parseColor(color)); //畫筆顏色 invalidate(); } /** * 設(shè)置進(jìn)度圓環(huán)寬度 * * @param width 進(jìn)度圓環(huán)寬度 */ public void setProgWidth(int width) { mProgPaint.setStrokeWidth(width); invalidate(); } /** * 設(shè)置進(jìn)度圓環(huán)顏色 * * @param color 景圓環(huán)顏色 */ public void setProgColor(/*@ColorRes int color*/String color) { mProgPaint.setColor(Color.parseColor(color)); //畫筆顏色 mProgPaint.setShader(null); invalidate(); } /** * 設(shè)置進(jìn)度圓環(huán)顏色(支持漸變色) * * @param startColor 進(jìn)度圓環(huán)開始顏色 * @param firstColor 進(jìn)度圓環(huán)結(jié)束顏色 */ public void setProgColor(@ColorRes int startColor, @ColorRes int firstColor) { mColorArray = new int[]{ContextCompat.getColor(getContext(), startColor), ContextCompat.getColor(getContext(), firstColor)}; mProgPaint.setShader(new LinearGradient(0, 0, 0, getMeasuredWidth(), mColorArray, null, Shader.TileMode.MIRROR)); invalidate(); } /** * 設(shè)置進(jìn)度圓環(huán)顏色(支持漸變色) * * @param colorArray 漸變色集合 */ public void setProgColor(@ColorRes int[] colorArray) { if (colorArray == null || colorArray.length < 2) return; mColorArray = new int[colorArray.length]; for (int index = 0; index < colorArray.length; index++) mColorArray[index] = ContextCompat.getColor(getContext(), colorArray[index]); mProgPaint.setShader(new LinearGradient(0, 0, 0, getMeasuredWidth(), mColorArray, null, Shader.TileMode.MIRROR)); invalidate(); }}
attrs.xml
<declare-styleable name='CircularProgressView'> <attr name='backWidth' format='dimension' /> <!--背景圓環(huán)寬度--> <attr name='progWidth' format='dimension' /> <!--進(jìn)度圓環(huán)寬度--> <attr name='progbgColor' format='color' /> <!--背景圓環(huán)顏色--> <attr name='progColor' format='color' /> <!--進(jìn)度圓環(huán)顏色--> <attr name='progStartColor' format='color' /> <!--進(jìn)度圓環(huán)開始顏色--> <attr name='progFirstColor' format='color' /> <!--進(jìn)度圓環(huán)結(jié)束顏色--> <attr name='progress' format='integer' /> <!--圓環(huán)進(jìn)度--> </declare-styleable>
使用方法
<com.view.CircularProgressView android:layout_width='170dp' android:layout_height='170dp' android:layout_centerInParent='true' android:layout_centerHorizontal='true' app:backWidth='5dp' app:progWidth='5dp' app:progbgColor='#4C5098' app:progress='50' android:layout_marginTop='500dp' />
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python中Anaconda3 安裝gdal庫(kù)的方法2. PHP AOP教程案例3. python用zip壓縮與解壓縮4. python公司內(nèi)項(xiàng)目對(duì)接釘釘審批流程的實(shí)現(xiàn)5. Notepad++如何配置python?配置python操作流程詳解6. Python 簡(jiǎn)介7. Python操作Excel工作簿的示例代碼(*.xlsx)8. JAVA如何轉(zhuǎn)換樹結(jié)構(gòu)數(shù)據(jù)代碼實(shí)例9. Python importlib模塊重載使用方法詳解10. Python自動(dòng)化之定位方法大殺器xpath
