안 드 로 이 드 사용자 정의 등급 평가 원형 진도 바
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);
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.