热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android自定义View仿华为圆形加载进度条

Android自定义View仿华为圆形加载进度条-View仿华为圆形加载进度条效果图实现思路可以看出该View可分为三个部分来实现最外围的圆,该部分需要区分进度圆和底部的刻度圆,进

View仿华为圆形加载进度条效果图

实现思路

可以看出该View可分为三个部分来实现

最外围的圆,该部分需要区分进度圆和底部的刻度圆,进度部分的刻度需要和底色刻度区分开来

中间显示的文字进度,需要让文字在View中居中显示

旋转的小圆点,小圆点需要模拟小球下落运动时的加速度效果,开始下落的时候慢,到最底部时最快,上来时速度再逐渐减慢

具体实现

先具体细分讲解,博客最后面给出全部源码

(1)首先为View创建自定义的xml属性
在工程的values目录下新建attrs.xml文件


 
 
  
  
  
  
  
 

各个属性的作用:

indexColor:进度圆的颜色
baseColor:刻度圆底色
dotColor:小圆点颜色
textSize:文字大小
textColor:文字颜色

(2)新建CircleLoadingView类继承View类,重写它的三个构造方法,获取用户设置的属性,同时指定默认值

public CircleLoadingView(Context context) {
  this(context, null);
 }

 public CircleLoadingView(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
 }

 public CircleLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  // 获取用户配置属性
  TypedArray tya = context.obtainStyledAttributes(attrs, R.styleable.CircleLoading);
  baseColor = tya.getColor(R.styleable.CircleLoading_baseColor, Color.LTGRAY);
  indexColor = tya.getColor(R.styleable.CircleLoading_indexColor, Color.BLUE);
  textColor = tya.getColor(R.styleable.CircleLoading_textColor, Color.BLUE);
  dotColor = tya.getColor(R.styleable.CircleLoading_dotColor, Color.RED);
  textSize = tya.getDimensionPixelSize(R.styleable.CircleLoading_textSize, 36);
  tya.recycle();

  initUI();
 }

我们从View绘制的第一步开始

(3)测量onMeasure,首先需要测量出View的宽和高,并指定View在wrap_content时的最小范围,对于View绘制流程还不熟悉的同学,可以先去了解下具体的绘制流程

浅谈Android View绘制三大流程探索及常见问题

重写onMeasure方法,其中我们要考虑当View的宽高被指定为wrap_content时的情况,如果我们不对wrap_content的情况进行处理,那么当使用者指定View的宽高为wrap_content时将无法正常显示出View

@Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);

  int myWidthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
  int myWidthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
  int myHeightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
  int myHeightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

  // 获取宽
  if (myWidthSpecMode == MeasureSpec.EXACTLY) {
   // match_parent/精确值
   mWidth = myWidthSpecSize;
  } else {
   // wrap_content
   mWidth = DensityUtil.dip2px(mContext, 120);
  }

  // 获取高
  if (myHeightSpecMode == MeasureSpec.EXACTLY) {
   // match_parent/精确值
   mHeight = myHeightSpecSize;
  } else {
   // wrap_content
   mHeight = DensityUtil.dip2px(mContext, 120);
  }

  // 设置该view的宽高
  setMeasuredDimension(mWidth, mHeight);
 }

MeasureSpec的状态分为三种EXACTLY、AT_MOST、UNSPECIFIED,这里只要单独指定非精确值EXACTLY之外的情况就好了。

本文中使用到的DensityUtil类,是为了将dp转换为px来使用,以便适配不同的屏幕显示效果

public static int dip2px(Context context, float dpValue) {
  final float scale = context.getResources().getDisplayMetrics().density;
  return (int) (dpValue * scale + 0.5f);
 }

(4)重写onDraw,绘制需要显示的内容

因为做的是单纯的View而不是ViewGroup,内部没有子控件需要确定位置,所以可直接跳过onLayout方法,直接开始对View进行绘制
分为三个部分绘制,绘制刻度圆,绘制文字值,绘制旋转小圆点

@Override
 protected void onDraw(Canvas canvas) {
  drawArcScale(canvas);
  drawTextValue(canvas);
  drawRotateDot(canvas);
 }

绘制刻度圆

先画一个小竖线,通过canvas.rotate()方法每次旋转3.6度(总共360度,用100/360=3.6)得到一个刻度为100的圆,然后通过progress参数,得到要显示的进度数,并把小于progress的刻度变成进度圆的颜色

/**
  * 画刻度
  */
 private void drawArcScale(Canvas canvas) {
  canvas.save();

  for (int i = 0; i <100; i++) {
   if (progress > i) {
    mScalePaint.setColor(indexColor);
   } else {
    mScalePaint.setColor(baseColor);
   }
   canvas.drawLine(mWidth / 2, 0, mHeight / 2, DensityUtil.dip2px(mContext, 10), mScalePaint);
   // 旋转的度数 = 100 / 360
   canvas.rotate(3.6f, mWidth / 2, mHeight / 2);
  }

  canvas.restore();
 }

绘制中间文字

文字绘制的坐标是以文字的左下角开始绘制的,所以需要先通过把文字装载到一个矩形Rect,通过画笔的getTextBounds方法取得字符串的长度和宽度,通过动态计算,来使文字居中显示

