1. 程式人生 > 程式設計 >android自定義等級評分圓形進度條

android自定義等級評分圓形進度條

本文例項為大家分享了android評分圓形進度條的具體程式碼,供大家參考,具體內容如下

一、測試截圖

android自定義等級評分圓形進度條

二、實現原理

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 自定義等級評分圓形進度條,用於裝置資料統計頁面一鍵評分<br>
 *  1、初始化邊界寬度、中心座標和外環、內環半徑,各種畫筆。<br>
 *  2、預設最大進度為100,目標進度由使用者來指定。<br>
 *  3、鎖定一個內圓環為可點選區域。 <br>
 *  4、點選元件時,呼叫start()方法啟動計時器,重繪介面。<br>
 * @author Freedoman
 * @date 2014-10-29
 * @version 1.0
 */
public class GradeProgressBar extends View {
 
 private static final String TAG = "CircleProgressBar";
 
 /**
 * 邊界寬度、中心座標和外環、內環半徑
 */
 private float boundsWidth;
 private float centerPoint;
 private float overRadius;
 private float radius;
 
 /**
 * 最大進度、當前進度、是否顯示進度文字
 */
 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;
 
 /**
 * 可點選區域的邊界
 */
 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,defStyleAttr);
 this.initialize();
 }
 
 /**
 * 初始化
 */
 private void initialize() {
 
 curProgress = 0;
 int whiteColor = Color.rgb(0xF0,0xF0,0xF0);
 
 // 外環畫筆
 overRoundPaint = new Paint();
 overRoundPaint.setColor(whiteColor);
 overRoundPaint.setStyle(Paint.Style.STROKE);
 overRoundPaint.setStrokeWidth(8);
 overRoundPaint.setAntiAlias(true);
 
 // 內環畫筆
 roundPaint = new Paint();
 roundPaint.setColor(Color.GRAY);
 roundPaint.setStrokeWidth(30);
 roundPaint.setStyle(Paint.Style.STROKE);
 roundPaint.setAntiAlias(true);
 
 // 進度環畫筆(除顏色外同於內環)
 progressRoundPaint = new Paint();
 progressRoundPaint.setColor(Color.rgb(0xFF,0x92,0x24));
 progressRoundPaint.setStrokeWidth(20);
 progressRoundPaint.setStyle(Paint.Style.STROKE);
 roundPaint.setAntiAlias(true);
 
 // 進度文字畫筆
 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);
 
 // 取當前佈局的最短邊作為邊框的長度
 float width = getWidth();
 float heigh = getHeight();
 boundsWidth = width <= heigh ? width : heigh;
 
 // 中心點
 centerPoint = boundsWidth / 2;
 // 外環半徑
 overRadius = centerPoint - 20;
 // 內環半徑
 radius = overRadius - 25;
 
 // 內環所在區域(正方形)鎖定為可點選區域
 clickBoundsLow = centerPoint - radius;
 clickBoundsHigh = centerPoint + radius;
 }
 
 /**
 * 啟動進度動畫
 */
 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,20);
 }
 
 @SuppressLint("DrawAllocation")
 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 
 // 外環
 canvas.drawCircle(centerPoint,centerPoint,overRadius,overRoundPaint);
 // 內環
 canvas.drawCircle(centerPoint,radius,roundPaint);
 
 // 進度環
 RectF oval = new RectF(centerPoint - radius,centerPoint - radius,centerPoint + radius,centerPoint + radius);
 float curArc = 360 * curProgress / maxProgress;
 canvas.drawArc(oval,curArc,false,progressRoundPaint);
 
 // 環中心進度文字
 int curPercent = (int) ((curProgress / maxProgress) * 100);
 float textWidth = progressTextPaint.measureText(curPercent + "%");
 canvas.drawText(curPercent + "%",centerPoint - textWidth / 2,progressTextPaint);
 
 if (curPercent == 0) {
 // 暫未評級
 float w = textPaint.measureText("暫未評級");
 canvas.drawText("暫未評級",centerPoint - w / 2,centerPoint + 40,textPaint);
 } else if (curPercent < targetProgress) {
 // 評級中...
 float w = textPaint.measureText("評級中...");
 canvas.drawText("評級中...",textPaint);
 } else if (curPercent == targetProgress) {
 // 評級完成
 float w = textPaint.measureText("評級完成");
 canvas.drawText("評級完成",textPaint);
 }
 
 // 對外傳遞資料
 if (listener != null) {
 listener.progressChanged(GradeProgressBar.this,curProgress);
 }
 }
 
 public synchronized float getMaxProgress() {
 return maxProgress;
 }
 
 /**
 * 設定進度的最大值
 * 
 * @param max
 */
 public synchronized void setMaxProgress(float max) {
 if (max < 0) {
 throw new IllegalArgumentException("max not less than 0");
 }
 this.maxProgress = max;
 }
 
 /**
 * 獲取進度.需要同步
 * 
 * @return
 */
 public synchronized float getProgress() {
 return targetProgress;
 }
 
 /**
 * 設定進度,此為執行緒安全控制元件,由於考慮多線的問題,需要同步 重新整理介面呼叫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;
 }
 }
 
 /**
 * 點選評分割槽域,進行評分
 * 
 * @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 對外介面,提供當前旋轉進度<br>
 *  1、<br>
 *  2、<br>
 * @author Freedoman
 * @date 2014-10-29
 * @version 1.0
 */
 public interface onProgressChangedListener {
 public void progressChanged(GradeProgressBar circleProgressBar,int curProgress);
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。