当前位置: 移动技术网 > IT编程>移动开发>Android > Android自定义带动画的半圆环型进度效果

Android自定义带动画的半圆环型进度效果

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

林蛙油的食用方法,阮维希的淘宝店,沙井消防器材

本文实例为大家分享了android半圆环型进度效果的具体代码,供大家参考,具体内容如下

package com.newair.ondrawtext;

import android.animation.valueanimator;
import android.annotation.targetapi;
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.os.build;
import android.util.attributeset;
import android.view.view;
import android.view.animation.overshootinterpolator;

/**
 * created by ouhimehime on 16/6/15.
 * --------自定义控件-------
 */
public class customview extends view {


  //画笔
  private paint paint;
  private rectf oval;


  //圆弧颜色
  private int roundcolor;
  //进度颜色
  private int progresscolor;
  //文字内容
  private boolean textisshow;
  //字体大小
  private float textsize = 14;
  //文字颜色
  private int textcolor;
  //最大进度
  private int max = 1000;
  //当前进度
  private int progress = 300;
  //圆弧宽度
  private int roundwidth = 30;

  private int viewwidth; //宽度--控件所占区域

  private float nowpro = 0;//用于动画

  private valueanimator animator;

  public customview(context context) {
    super(context);
  }

  public customview(context context, attributeset attrs) {
    super(context, attrs);
    initattrs(attrs, context);
  }

  public customview(context context, attributeset attrs, int defstyleattr) {
    super(context, attrs, defstyleattr);
    initattrs(attrs, context);
  }

  @targetapi(build.version_codes.lollipop)
  public customview(context context, attributeset attrs, int defstyleattr, int defstyleres) {
    super(context, attrs, defstyleattr, defstyleres);
    initattrs(attrs, context);
  }


  private void initattrs(attributeset attr, context context) {
    typedarray array = context.obtainstyledattributes(attr, r.styleable.customview);


    roundcolor = array.getcolor(r.styleable.customview_roundcolor, color.black);//环形颜色
    progresscolor = array.getcolor(r.styleable.customview_progresscolor, color.red);//进度颜色
    textisshow = array.getboolean(r.styleable.customview_textisshow, false);//文字
    textsize = array.getdimension(r.styleable.customview_textsize, 14);//文字大小
    textcolor = array.getcolor(r.styleable.customview_textcolor, color.black);//文字颜色
    roundwidth = array.getint(r.styleable.customview_roundwidth, 30);//圆环宽度

    array.recycle();

    //动画
    animator = valueanimator.offloat(0, progress);
    animator.setduration(1800);
    animator.setinterpolator(new overshootinterpolator());
    animator.addupdatelistener(new valueanimator.animatorupdatelistener() {
      @override
      public void onanimationupdate(valueanimator animation) {
        nowpro = (float) animation.getanimatedvalue();
        postinvalidate();
      }
    });
    animator.start();

  }

  @override
  protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {

    final int widthspecmode = measurespec.getmode(widthmeasurespec);
    final int widthspecsize = measurespec.getsize(widthmeasurespec);

    if (widthspecmode == measurespec.at_most) {//可获得最大空间
      setmeasureddimension(widthmeasurespec, (widthspecsize / 2) + (int) (math.cos(20) * (widthspecsize / 2)));
    } else if (widthmeasurespec == measurespec.exactly) {//一般指精确值
      setmeasureddimension(widthmeasurespec, (widthspecsize / 2) + (int) (math.cos(20) * (widthspecsize / 2)));
    } else {
      setmeasureddimension(widthmeasurespec, (viewwidth / 2) + (int) (math.cos(20) * (viewwidth / 2)));
    }
  }

  @override
  protected void onsizechanged(int w, int h, int oldw, int oldh) {
    super.onsizechanged(w, h, oldw, oldh);

    viewwidth = w;//得到宽度以此来计算控件所占实际大小

    //计算画布所占区域
    oval = new rectf();
    oval.left = roundwidth + getpaddingleft();
    oval.top = roundwidth + getpaddingtop();
    oval.right = viewwidth - roundwidth - getpaddingright();
    oval.bottom = viewwidth - roundwidth - getpaddingbottom();

  }


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

    paint paint = new paint();
    paint.setantialias(true);            //设置画笔为无锯齿
    paint.setcolor(roundcolor);           //设置画笔颜色
    paint.setstrokewidth(roundwidth);        //线宽
    paint.setstyle(paint.style.stroke);       //空心
    canvas.drawarc(oval, 160, 220, false, paint);  //绘制圆弧

    //画进度层
    paint.setcolor(progresscolor);
    paint.setstrokewidth(roundwidth + 1);
    canvas.drawarc(oval, 160, 220 * nowpro / max, false, paint); //绘制圆弧


    if (textisshow) {
      paint.setcolor(textcolor);
      paint.setstrokewidth(0);
      paint.settypeface(typeface.default);
      paint.settextsize(textsize * 2);
      float textwidth = paint.measuretext((int) ((nowpro / (float) max) * 100) + "%");
      canvas.drawtext((int) ((nowpro / (float) max) * 100) + "%", viewwidth / 2 - textwidth / 2, viewwidth / 2, paint);
    }

  }


  private int getdefaultheight() {
    return 0;
  }

  private int getdefaultwidth() {
    return 0;
  }


  public int getroundcolor() {
    return roundcolor;
  }

  public void setroundcolor(int roundcolor) {
    this.roundcolor = roundcolor;
  }

  public int getprogresscolor() {
    return progresscolor;
  }

  public void setprogresscolor(int progresscolor) {
    this.progresscolor = progresscolor;
  }

  public boolean gettext() {
    return textisshow;
  }

  public void settext(boolean text) {
    this.textisshow = text;
  }

  public float gettextsize() {
    return textsize;
  }

  public void settextsize(float textsize) {
    this.textsize = textsize;
  }

  public int gettextcolor() {
    return textcolor;
  }

  public void settextcolor(int textcolor) {
    this.textcolor = textcolor;
  }

  public int getmax() {
    return max;
  }

  public void setmax(int max) {
    this.max = max;
  }

  public int getprogress() {
    return progress;
  }

  public void setprogress(int progress) {
    this.progress = progress;
  }
}

自定义属性

<declare-styleable name="customview">
    <attr name="roundcolor" format="color" />
    <attr name="progresscolor" format="color" />
    <attr name="textisshow" format="boolean" />
    <attr name="textsize" format="dimension" />
    <attr name="textcolor" format="color" />
    <attr name="roundwidth" format="integer" />
  </declare-styleable>

用法

<com.newair.ondrawtext.customview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="visible"
    app:progresscolor="@android:color/holo_orange_dark"
    app:roundcolor="@android:color/holo_blue_dark"
    app:roundwidth="45"
    app:textcolor="@android:color/black"
    app:textisshow="true"
    app:textsize="14sp" />

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

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

相关文章:

验证码:
移动技术网