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

所属分类: 软件编程 / Android 阅读数: 44
收藏 0 赞 0 分享

本文实例为大家分享了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" />

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

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多