当前位置: 移动技术网 > IT编程>移动开发>Android > Android自定义圆形进度条

Android自定义圆形进度条

2019年07月24日  | 移动技术网IT编程  | 我要评论

王潮歌 开讲啦,smart five,动画图片库

今天小编来手写一个自定义圆形进度条:先看效果:

首先我们在attrs属性文件中增加几个自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources>

 <declare-styleable name="customprogressbar">
  <!-- 圆形进度条进度显示的颜色 -->
  <attr name="roundprogresscolor" format="color"></attr>
  <!-- 外圈圆的颜色 -->
  <attr name="roundcolor" format="color"></attr>
  <!-- 圆的总宽度 -->
  <attr name="roundwidth" format="dimension"></attr>
  <!-- 字体显示的大小 -->
  <attr name="textsize" format="dimension"></attr>
  <!-- 字体显示的颜色 -->
  <attr name="textcolor" format="color"></attr>
  <!-- 进度的最大值 -->
  <attr name="max" format="integer"></attr>
  <!-- 是否显示文字 -->
  <attr name="textshow" format="boolean"></attr>
 </declare-styleable>

</resources>

上我们自定义类的实现代码:

package xxx.xxx.xxx;

import android.content.context;
import android.content.res.typedarray;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.graphics.rectf;
import android.graphics.typeface;
import android.support.annotation.nullable;
import android.util.attributeset;
import android.view.view;

import test.dn.com.dn_test.r;

/**
 * created by administrator on 2017/5/16 0016.
 */

public class circleprogressbar extends view {

 private int max; //最大值
 private int roundcolor; //圆形进度条的颜色
 private int roundprogresscolor;//圆形进度条进度的颜色
 private int textcolor;  //字体的颜色
 private float textsize;  //字体的大小
 private float roundwidth; //圆的宽度
 private boolean textshow; //是否显示圆
 private int progress; //当前进度
 private paint mpaint; //画笔
 public static final int stroke = 0;
 public static final int fill = 1;

 public circleprogressbar(context context, @nullable attributeset attrs) {
  super(context, attrs);
  //初始化一只笔
  mpaint = new paint();
  //获取xml当中设置的属性,如果没有设置,则设置一个默认值
  typedarray typedarray = context.obtainstyledattributes(attrs , r.styleable.customprogressbar);
  max = typedarray.getinteger(r.styleable.customprogressbar_max , 100);
  roundcolor = typedarray.getcolor(r.styleable.customprogressbar_roundcolor, color.red);
  roundprogresscolor = typedarray.getcolor(r.styleable.customprogressbar_roundprogresscolor , color.blue);
  textcolor = typedarray.getcolor(r.styleable.customprogressbar_textcolor , color.green);
  textsize = typedarray.getdimension(r.styleable.customprogressbar_textsize , 55);
  roundwidth = typedarray.getdimension(r.styleable.customprogressbar_roundwidth , 10);
  textshow = typedarray.getboolean(r.styleable.customprogressbar_textshow , true);

 }

 @override
 protected void ondraw(canvas canvas) {
  super.ondraw(canvas);
  //画背景圆环
  int center = getwidth() / 2;
  //设置半径
  float radius = center - roundwidth / 2;
  //设置圆圈的颜色
  mpaint.setcolor(roundcolor);
  mpaint.setstyle(paint.style.stroke);
  mpaint.setstrokewidth(roundwidth);//圆环的宽度
  mpaint.setantialias(true);//设置抗锯齿

  //画外圈
  canvas.drawcircle(center , center ,radius , mpaint);

  //画进度百分比
  mpaint.setcolor(textcolor);
  mpaint.setstrokewidth(0);
  //设置字体大小
  mpaint.settextsize(textsize);
  mpaint.settypeface(typeface.default);
  //设置笔帽
  mpaint.setstrokecap(paint.cap.round);
  //设置文字的摆放方式为居中
  mpaint.settextalign(paint.align.center);
  //获取当前进度的值
  int percent = (int) (progress / (float)max * 100);
  string strpercent = percent + "%";
  //获取画笔的文字属性,总共有bottom , top , leading , ascent , descent 这个以后会详细讲解
  paint.fontmetricsint fm = mpaint.getfontmetricsint();
  if(percent != 0){
   canvas.drawtext(strpercent , getwidth() / 2 ,
     getwidth() / 2 + (fm.bottom - fm.top) / 2 - fm.bottom, mpaint);
  }
  //画圆弧
  rectf oval = new rectf(center - radius , center - radius ,center + radius , center + radius);
  mpaint.setcolor(roundprogresscolor);
  mpaint.setstrokewidth(roundwidth);
  mpaint.setstyle(paint.style.stroke);
  //设置笔帽
  mpaint.setstrokecap(paint.cap.round);
  //话进度
  canvas.drawarc(oval , 0 , 360 * progress / max , false , mpaint);
 }

 public void setprogress(int progress){
  if(progress < 0){
   throw new illegalargumentexception("进度progress不能小于0");
  }
  if(progress > max){
   progress = max;
  }
  if(progress <= max){
   this.progress = progress;
   postinvalidate();
  }

 }
}

在我们的xml中设置控件:

 <xxx.xxx.circleprogressbar
  android:id="@+id/progressbar"
  android:layout_width="100dp"
  android:layout_height="100dp"
  app:roundprogresscolor="#ff00ff"
  app:textcolor="#666666"
  app:textsize="20dp"
  app:roundwidth="15dp"
  />

activity功能实现代码:

mprogressbar = (circleprogressbar) findviewbyid(r.id.progressbar);
  mprogressbar.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    //模拟http请求
    new thread(new runnable() {
     @override
     public void run() {
      while (progress <= 100){
       progress += 2;
       mprogressbar.setprogress(progress);
       //模拟网络请求,每隔100毫秒增加一个进度
       systemclock.sleep(100);
      }
     }
    }).start();
   }
  });

完结!

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网