/**
  * 画内部数值
  */
 private void drawTextValue(Canvas canvas) {
  canvas.save();

  String showValue = String.valueOf(progress);
  Rect textBound = new Rect();
  mTextPaint.getTextBounds(showValue, 0, showValue.length(), textBound); // 获取文字的矩形范围
  float textWidth = textBound.right - textBound.left; // 获得文字宽
  float textHeight = textBound.bottom - textBound.top; // 获得文字高
  canvas.drawText(showValue, mWidth / 2 - textWidth / 2, mHeight / 2 + textHeight / 2, mTextPaint);

  canvas.restore();
 }

绘制旋转小圆点

这个小圆点就是简单的绘制一个填充的圆形就好

/**
  * 画旋转小圆点
  */
 private void drawRotateDot(final Canvas canvas) {
  canvas.save();

  canvas.rotate(mDotProgress * 3.6f, mWidth / 2, mHeight / 2);
  canvas.drawCircle(mWidth / 2, DensityUtil.dip2px(mContext, 10) + DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 3), mDotPaint);

  canvas.restore();
 }

让它自己动起来可以通过两种方式,一种是开一个线程,在线程中改变mDotProgress的数值,并通过postInvalidate方法跨线程刷新View的显示效果

new Thread() {
   @Override
   public void run() {
    while (true) {
     mDotProgress++;
     if (mDotProgress == 100) {
      mDotProgress = 0;
     }
     postInvalidate();
     try {
      Thread.sleep(50);
     } catch (InterruptedException e) {
      e.printStackTrace();
     }
    }
   }
  }.start();

开线程的方式不推荐使用,这是没必要的开销,而且线程不好控制,要实现让小圆点在运行过程中开始和结束时慢,运动到中间时加快这种效果不好实现,所以最好的方式是使用属性动画,需要让小圆点动起来时,调用以下方法就好了

/**
  * 启动小圆点旋转动画
  */
 public void startDotAnimator() {
  animator = ValueAnimator.ofFloat(0, 100);
  animator.setDuration(1500);
  animator.setRepeatCount(ValueAnimator.INFINITE);
  animator.setRepeatMode(ValueAnimator.RESTART);
  animator.setInterpolator(new AccelerateDecelerateInterpolator());
  animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
   @Override
   public void onAnimationUpdate(ValueAnimator animation) {
    // 设置小圆点的进度,并通知界面重绘
    mDotProgress = (Float) animation.getAnimatedValue();
    invalidate();
   }
  });
  animator.start();
 }

在属性动画中可以通过setInterpolator方法指定不同的插值器,这里要模拟小球掉下来的重力效果,所以需要使用AccelerateDecelerateInterpolator插值类,该类的效果就是在动画开始时和结束时变慢,中间加快

(5)设置当前进度值

对外提供一个方法,用来更新当前圆的进度

/**
  * 设置进度
  */
 public void setProgress(int progress) {
  this.progress = progress;
  invalidate();
 }

通过外部调用setProgress方法就可以跟更新当前圆的进度了

源码

/**
 * 仿华为圆形加载进度条
 * Created by zhuwentao on 2017-08-19.
 */
public class CircleLoadingView extends View {

 private Context mContext;

 // 刻度画笔
 private Paint mScalePaint;

 // 小原点画笔
 private Paint mDotPaint;

 // 文字画笔
 private Paint mTextPaint;

 // 当前进度
 private int progress = 0;

 /**
  * 小圆点的当前进度
  */
 public float mDotProgress;

 // View宽
 private int mWidth;

 // View高
 private int mHeight;

 private int indexColor;

 private int baseColor;

 private int dotColor;

 private int textSize;

 private int textColor;

 private ValueAnimator animator;

 public CircleLoadingView(Context context) {
  this(context, null);
 }

 public CircleLoadingView(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
 }

 public CircleLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  // 获取用户配置属性
  TypedArray tya = context.obtainStyledAttributes(attrs, R.styleable.CircleLoading);
  baseColor = tya.getColor(R.styleable.CircleLoading_baseColor, Color.LTGRAY);
  indexColor = tya.getColor(R.styleable.CircleLoading_indexColor, Color.BLUE);
  textColor = tya.getColor(R.styleable.CircleLoading_textColor, Color.BLUE);
  dotColor = tya.getColor(R.styleable.CircleLoading_dotColor, Color.RED);
  textSize = tya.getDimensionPixelSize(R.styleable.CircleLoading_textSize, 36);
  tya.recycle();

