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

Android绘制圆形百分比加载圈效果

程序员文章站 2023-12-01 17:47:10
先看一组加载效果图,有点粉粉的加载圈: 自定义这样的圆形加载圈还是比较简单的,主要是用到canvans的绘制文本,绘制圆和绘制圆弧的api: /**...

先看一组加载效果图,有点粉粉的加载圈:

Android绘制圆形百分比加载圈效果Android绘制圆形百分比加载圈效果

自定义这样的圆形加载圈还是比较简单的,主要是用到canvans的绘制文本,绘制圆和绘制圆弧的api:

/**
 * 绘制圆
 * @param cx   圆心x坐标
 * @param cy   圆心y坐标
 * @param radius 圆的半径
 * @param paint 画笔
 */
 public void drawcircle(float cx, float cy, float radius, @nonnull paint paint) {
 ...
 }


/**
 * 绘制圆弧
 * @param oval    决定圆弧范围的矩形区域
 * @param startangle 开始角度
 * @param sweepangle 绘制的角度
 * @param usecenter 是否需要连接圆心,true连接,false不连接
 * @param paint   画笔
 */
 public void drawarc(rectf oval, float startangle, float sweepangle, boolean usecenter,paint paint) {
  ...
 }


 /**
  * 绘制文本
  * @param text 需要绘制的字符串
  * @param x   绘制文本起点x坐标,注意文本比较特殊,它的起点是左下角的x坐标
  * @param y   绘制文本起点y坐标,同样是左下角的y坐标
  * @param paint 画笔
  */
 public void drawtext(string text, float x, float y, paint paint) {
  ...
 }

开始绘图前需要考虑的以下几点:

1.获取控件的宽和高,这个是决定圆的半径大小的,半径大小等于宽高的最小值的1/2,为什么是最小值呢?因为这样就不会受布局文件中宽高属性不一样的影响,当然我们自己在使用的时候肯定是宽高都是会写成一样的,这样就刚好是一个正方形,绘制出来的圆就刚好在该正方形区域内.做了这样的处理,其他人在用的时候就不用当心圆会不会超出控件范围的情况了.

2.确定圆心的坐标,有了半径和圆心坐标就可以确定一个圆了,布局中的控件区域其实都是一个矩形区域,如果想要绘制出来的圆刚好处于控件的矩形区域内并且和矩形的最短的那条边相切,那么圆心坐标的就是该矩形宽高的1/2,即刚好位于矩形区域的中心点.

3.绘制圆弧,注意这里的圆弧指的是进度圈,看上面的示例图是有2种样式,分别是实心的加载圈和空心加载圈,这个其实就是paint的样式决定,如果是实心圆,paint设置为paint.style.fill(填充模式),同时drawarc的usecenter设置为true;如果是空心圆则paint设置为paint.style.stroke(线性模式),同时drawarc的usecenter设置为false即可.值得一提的是绘制空心圆的时候还需要考虑圆弧的宽度,宽度有多大将决定进度圈的厚度.因此在定义空心圆的矩形区域的时候需要减去进度圈的厚度,否则画出来的进度圈会超出控件的区域.

4.绘制文本,需要定位起始点,文本的起始点比较特殊,它是以左下角为起始点的,起点x坐标=圆心x坐标-文本宽度1/2;起始点y坐标=圆心y坐标+文本高度1/2;至于文本的宽高获取可以通过paint的gettextbounds()方法获取,具体等下看代码.

ok,直接上代码,注释已经很详细了.

圆形加载圈

public class circleprogressview extends view {
  private int width;//控件宽度
  private int height;//控件高
  private float radius;//半径
  private float pointx;//圆心x坐标
  private float pointy;//圆心y坐标
  private int circlebackgroundcolor;//背景颜色
  private int circlebackgroundalpha; //背景透明度
  private int circleringcolor;//进度颜色
  private int progresstextcolor;//进度文本的颜色
  private int progresstextsize;//进度文本的字体大小
  private int circleringwidth; //进度的宽度
  private paint mpaint;
  private int progress;//进度值
  private boolean isringstyle;//是否是空心的圆环进度样式

  public circleprogressview(context context) {
    this(context, null);
  }

  public circleprogressview(context context, attributeset attrs) {
    this(context, attrs, 0);
  }

  public circleprogressview(context context, attributeset attrs, int defstyleattr) {
    super(context, attrs, defstyleattr);
    circlebackgroundcolor = color.white;
    circleringcolor = color.parsecolor("#3a91ff");
    progresstextcolor = color.black;
    circlebackgroundalpha = 128;
    progresstextsize = 32;
    circleringwidth = 10;
    mpaint = new paint();
    mpaint.setantialias(true);
    mpaint.setstyle(paint.style.fill);
  }

