안 드 로 이 드 사용자 정의 등급 평가 원형 진도 바

8566 단어 android진도 표
본 논문 의 사례 는 안 드 로 이 드 가 원형 진도 바 를 평가 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
1.테스트 캡 처

2.실현 원리

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, attrs, 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, 0, 20);
 }
 
 @SuppressLint("DrawAllocation")
 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 
 //   
 canvas.drawCircle(centerPoint, centerPoint, overRadius, overRoundPaint);
 //   
 canvas.drawCircle(centerPoint, centerPoint, radius, roundPaint);
 
 //    
 RectF oval = new RectF(centerPoint - radius, centerPoint - radius,
 centerPoint + radius, centerPoint + radius);
 float curArc = 360 * curProgress / maxProgress;
 canvas.drawArc(oval, 0, curArc, false, progressRoundPaint);
 
 //        
 int curPercent = (int) ((curProgress / maxProgress) * 100);
 float textWidth = progressTextPaint.measureText(curPercent + "%");
 canvas.drawText(curPercent + "%", centerPoint - textWidth / 2,
 centerPoint, 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("   ...", centerPoint - w / 2, centerPoint + 40,
 textPaint);
 } else if (curPercent == targetProgress) {
 //     
 float w = textPaint.measureText("    ");
 canvas.drawText("    ", centerPoint - w / 2, centerPoint + 40,
 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);
 }
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기