欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

android自定义等级评分圆形进度条

程序员文章站 2024-01-15 21:18:28
本文实例为大家分享了android评分圆形进度条的具体代码,供大家参考,具体内容如下一、测试截图二、实现原理package com.freedomanlib; import java.util.tim...

本文实例为大家分享了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, 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);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: android 进度条