  @override
  protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
    super.onmeasure(widthmeasurespec, heightmeasurespec);
    width = getmeasuredwidth();
    height = getmeasuredheight();
    radius = math.min(width, height) / 2.0f;
    pointx = width / 2.0f;
    pointy = height / 2.0f;
  }

  @override
  protected void ondraw(canvas canvas) {
    drawbackground(canvas);
    drawcirclering(canvas);
    drawprogresstext(canvas);
  }

  /**
   * 绘制背景色
   *
   * @param canvas
   */
  private void drawbackground(canvas canvas) {
    mpaint.setcolor(circlebackgroundcolor);
    mpaint.setalpha(circlebackgroundalpha);
    canvas.drawcircle(pointx, pointy, radius, mpaint);
  }

  /**
   * 绘制圆环
   *
   * @param canvas
   */
  private void drawcirclering(canvas canvas) {
    mpaint.setcolor(circleringcolor);
    rectf oval = new rectf();
    if (isringstyle) {
      mpaint.setstyle(paint.style.stroke);
      mpaint.setstrokewidth(circleringwidth);
      mpaint.setstrokecap(paint.cap.round);
      //注意:定义圆环的矩形区域是需要考虑圆环的宽度
      oval.left = circleringwidth / 2.0f;
      oval.top = height / 2.0f - radius + circleringwidth / 2.0f;
      oval.right = 2 * radius - circleringwidth / 2.0f;
      oval.bottom = height / 2.0f + radius - circleringwidth / 2.0f;
      canvas.drawarc(oval, 0, 360 * progress / 100, false, mpaint);
    } else {
      mpaint.setstyle(paint.style.fill);
      oval.left = 0;
      oval.top = height / 2.0f - radius;
      oval.right = 2 * radius;
      oval.bottom = height / 2.0f + radius;
      canvas.drawarc(oval, 0, 360 * progress / 100, true, mpaint);
    }

  }

  /**
   * 绘制进度文本
   *
   * @param canvas
   */
  private void drawprogresstext(canvas canvas) {
    mpaint.setcolor(progresstextcolor);
    mpaint.setstyle(paint.style.fill);
    mpaint.settextsize(progresstextsize);
    rect textbound = new rect();
    string text = progress + "%";
    //获取文本的矩形区域到textbound中
    mpaint.gettextbounds(text, 0, text.length(), textbound);
    int textwidth = textbound.width();
    int textheight = textbound.height();
    float x = pointx - textwidth / 2.0f;
    float y = pointy + textheight / 2.0f;
    canvas.drawtext(text, x, y, mpaint);
  }

  /**
   * 更新进度
   *
   * @param progress
   */
  public void setvalue(int progress) {
    this.progress = progress;
    invalidate();
  }

  /**
   * 设置进度圆环的样式
   *
   * @param isring true是空心的圆环,false表示实心的圆环
   */
  public void setcircleringstyle(boolean isring) {
    this.isringstyle = isring;
  }

  /**
   * 设置背景透明度
   *
   * @param alpha 0~255
   */
  public void setcirclebackgroundalpha(int alpha) {
    this.circlebackgroundalpha = alpha;
  }

  /**
   * 设置进度文本的大小
   * @param progresstextsize
   */
  public void setprogresstextsize(int progresstextsize) {
    this.progresstextsize = progresstextsize;
  }

  /**
   * 设置进度文本的颜色
   * @param progresstextcolor
   */
  public void setprogresstextcolor(int progresstextcolor) {
    this.progresstextcolor = progresstextcolor;
  }
}

测试类

public class mainactivity extends appcompatactivity {
  private circleprogressview mcircleprogressview;
  private int progress;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    mcircleprogressview = (circleprogressview) findviewbyid(r.id.progressview);
    mcircleprogressview.setcircleringstyle(false);//实心圆
    handlerthread thread = new handlerthread("draw-thread");
    thread.start();
    handler thandler = new handler(thread.getlooper()) {
      @override
      public void handlemessage(message msg) {
        runonuithread(new runnable() {
          @override
          public void run() {
            mcircleprogressview.setvalue(progress++);

          }
        });
        if (progress <100) {
          sendemptymessagedelayed(0, 100);
        }
      }
    };
    thandler.sendemptymessage(0);
  }
}

xml使用方式

<?xml version="1.0" encoding="utf-8"?>
<relativelayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/coloraccent"
 tools:context="mchenys.net.csdn.blog.progressview.mainactivity">


 <mchenys.net.csdn.blog.progressview.circleprogressview
 android:id="@+id/progressview"
 android:layout_width="100dp"
 android:layout_height="200dp"
 android:layout_centerinparent="true"
 />

</relativelayout>

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