热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

Android自定义控件实现带文本与数字的圆形进度条

这篇文章主要为大家详细介绍了Android自定义控件实现带文本与数字的圆形进度条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android实现圆形进度条的具体代码,供大家参考,具体内容如下

实现的效果图如下所示:

第一步:绘制下方有缺口的空心圆,称为外围大弧吧

anvas.clipRect(0, 0, mWidth, mHeight / 2 + radius - textHeight * 3 / 4);

第二步:计算绘制圆弧进度条时的起始角度,设置为外围大弧的左端点为进度值得起点,扫过的角度所占外围大弧的百分比就是进度值

第三步:绘制数字、文字、百分号

第四步:使用Handler Runnable 和DecelerateInterpolator是进度条和数字动起来

测试代码:

final CustomCircleBar circle=(CustomCircleBar)findViewById(R.id.win_home);
circle.setPercent(10);
circle.setCustomText("呵呵");
circle.setProgessColor(getResources().getColor(R.color.blue));
final Random random=new Random();
circle.setOnClickListener(new View.OnClickListener(){
 @Override
 public void onClick(View v){
 circle.setPercent(random.nextInt(100));
 }
});

完成代码如下:

public class CustomCircleBar extends View {
 private Context context;
 /**
 * 进度值
 */
 private int percent;
 /**
 * 颜色值
 */
 private int mProgessColor;
 /**
 * 下边的文字名称
 */
 private String mCustomText;
 /**
 * 外圈圆环的画笔
 */
 private Paint paintBar = new Paint();
 /**
 * 下边文字的画笔
 */
 private Paint paintText = new Paint();
 /**
 * 动态获取属性值
 */
 private TypedValue typedValue;
 /**
 * 先加速后减速
 */
 DecelerateInterpolator mDecelerateInterpolator = new DecelerateInterpolator();
 /**
 * 动画持续时间
 */
 private int duration = 10;
 private int curTime = 0;
 public CustomCircleBar(Context context) {
 super(context);
 this.cOntext=context;
 init();
 }
 
 public CustomCircleBar(Context context, AttributeSet attrs) {
 super(context, attrs);
 this.cOntext=context;
 init();
 }
 
 public CustomCircleBar(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 this.cOntext=context;
 init();
 }
 
 
 
 public void setPercent(int percent) {
 this.percent = percent;
 /*isShown():Returns the visibility of this view and all of its ancestors*/
 if (isShown()) {
  /**
  * 设置进度后重新开始一次动画
  */
  curTime=0;
  this.invalidate();
 }
 }
 
 public void setProgessColor(int mProgessColor) {
 this.mProgessColor = mProgessColor;
 if (isShown()) {
  this.invalidate();
 }
 }
 
 
 public void setCustomText(String mCustomText) {
 this.mCustomText = mCustomText;
 }
 
 private Handler mHandler = new Handler();
 private Runnable mAnimation = new Runnable() {
 @Override
 public void run() {
  if (curTime 

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


推荐阅读
author-avatar
老猫
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有