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

Android自定义view渐变圆形动画

程序员文章站 2022-11-14 10:31:42
本文实例为大家分享了android自定义view渐变圆形动画的具体代码,供大家参考,具体内容如下 直接上效果图 自定义属性 attrs.xml文件 &l...

本文实例为大家分享了android自定义view渐变圆形动画的具体代码,供大家参考,具体内容如下

直接上效果图

Android自定义view渐变圆形动画

自定义属性

attrs.xml文件

<resources>
  <declare-styleable name="progressring">
    <!--进度起始色-->
    <attr name="pr_progress_start_color" format="color" />
    <!--进度结束色-->
    <attr name="pr_progress_end_color" format="color" />
    <!--背景起始色-->
    <attr name="pr_bg_start_color" format="color" />
    <!--背景中间色-->
    <attr name="pr_bg_mid_color" format="color" />
    <!--背景结束色-->
    <attr name="pr_bg_end_color" format="color" />
    <!--进度值 介于0-100-->
    <attr name="pr_progress" format="integer" />
    <!--进度宽度-->
    <attr name="pr_progress_width" format="dimension" />
    <!--起始角度-->
    <attr name="pr_start_angle" format="integer" />
    <!--扫过的角度-->
    <attr name="pr_sweep_angle" format="integer" />
    <!--是否显示动画-->
    <attr name="pr_show_anim" format="boolean" />
  </declare-styleable>

</resources>

创建一个类 progressring继承自 view

public class progressring extends view {

  private int progressstartcolor;
  private int progressendcolor;
  private int bgstartcolor;
  private int bgmidcolor;
  private int bgendcolor;
  private int progress;
  private float progresswidth;
  private int startangle;
  private int sweepangle;
  private boolean showanim;

  private int mmeasureheight;
  private int mmeasurewidth;

  private paint bgpaint = new paint(paint.anti_alias_flag | paint.dither_flag);
  private paint progresspaint = new paint(paint.anti_alias_flag | paint.dither_flag);

  private rectf prectf;

  private float unitangle;

  private int curprogress = 0;

  public progressring(context context, attributeset attrs) {
    super(context, attrs);
    typedarray ta = context.obtainstyledattributes(attrs, r.styleable.progressring);
    progressstartcolor = ta.getcolor(r.styleable.progressring_pr_progress_start_color, color.yellow);
    progressendcolor = ta.getcolor(r.styleable.progressring_pr_progress_end_color, progressstartcolor);
    bgstartcolor = ta.getcolor(r.styleable.progressring_pr_bg_start_color, color.ltgray);
    bgmidcolor = ta.getcolor(r.styleable.progressring_pr_bg_mid_color, bgstartcolor);
    bgendcolor = ta.getcolor(r.styleable.progressring_pr_bg_end_color, bgstartcolor);
    progress = ta.getint(r.styleable.progressring_pr_progress, 0);
    progresswidth = ta.getdimension(r.styleable.progressring_pr_progress_width, 8f);
    startangle = ta.getint(r.styleable.progressring_pr_start_angle, 150);
    sweepangle = ta.getint(r.styleable.progressring_pr_sweep_angle, 240);
    showanim = ta.getboolean(r.styleable.progressring_pr_show_anim, true);
    ta.recycle();

    unitangle = (float) (sweepangle / 100.0);

    bgpaint.setstyle(paint.style.stroke);
    bgpaint.setstrokecap(paint.cap.round);
    bgpaint.setstrokewidth(progresswidth);

    progresspaint.setstyle(paint.style.stroke);
    progresspaint.setstrokecap(paint.cap.round);
    progresspaint.setstrokewidth(progresswidth);

  }

  @override
  protected void ondraw(canvas canvas) {
    super.ondraw(canvas);

    /*for (int i = 0, end = (int) (progress * unitangle); i <= end; i++) {
      progresspaint.setcolor(getgradient(i / (float) end, progressstartcolor, progressendcolor));
      canvas.drawarc(prectf,
          startangle + i,
          1,
          false,
          progresspaint);
    }*/
    if (!showanim) {
      curprogress = progress;
    }

    drawbg(canvas);
    drawprogress(canvas);

    if (curprogress < progress) {
      curprogress++;
      postinvalidate();
    }
  }

  // 只需要画进度之外的背景即可
  private void drawbg(canvas canvas) {
    float halfsweep = sweepangle / 2;
    for (int i = sweepangle, st = (int) (curprogress * unitangle); i > st; --i) {
      if (i - halfsweep > 0) {
        bgpaint.setcolor(getgradient((i - halfsweep) / halfsweep, bgmidcolor, bgendcolor));
      } else {
        bgpaint.setcolor(getgradient((halfsweep - i) / halfsweep, bgmidcolor, bgstartcolor));
      }
      canvas.drawarc(prectf,
          startangle + i,
          1,
          false,
          bgpaint);
    }
  }

  private void drawprogress(canvas canvas) {
    for (int i = 0, end = (int) (curprogress * unitangle); i <= end; i++) {
      progresspaint.setcolor(getgradient(i / (float) end, progressstartcolor, progressendcolor));
      canvas.drawarc(prectf,
          startangle + i,
          1,
          false,
          progresspaint);
    }
  }

  public void setprogress(@intrange(from = 0, to = 100) int progress) {
    this.progress = progress;
    invalidate();
  }

  public int getprogress() {
    return progress;
  }

  public int getgradient(float fraction, int startcolor, int endcolor) {
    if (fraction > 1) fraction = 1;
    int alphastart = color.alpha(startcolor);
    int redstart = color.red(startcolor);
    int bluestart = color.blue(startcolor);
    int greenstart = color.green(startcolor);
    int alphaend = color.alpha(endcolor);
    int redend = color.red(endcolor);
    int blueend = color.blue(endcolor);
    int greenend = color.green(endcolor);
    int alphadifference = alphaend - alphastart;
    int reddifference = redend - redstart;
    int bluedifference = blueend - bluestart;
    int greendifference = greenend - greenstart;
    int alphacurrent = (int) (alphastart + fraction * alphadifference);
    int redcurrent = (int) (redstart + fraction * reddifference);
    int bluecurrent = (int) (bluestart + fraction * bluedifference);
    int greencurrent = (int) (greenstart + fraction * greendifference);
    return color.argb(alphacurrent, redcurrent, greencurrent, bluecurrent);
  }

  @override
  protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
    super.onmeasure(widthmeasurespec, heightmeasurespec);
    mmeasurewidth = getmeasuredwidth();
    mmeasureheight = getmeasuredheight();
    if (prectf == null) {
      float halfprogresswidth = progresswidth / 2;
      prectf = new rectf(halfprogresswidth + getpaddingleft(),
          halfprogresswidth + getpaddingtop(),
          mmeasurewidth - halfprogresswidth - getpaddingright(),
          mmeasureheight - halfprogresswidth - getpaddingbottom());
    }

  }
}

xml布局

<mycircle.progressring
  android:layout_width="320dp"
  android:layout_height="320dp"
  android:layout_gravity="center_horizontal"
  app:pr_bg_end_color="#00ffffff"
  app:pr_bg_mid_color="#cccccc"
  app:pr_bg_start_color="#00ffffff"
  app:pr_progress="70"
  app:pr_progress_end_color="#f78930"
  app:pr_progress_start_color="#00ffffff"
  app:pr_progress_width="40dp" />

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