  initUI();
 }

 private void initUI() {
  mCOntext= getContext();

  // 刻度画笔
  mScalePaint = new Paint();
  mScalePaint.setAntiAlias(true);
  mScalePaint.setStrokeWidth(DensityUtil.dip2px(mContext, 1));
  mScalePaint.setStrokeCap(Paint.Cap.ROUND);
  mScalePaint.setColor(baseColor);
  mScalePaint.setStyle(Paint.Style.STROKE);

  // 小圆点画笔
  mDotPaint = new Paint();
  mDotPaint.setAntiAlias(true);
  mDotPaint.setColor(dotColor);
  mDotPaint.setStrokeWidth(DensityUtil.dip2px(mContext, 1));
  mDotPaint.setStyle(Paint.Style.FILL);

  // 文字画笔
  mTextPaint = new Paint();
  mTextPaint.setAntiAlias(true);
  mTextPaint.setColor(textColor);
  mTextPaint.setTextSize(textSize);
  mTextPaint.setStrokeWidth(DensityUtil.dip2px(mContext, 1));
  mTextPaint.setStyle(Paint.Style.FILL);
 }

 @Override
 protected void onDraw(Canvas canvas) {
  drawArcScale(canvas);
  drawTextValue(canvas);
  drawRotateDot(canvas);
 }

 /**
  * 画刻度
  */
 private void drawArcScale(Canvas canvas) {
  canvas.save();

  for (int i = 0; i <100; i++) {
   if (progress > i) {
    mScalePaint.setColor(indexColor);
   } else {
    mScalePaint.setColor(baseColor);
   }
   canvas.drawLine(mWidth / 2, 0, mHeight / 2, DensityUtil.dip2px(mContext, 10), mScalePaint);
   // 旋转的度数 = 100 / 360
   canvas.rotate(3.6f, mWidth / 2, mHeight / 2);
  }

  canvas.restore();
 }

 /**
  * 画内部数值
  */
 private void drawTextValue(Canvas canvas) {
  canvas.save();

  String showValue = String.valueOf(progress);
  Rect textBound = new Rect();
  mTextPaint.getTextBounds(showValue, 0, showValue.length(), textBound); // 获取文字的矩形范围
  float textWidth = textBound.right - textBound.left; // 获得文字宽
  float textHeight = textBound.bottom - textBound.top; // 获得文字高
  canvas.drawText(showValue, mWidth / 2 - textWidth / 2, mHeight / 2 + textHeight / 2, mTextPaint);

  canvas.restore();
 }

 /**
  * 画旋转小圆点
  */
 private void drawRotateDot(final Canvas canvas) {
  canvas.save();

  canvas.rotate(mDotProgress * 3.6f, mWidth / 2, mHeight / 2);
  canvas.drawCircle(mWidth / 2, DensityUtil.dip2px(mContext, 10) + DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 3), mDotPaint);

  canvas.restore();
 }

 /**
  * 启动小圆点旋转动画
  */
 public void startDotAnimator() {
  animator = ValueAnimator.ofFloat(0, 100);
  animator.setDuration(1500);
  animator.setRepeatCount(ValueAnimator.INFINITE);
  animator.setRepeatMode(ValueAnimator.RESTART);
  animator.setInterpolator(new AccelerateDecelerateInterpolator());
  animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
   @Override
   public void onAnimationUpdate(ValueAnimator animation) {
    // 设置小圆点的进度,并通知界面重绘
    mDotProgress = (Float) animation.getAnimatedValue();
    invalidate();
   }
  });
  animator.start();
 }

 /**
  * 设置进度
  */
 public void setProgress(int progress) {
  this.progress = progress;
  invalidate();
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);

  int myWidthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
  int myWidthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
  int myHeightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
  int myHeightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

  // 获取宽
  if (myWidthSpecMode == MeasureSpec.EXACTLY) {
   // match_parent/精确值
   mWidth = myWidthSpecSize;
  } else {
   // wrap_content
   mWidth = DensityUtil.dip2px(mContext, 120);
  }

  // 获取高
  if (myHeightSpecMode == MeasureSpec.EXACTLY) {
   // match_parent/精确值
   mHeight = myHeightSpecSize;
  } else {
   // wrap_content
   mHeight = DensityUtil.dip2px(mContext, 120);
  }

  // 设置该view的宽高
  setMeasuredDimension(mWidth, mHeight);
 }
}

总结

在的onDraw方法中需要避免频繁的new对象,所以把一些如初始化画笔Paint的方法放到了最前面的构造方法中进行。

在分多个模块绘制时,应该使用canvas.save()和canvas.restore()的组合,来避免不同模块绘制时的相互干扰,在这两个方法中绘制相当于PS中的图层概念,上一个图层进行的修改不会影响到下一个图层的显示效果。

在需要显示动画效果的地方使用属性动画来处理,可自定义的效果强,在系统提供的插值器类不够用的情况下,我么还可通过继承Animation类,重写它的applyTransformation方法来处理各种复杂的动画效果。


推荐阅读
  • 本文介绍了在MFC下利用C++和MFC的特性动态创建窗口的方法,包括继承现有的MFC类并加以改造、插入工具栏和状态栏对象的声明等。同时还提到了窗口销毁的处理方法。本文详细介绍了实现方法并给出了相关注意事项。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文讲述了如何通过代码在Android中更改Recycler视图项的背景颜色。通过在onBindViewHolder方法中设置条件判断,可以实现根据条件改变背景颜色的效果。同时,还介绍了如何修改底部边框颜色以及提供了RecyclerView Fragment layout.xml和项目布局文件的示例代码。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
author-avatar
Mr_XieZhiQ
无表面兄弟,不编程!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有