android自定義等級(jí)評(píng)分圓形進(jìn)度條
本文實(shí)例為大家分享了android評(píng)分圓形進(jìn)度條的具體代碼,供大家參考,具體內(nèi)容如下
一、測(cè)試截圖
二、實(shí)現(xiàn)原理
package com.freedomanlib; import java.util.Timer; import java.util.TimerTask; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.Typeface; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * @name GradeProgressBar * @Descripation 自定義等級(jí)評(píng)分圓形進(jìn)度條,用于設(shè)備數(shù)據(jù)統(tǒng)計(jì)頁(yè)面一鍵評(píng)分<br> * 1、初始化邊界寬度、中心坐標(biāo)和外環(huán)、內(nèi)環(huán)半徑,各種畫筆。<br> * 2、默認(rèn)最大進(jìn)度為100,目標(biāo)進(jìn)度由用戶來(lái)指定。<br> * 3、鎖定一個(gè)內(nèi)圓環(huán)為可點(diǎn)擊區(qū)域。 <br> * 4、點(diǎn)擊組件時(shí),調(diào)用start()方法啟動(dòng)計(jì)時(shí)器,重繪界面。<br> * @author Freedoman * @date 2014-10-29 * @version 1.0 */ public class GradeProgressBar extends View { private static final String TAG = "CircleProgressBar"; /** * 邊界寬度、中心坐標(biāo)和外環(huán)、內(nèi)環(huán)半徑 */ private float boundsWidth; private float centerPoint; private float overRadius; private float radius; /** * 最大進(jìn)度、當(dāng)前進(jìn)度、是否顯示進(jìn)度文本 */ private float maxProgress = 100; private float targetProgress; private int curProgress; /** * 幾種畫筆 */ private Paint overRoundPaint; private Paint roundPaint; private Paint progressRoundPaint; private Paint progressTextPaint; private Paint textPaint; /** * 可點(diǎn)擊區(qū)域的邊界 */ private float clickBoundsLow; private float clickBoundsHigh; private onProgressChangedListener listener; public GradeProgressBar(Context context) { this(context, null); } public GradeProgressBar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public GradeProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.initialize(); } /** * 初始化 */ private void initialize() { curProgress = 0; int whiteColor = Color.rgb(0xF0, 0xF0, 0xF0); // 外環(huán)畫筆 overRoundPaint = new Paint(); overRoundPaint.setColor(whiteColor); overRoundPaint.setStyle(Paint.Style.STROKE); overRoundPaint.setStrokeWidth(8); overRoundPaint.setAntiAlias(true); // 內(nèi)環(huán)畫筆 roundPaint = new Paint(); roundPaint.setColor(Color.GRAY); roundPaint.setStrokeWidth(30); roundPaint.setStyle(Paint.Style.STROKE); roundPaint.setAntiAlias(true); // 進(jìn)度環(huán)畫筆(除顏色外同于內(nèi)環(huán)) progressRoundPaint = new Paint(); progressRoundPaint.setColor(Color.rgb(0xFF, 0x92, 0x24)); progressRoundPaint.setStrokeWidth(20); progressRoundPaint.setStyle(Paint.Style.STROKE); roundPaint.setAntiAlias(true); // 進(jìn)度文本畫筆 progressTextPaint = new Paint(); progressTextPaint.setColor(whiteColor); progressTextPaint.setStyle(Paint.Style.STROKE); progressTextPaint.setStrokeWidth(0); progressTextPaint.setTextSize(80); progressTextPaint.setTypeface(Typeface.DEFAULT_BOLD); // 文本畫筆 textPaint = new Paint(); textPaint.setColor(whiteColor); textPaint.setStyle(Paint.Style.STROKE); textPaint.setStrokeWidth(0); textPaint.setTextSize(40); textPaint.setTypeface(Typeface.DEFAULT_BOLD); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 取當(dāng)前布局的最短邊作為邊框的長(zhǎng)度 float width = getWidth(); float heigh = getHeight(); boundsWidth = width <= heigh ? width : heigh; // 中心點(diǎn) centerPoint = boundsWidth / 2; // 外環(huán)半徑 overRadius = centerPoint - 20; // 內(nèi)環(huán)半徑 radius = overRadius - 25; // 內(nèi)環(huán)所在區(qū)域(正方形)鎖定為可點(diǎn)擊區(qū)域 clickBoundsLow = centerPoint - radius; clickBoundsHigh = centerPoint + radius; } /** * 啟動(dòng)進(jìn)度動(dòng)畫 */ public void start() { curProgress = 0; if (targetProgress == 0) { targetProgress = 66; } final Timer timer = new Timer(); TimerTask timerTask = new TimerTask() { @Override public void run() { curProgress++; if (curProgress == targetProgress) { timer.cancel(); } postInvalidate(); } }; timer.schedule(timerTask, 0, 20); } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 外環(huán) canvas.drawCircle(centerPoint, centerPoint, overRadius, overRoundPaint); // 內(nèi)環(huán) canvas.drawCircle(centerPoint, centerPoint, radius, roundPaint); // 進(jìn)度環(huán) RectF oval = new RectF(centerPoint - radius, centerPoint - radius, centerPoint + radius, centerPoint + radius); float curArc = 360 * curProgress / maxProgress; canvas.drawArc(oval, 0, curArc, false, progressRoundPaint); // 環(huán)中心進(jìn)度文本 int curPercent = (int) ((curProgress / maxProgress) * 100); float textWidth = progressTextPaint.measureText(curPercent + "%"); canvas.drawText(curPercent + "%", centerPoint - textWidth / 2, centerPoint, progressTextPaint); if (curPercent == 0) { // 暫未評(píng)級(jí) float w = textPaint.measureText("暫未評(píng)級(jí)"); canvas.drawText("暫未評(píng)級(jí)", centerPoint - w / 2, centerPoint + 40, textPaint); } else if (curPercent < targetProgress) { // 評(píng)級(jí)中... float w = textPaint.measureText("評(píng)級(jí)中..."); canvas.drawText("評(píng)級(jí)中...", centerPoint - w / 2, centerPoint + 40, textPaint); } else if (curPercent == targetProgress) { // 評(píng)級(jí)完成 float w = textPaint.measureText("評(píng)級(jí)完成"); canvas.drawText("評(píng)級(jí)完成", centerPoint - w / 2, centerPoint + 40, textPaint); } // 對(duì)外傳遞數(shù)據(jù) if (listener != null) { listener.progressChanged(GradeProgressBar.this, curProgress); } } public synchronized float getMaxProgress() { return maxProgress; } /** * 設(shè)置進(jìn)度的最大值 * * @param max */ public synchronized void setMaxProgress(float max) { if (max < 0) { throw new IllegalArgumentException("max not less than 0"); } this.maxProgress = max; } /** * 獲取進(jìn)度.需要同步 * * @return */ public synchronized float getProgress() { return targetProgress; } /** * 設(shè)置進(jìn)度,此為線程安全控件,由于考慮多線的問(wèn)題,需要同步 刷新界面調(diào)用postInvalidate()能在非UI線程刷新 * * @param progress */ public synchronized void setProgress(float progress) { if (progress < 0) { throw new IllegalArgumentException("progress not less than 0"); } if (progress > maxProgress) { progress = maxProgress; } if (progress <= maxProgress) { this.targetProgress = progress; } } public void setOnProgressChangedListener(onProgressChangedListener listener) { if (listener == null) { this.listener = listener; } } /** * 點(diǎn)擊評(píng)分區(qū)域,進(jìn)行評(píng)分 * * @param event * @return */ @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); if (x > clickBoundsLow && x < clickBoundsHigh && y > clickBoundsLow && y < clickBoundsHigh) { start(); } return super.onTouchEvent(event); } /** * @name onProgressChangedListener * @Descripation 對(duì)外接口,提供當(dāng)前旋轉(zhuǎn)進(jìn)度<br> * 1、<br> * 2、<br> * @author Freedoman * @date 2014-10-29 * @version 1.0 */ public interface onProgressChangedListener { public void progressChanged(GradeProgressBar circleProgressBar, int curProgress); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android progressbar實(shí)現(xiàn)帶底部指示器和文字的進(jìn)度條
- Android Studio實(shí)現(xiàn)進(jìn)度條效果
- Android ProgressBar 模擬進(jìn)度條效果的實(shí)現(xiàn)
- Android自定義分段式進(jìn)度條
- Android自定義圓形進(jìn)度條效果
- Android seekbar實(shí)現(xiàn)可拖動(dòng)進(jìn)度條
- Android自定義View實(shí)現(xiàn)圓形進(jìn)度條
- Android實(shí)現(xiàn)進(jìn)度條(ProgressBar)的功能與用法
- Android自定義圓弧進(jìn)度條加數(shù)字動(dòng)態(tài)變化
- Android自定義控件之圓形進(jìn)度條動(dòng)畫
- Android 進(jìn)度條自動(dòng)前進(jìn)效果的實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)帶有指示器的進(jìn)度條
相關(guān)文章
Android開發(fā)中RecyclerView模仿探探左右滑動(dòng)布局功能
本文給大家分享android開發(fā)中RecyclerView模仿探探左右滑動(dòng)布局功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01Android自定義view實(shí)現(xiàn)雪花特效實(shí)例代碼
實(shí)現(xiàn)雪花的效果其實(shí)也可以通過(guò)自定義View的方式來(lái)實(shí)現(xiàn)的,而且操作上也相對(duì)簡(jiǎn)單一些,下面這篇文章主要給大家介紹了關(guān)于Android自定義view實(shí)現(xiàn)雪花特效的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12Android通過(guò)繼承Binder類實(shí)現(xiàn)多進(jìn)程通信
本篇文章主要介紹了Android通過(guò)繼承Binder類實(shí)現(xiàn)多進(jìn)程通信,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Android高級(jí)動(dòng)畫篇之SVG矢量動(dòng)畫范例
矢量動(dòng)畫即是在計(jì)算機(jī)中使用數(shù)學(xué)方程來(lái)描述屏幕上復(fù)雜的曲線,利用圖形的抽象運(yùn)動(dòng)特征來(lái)記錄變化的畫面信息的動(dòng)畫,本篇帶你了解在Android中的矢量動(dòng)畫2021-11-11Android4.4開發(fā)之電池低電量告警提示原理與實(shí)現(xiàn)方法分析
這篇文章主要介紹了Android4.4開發(fā)之電池低電量告警提示原理與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android4.4電池電量警告的原理及相關(guān)操作技巧,需要的朋友可以參考下2017-09-09詳談Android動(dòng)畫效果translate、scale、alpha、rotate
下面小編就為大家?guī)?lái)一篇詳談Android動(dòng)畫效果translate、scale、alpha、rotate。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01Kotlin遍歷集合導(dǎo)致并發(fā)修改異常的原因和解決方法
這篇文章主要介紹了Kotlin遍歷集合導(dǎo)致并發(fā)修改異常的原因和解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03輕松實(shí)現(xiàn)Android自定義九宮格圖案解鎖
這篇文章主要幫助大家輕松實(shí)現(xiàn)Android九宮格圖案解鎖功能,可以將圖案轉(zhuǎn)化成數(shù)字密碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android徹底清除APP數(shù)據(jù)的兩種方案總結(jié)
大家在用Android手機(jī)的時(shí)候肯定都遇到過(guò)內(nèi)存剩余空間越來(lái)越小的情況,所以下面這篇文章主要給大家介紹了關(guān)于Android徹底清除APP數(shù)據(jù)的兩種方案,需要的朋友可以參考下2021-11-11