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

发布时间 - 2026-01-11 02:00:22    点击率:

本文实例为大家分享了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半圆环进度  # Android圆环型进度效果  # Android半圆环型进度效果  # Android自定义view绘制圆环占比动画  # Android实现动态圆环的图片头像控件  # Android自定义View之酷炫圆环(二)  # Android自定义View之酷炫数字圆环  # Android开发笔记之:在ImageView上绘制圆环的实现方法  # Android实现长按圆环动画View效果的思路代码  # 所占  # 自定义  # 大家分享  # 具体内容  # 大家多多  # viewWidth  # roundWidth  # nowPro  # attrs  # initAttrs  # super  # animator  # context  # textIsShow  # float  # boolean  # roundColor  # progressColor  # textSize  # progress 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 如何自定义safari浏览器工具栏?个性化设置safari浏览器界面教程【技巧】  如何快速生成凡客建站的专业级图册?  Gemini手机端怎么发图片_Gemini手机端发图方法【步骤】  ChatGPT怎么生成Excel公式_ChatGPT公式生成方法【指南】  laravel怎么在请求结束后执行任务(Terminable Middleware)_laravel Terminable Middleware请求结束任务执行方法  Java解压缩zip - 解压缩多个文件或文件夹实例  黑客如何通过漏洞一步步攻陷网站服务器?  大同网页,大同瑞慈医院官网?  php 三元运算符实例详细介绍  电视网站制作tvbox接口,云海电视怎样自定义添加电视源?  做企业网站制作流程,企业网站制作基本流程有哪些?  java中使用zxing批量生成二维码立牌  深圳网站制作的公司有哪些,dido官方网站?  Laravel Docker环境搭建教程_Laravel Sail使用指南  详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)  Laravel如何创建和注册中间件_Laravel中间件编写与应用流程  如何获取上海专业网站定制建站电话?  EditPlus中的正则表达式 实战(1)  javascript中数组(Array)对象和字符串(String)对象的常用方法总结  进行网站优化必须要坚持的四大原则  微信小程序 wx.uploadFile无法上传解决办法  b2c电商网站制作流程,b2c水平综合的电商平台?  如何确保西部建站助手FTP传输的安全性?  Laravel如何实现本地化和多语言支持?(i18n教程)  Linux网络带宽限制_tc配置实践解析【教程】  如何快速搭建高效WAP手机网站?  Laravel队列由Redis驱动怎么配置_Laravel Redis队列使用教程  长沙企业网站制作哪家好,长沙水业集团官方网站?  Laravel与Inertia.js怎么结合_使用Laravel和Inertia构建现代单页应用  高防服务器租用如何选择配置与防御等级?  php读取心率传感器数据怎么弄_php获取max30100的心率值【指南】  如何在景安云服务器上绑定域名并配置虚拟主机?  Laravel Admin后台管理框架推荐_Laravel快速开发后台工具  Laravel如何实现数据库事务?(DB Facade示例)  Java Adapter 适配器模式(类适配器,对象适配器)优缺点对比  学生网站制作软件,一个12岁的学生写小说,应该去什么样的网站?  Win11怎么更改系统语言为中文_Windows11安装语言包并设为显示语言  Laravel如何设置自定义的日志文件名_Laravel根据日期或用户ID生成动态日志【技巧】  Zeus浏览器网页版官网入口 宙斯浏览器官网在线通道  Laravel如何处理CORS跨域问题_Laravel项目CORS配置与解决方案  如何制作一个表白网站视频,关于勇敢表白的小标题?  标题:Vue + Vuex 项目中正确使用 JWT 进行身份认证的实践指南  如何批量查询域名的建站时间记录?  如何在阿里云香港服务器快速搭建网站?  Laravel如何配置和使用队列处理异步任务_Laravel队列驱动与任务分发实例  如何用花生壳三步快速搭建专属网站?  Laravel如何使用Scope本地作用域_Laravel模型常用查询逻辑封装技巧【手册】  Midjourney怎么调整光影效果_Midjourney光影调整方法【指南】  如何基于云服务器快速搭建个人网站?  Laravel如何理解并使用服务容器(Service Container)_Laravel依赖注入与容器绑定说明