Android 自定义圆弧进度条

本贴最后更新于 2537 天前,其中的信息可能已经时过境迁

挺久没写文章了,近段时间被拉过去写 JS 项目了,在做一个项目的时候,遇到一个新的需求就是空气质量,实现空气污染指数的时候,需要到一个圆弧的进度,在网上没找到合适,干脆就自己写了一个,顺便复习一下自定义 View,下面是具体的实现。

先看一下效果

这里的话我只做一个进度条,使用也很简单。圆弧外的文本是一个 textview,不是这个控件里面的,说明一下。

下面先看一下整体的代码:

import android.animation.ValueAnimator;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Rect;

import android.graphics.RectF;

import android.support.annotation.Nullable;

import android.util.AttributeSet;

import android.view.View;



public class ArcProgressBar extends View {

private static final String TAG = "ArcProgressBar";

/**

 * 圆弧的宽度

 */

private int mStrokeWidth = dp2px(8);

/**

 * 圆弧开始的角度

 */

private float mStartAngle = 135;

/**

 * 起点角度和终点角度对应的夹角大小

 */

private float mAngleSize = 270;

/**

 * 圆弧背景颜色

 */

private int mArcBgColor = Color.YELLOW;

/**

 * 最大的进度,用于计算进度与夹角的比例

 */

private float mMaxProgress = 500;

/**

 * 当前进度对应的起点角度到当前进度角度夹角的大小

 */

private float mCurrentAngleSize = 0;

/**

 * 当前进度

 */

private float mCurrentProgress = 0;

/**

 * 动画的执行时长

 */

private long mDuration = 3000;

/**

 * 进度圆弧的颜色

 */

private int mProgressColor = Color.RED;

/**

 * 第一行文本

 */

private String mFirstText = "42";

/**

 * 第一行文本的颜色

 */

private int mFirstTextColor = Color.RED;

/**

 * 第一行文本的字体大小

 */

private float mFirstTextSize = 56f;

/**

 * 第二行文本

 */

private String mSecondText = "优";

/**

 * 第二行文本的颜色

 */

private int mSecondTextColor = Color.RED;

/**

 * 第二行文本的字体大小

 */

private float mSecondTextSize = 56f;

public ArcProgressBar(Context context) {

    super(context, null);

}

public ArcProgressBar(Context context, @Nullable AttributeSet attrs) {

    super(context, attrs, 0);

}

public ArcProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

    super(context, attrs, defStyleAttr);

    initAttr(context, attrs);

}

/**

 * 设置初始化的参数

 *

 * @param context

 * @param attrs

 */

private void initAttr(Context context, AttributeSet attrs) {

    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ArcProgressBar);

    mMaxProgress = array.getFloat(R.styleable.ArcProgressBar_arc_max_progress, 500f);

    mArcBgColor = array.getColor(R.styleable.ArcProgressBar_arc_bg_color, Color.YELLOW);

    mStrokeWidth = dp2px(array.getDimension(R.styleable.ArcProgressBar_arc_stroke_width, 12f));

    mCurrentProgress = array.getFloat(R.styleable.ArcProgressBar_arc_progress, 300f);

    mProgressColor = array.getColor(R.styleable.ArcProgressBar_arc_progress_color, Color.RED);

    mFirstText = array.getString(R.styleable.ArcProgressBar_arc_first_text);

    mFirstTextSize = dp2px(array.getDimension(R.styleable.ArcProgressBar_arc_first_text_size, 20f));

    mFirstTextColor = array.getColor(R.styleable.ArcProgressBar_arc_first_text_color, Color.RED);

    mSecondText = array.getString(R.styleable.ArcProgressBar_arc_second_text);

    mSecondTextSize = dp2px(array.getDimension(R.styleable.ArcProgressBar_arc_second_text_size, 20f));

    mSecondTextColor = array.getColor(R.styleable.ArcProgressBar_arc_second_text_color, Color.RED);

    mAngleSize = array.getFloat(R.styleable.ArcProgressBar_arc_angle_size, 270f);

    mStartAngle = array.getFloat(R.styleable.ArcProgressBar_arc_start_angle, 135f);

    array.recycle();

}

@Override

protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

    int centerX = getWidth() / 2;

    RectF rectF = new RectF();

    rectF.left = mStrokeWidth;

    rectF.top = mStrokeWidth;

    rectF.right = centerX * 2 - mStrokeWidth;

    rectF.bottom = centerX * 2 - mStrokeWidth;

    //画最外层的圆弧

    drawArcBg(canvas, rectF);

    //画进度

    drawArcProgress(canvas, rectF);

    //绘制第一级文本

    drawFirstText(canvas, centerX);

    //绘制第二级文本

    drawSecondText(canvas, centerX);

}

/**

 * 画最开始的圆弧

 *

 * @param canvas

 * @param rectF

 */

private void drawArcBg(Canvas canvas, RectF rectF) {

    Paint mPaint = new Paint();

    //画笔的填充样式,Paint.Style.FILL 填充内部;Paint.Style.FILL_AND_STROKE 填充内部和描边;Paint.Style.STROKE 描边

    mPaint.setStyle(Paint.Style.STROKE);

    //圆弧的宽度

    mPaint.setStrokeWidth(mStrokeWidth);

    //抗锯齿

    mPaint.setAntiAlias(true);

    //画笔的颜色

    mPaint.setColor(mArcBgColor);

    //画笔的样式 Paint.Cap.Round 圆形,Cap.SQUARE 方形

    mPaint.setStrokeCap(Paint.Cap.ROUND);

    //开始画圆弧

    canvas.drawArc(rectF, mStartAngle, mAngleSize, false, mPaint);

}

/**

 * 画进度的圆弧

 *

 * @param canvas

 * @param rectF

 */

private void drawArcProgress(Canvas canvas, RectF rectF) {

    Paint paint = new Paint();

    paint.setStyle(Paint.Style.STROKE);

    paint.setStrokeWidth(mStrokeWidth);

    paint.setColor(mProgressColor);

    paint.setAntiAlias(true);

    paint.setStrokeCap(Paint.Cap.ROUND);

    canvas.drawArc(rectF, mStartAngle, mCurrentAngleSize, false, paint);

}

/**

 * 绘制第一级文字

 *

 * @param canvas  画笔

 * @param centerX 位置

 */

private void drawFirstText(Canvas canvas, float centerX) {

    Paint paint = new Paint();

    paint.setAntiAlias(true);

    paint.setColor(mFirstTextColor);

    paint.setTextAlign(Paint.Align.CENTER);

    paint.setTextSize(mFirstTextSize);

    Rect firstTextBounds = new Rect();

    paint.getTextBounds(mFirstText, 0, mFirstText.length(), firstTextBounds);

    canvas.drawText(mFirstText, centerX, firstTextBounds.height() / 2 + getHeight() * 2 / 5, paint);

}

/**

 * 绘制第二级文本

 *

 * @param canvas  画笔

 * @param centerX 文本

 */

private void drawSecondText(Canvas canvas, float centerX) {

    Paint paint = new Paint();

    paint.setAntiAlias(true);

    paint.setColor(mSecondTextColor);

    paint.setTextAlign(Paint.Align.CENTER);

    paint.setTextSize(mSecondTextSize);

    Rect bounds = new Rect();

    paint.getTextBounds(mSecondText, 0, mSecondText.length(), bounds);

    canvas.drawText(mSecondText, centerX, getHeight() / 2 + bounds.height() / 2 +

            getFontHeight(mSecondText, mSecondTextSize), paint);

}

/**

 * 设置最大的进度

 *

 * @param progress

 */

public void setMaxProgress(int progress) {

    if (progress < 0) {

        throw new IllegalArgumentException("Progress value can not be less than 0 ");

    }

    mMaxProgress = progress;

}

/**

 * 设置当前进度

 *

 * @param progress

 */

public void setProgress(float progress) {

    if (progress < 0) {

        throw new IllegalArgumentException("Progress value can not be less than 0");

    }

    if (progress > mMaxProgress) {

        progress = mMaxProgress;

    }

    mCurrentProgress = progress;

    float size = mCurrentProgress / mMaxProgress;

    mCurrentAngleSize = (int) (mAngleSize * size);

    setAnimator(0, mCurrentAngleSize);

}

/**

 * 设置进度圆弧的颜色

 *

 * @param color

 */

public void setProgressColor(int color) {

    if (color == 0) {

        throw new IllegalArgumentException("Color can no be 0");

    }

    mProgressColor = color;

}

/**

 * 设置圆弧的颜色

 *

 * @param color

 */

public void setArcBgColor(int color) {

    if (color == 0) {

        throw new IllegalArgumentException("Color can no be 0");

    }

    mArcBgColor = color;

}

/**

 * 设置圆弧的宽度

 *

 * @param strokeWidth

 */

public void setStrokeWidth(int strokeWidth) {

    if (strokeWidth < 0) {

        throw new IllegalArgumentException("strokeWidth value can not be less than 0");

    }

    mStrokeWidth = dp2px(strokeWidth);

}

/**

 * 设置动画的执行时长

 *

 * @param duration

 */

public void setAnimatorDuration(long duration) {

    if (duration < 0) {

        throw new IllegalArgumentException("Duration value can not be less than 0");

    }

    mDuration = duration;

}

/**

 * 设置第一行文本

 *

 * @param text

 */

public void setFirstText(String text) {

    mFirstText = text;

}

/**

 * 设置第一行文本的颜色

 *

 * @param color

 */

public void setFirstTextColor(int color) {

    if (color <= 0) {

        throw new IllegalArgumentException("Color value can not be less than 0");

    }

    mFirstTextColor = color;

}

/**

 * 设置第一行文本的大小

 *

 * @param textSize

 */

public void setFirstTextSize(float textSize) {

    if (textSize <= 0) {

        throw new IllegalArgumentException("textSize can not be less than 0");

    }

    mFirstTextSize = textSize;

}

/**

 * 设置第二行文本

 *

 * @param text

 */

public void setSecondText(String text) {

    mSecondText = text;

}

/**

 * 设置第二行文本的颜色

 *

 * @param color

 */

public void setSecondTextColor(int color) {

    if (color == 0) {

        throw new IllegalArgumentException("Color value can not be less than 0");

    }

    mSecondTextColor = color;

}

/**

 * 设置第二行文本的大小

 *

 * @param textSize

 */

public void setSecondTextSize(float textSize) {

    if (textSize <= 0) {

        throw new IllegalArgumentException("textSize can not be less than 0");

    }

    mSecondTextSize = textSize;

}

/**

 * 设置圆弧开始的角度

 *

 * @param startAngle

 */

public void setStartAngle(int startAngle) {

    mStartAngle = startAngle;

}

/**

 * 设置圆弧的起始角度到终点角度的大小

 *

 * @param angleSize

 */

public void setAngleSize(int angleSize) {

    mAngleSize = angleSize;

}

/**

 * dp转成px

 *

 * @param dp

 * @return

 */

private int dp2px(float dp) {

    float density = getResources().getDisplayMetrics().density;

    return (int) (dp * density + 0.5f * (dp >= 0 ? 1 : -1));

}

/**

 * 设置动画

 *

 * @param start  开始位置

 * @param target 结束位置

 */

private void setAnimator(float start, float target) {

    ValueAnimator valueAnimator = ValueAnimator.ofFloat(start, target);

    valueAnimator.setDuration(mDuration);

    valueAnimator.setTarget(mCurrentAngleSize);

    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

        @Override

        public void onAnimationUpdate(ValueAnimator valueAnimator) {

            mCurrentAngleSize = (float) valueAnimator.getAnimatedValue();

            invalidate();

        }

    });

    valueAnimator.start();

}

/**

 * 测量字体的高度

 *

 * @param textStr

 * @param fontSize

 * @return

 */

private float getFontHeight(String textStr, float fontSize) {

    Paint paint = new Paint();

    paint.setTextSize(fontSize);

    Rect bounds = new Rect();

    paint.getTextBounds(textStr, 0, textStr.length(), bounds);

    return bounds.height();

}

}

然后是具体使用控件里面的代码:

    android:id="@+id/arc"

    android:layout_width="100dp"

    android:layout_height="100dp"

    android:layout_gravity="center"/>

这里使用非常简单,跟其它控件的使用没什么区别。

里面还定义了很多直接在 xml 文件里面就可以直接设置的属性

这个自定义 View 非常简单,代码里面注释也写的很清楚了,就不做过多的介绍了,有兴趣的可以去做更多的扩展,比如,直接做成圆形或者扇形,还有这里必须要两行文本,如果项目需要,也可以直接去删除掉文本,不过需要计算一下文本的绘制位置。

有兴趣的可以去 GitHub 上面给个 Start,地址是:传送门

  • Android

    Android 是一种以 Linux 为基础的开放源码操作系统,主要使用于便携设备。2005 年由 Google 收购注资,并拉拢多家制造商组成开放手机联盟开发改良,逐渐扩展到到平板电脑及其他领域上。

    334 引用 • 323 回帖 • 19 关注
  • 进度条
    1 引用 • 1 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...

推荐标签 标签

  • B3log

    B3log 是一个开源组织,名字来源于“Bulletin Board Blog”缩写,目标是将独立博客与论坛结合,形成一种新的网络社区体验,详细请看 B3log 构思。目前 B3log 已经开源了多款产品:SymSoloVditor思源笔记

    1083 引用 • 3461 回帖 • 257 关注
  • 自由行
    1 关注
  • FreeMarker

    FreeMarker 是一款好用且功能强大的 Java 模版引擎。

    23 引用 • 20 回帖 • 429 关注
  • 禅道

    禅道是一款国产的开源项目管理软件,她的核心管理思想基于敏捷方法 scrum,内置了产品管理和项目管理,同时又根据国内研发现状补充了测试管理、计划管理、发布管理、文档管理、事务管理等功能,在一个软件中就可以将软件研发中的需求、任务、bug、用例、计划、发布等要素有序的跟踪管理起来,完整地覆盖了项目管理的核心流程。

    6 引用 • 15 回帖 • 182 关注
  • OkHttp

    OkHttp 是一款 HTTP & HTTP/2 客户端库,专为 Android 和 Java 应用打造。

    16 引用 • 6 回帖 • 48 关注
  • 人工智能

    人工智能(Artificial Intelligence)是研究、开发用于模拟、延伸和扩展人的智能的理论、方法、技术及应用系统的一门技术科学。

    77 引用 • 159 回帖
  • Kafka

    Kafka 是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据。 这种动作(网页浏览,搜索和其他用户的行动)是现代系统中许多功能的基础。 这些数据通常是由于吞吐量的要求而通过处理日志和日志聚合来解决。

    35 引用 • 35 回帖
  • GitLab

    GitLab 是利用 Ruby 一个开源的版本管理系统,实现一个自托管的 Git 项目仓库,可通过 Web 界面操作公开或私有项目。

    46 引用 • 72 回帖
  • FFmpeg

    FFmpeg 是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。

    23 引用 • 31 回帖 • 8 关注
  • Quicker

    Quicker 您的指尖工具箱!操作更少,收获更多!

    26 引用 • 85 回帖
  • Python

    Python 是一种面向对象、直译式电脑编程语言,具有近二十年的发展历史,成熟且稳定。它包含了一组完善而且容易理解的标准库,能够轻松完成很多常见的任务。它的语法简捷和清晰,尽量使用无异义的英语单词,与其它大多数程序设计语言使用大括号不一样,它使用缩进来定义语句块。

    536 引用 • 672 回帖
  • 创业

    你比 99% 的人都优秀么?

    83 引用 • 1398 回帖
  • IDEA

    IDEA 全称 IntelliJ IDEA,是一款 Java 语言开发的集成环境,在业界被公认为最好的 Java 开发工具之一。IDEA 是 JetBrains 公司的产品,这家公司总部位于捷克共和国的首都布拉格,开发人员以严谨著称的东欧程序员为主。

    180 引用 • 400 回帖 • 1 关注
  • Sandbox

    如果帖子标签含有 Sandbox ,则该帖子会被视为“测试帖”,主要用于测试社区功能,排查 bug 等,该标签下内容不定期进行清理。

    386 引用 • 1226 回帖 • 593 关注
  • Google

    Google(Google Inc.,NASDAQ:GOOG)是一家美国上市公司(公有股份公司),于 1998 年 9 月 7 日以私有股份公司的形式创立,设计并管理一个互联网搜索引擎。Google 公司的总部称作“Googleplex”,它位于加利福尼亚山景城。Google 目前被公认为是全球规模最大的搜索引擎,它提供了简单易用的免费服务。不作恶(Don't be evil)是谷歌公司的一项非正式的公司口号。

    49 引用 • 192 回帖
  • ActiveMQ

    ActiveMQ 是 Apache 旗下的一款开源消息总线系统,它完整实现了 JMS 规范,是一个企业级的消息中间件。

    19 引用 • 13 回帖 • 641 关注
  • 开源中国

    开源中国是目前中国最大的开源技术社区。传播开源的理念,推广开源项目,为 IT 开发者提供了一个发现、使用、并交流开源技术的平台。目前开源中国社区已收录超过两万款开源软件。

    7 引用 • 86 回帖
  • Oracle

    Oracle(甲骨文)公司,全称甲骨文股份有限公司(甲骨文软件系统有限公司),是全球最大的企业级软件公司,总部位于美国加利福尼亚州的红木滩。1989 年正式进入中国市场。2013 年,甲骨文已超越 IBM,成为继 Microsoft 后全球第二大软件公司。

    103 引用 • 126 回帖 • 443 关注
  • IPFS

    IPFS(InterPlanetary File System,星际文件系统)是永久的、去中心化保存和共享文件的方法,这是一种内容可寻址、版本化、点对点超媒体的分布式协议。请浏览 IPFS 入门笔记了解更多细节。

    20 引用 • 245 回帖 • 239 关注
  • Linux

    Linux 是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 POSIX 和 Unix 的多用户、多任务、支持多线程和多 CPU 的操作系统。它能运行主要的 Unix 工具软件、应用程序和网络协议,并支持 32 位和 64 位硬件。Linux 继承了 Unix 以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。

    923 引用 • 936 回帖
  • 爬虫

    网络爬虫(Spider、Crawler),是一种按照一定的规则,自动地抓取万维网信息的程序。

    106 引用 • 275 回帖
  • frp

    frp 是一个可用于内网穿透的高性能的反向代理应用,支持 TCP、UDP、 HTTP 和 HTTPS 协议。

    16 引用 • 7 回帖 • 1 关注
  • Love2D

    Love2D 是一个开源的, 跨平台的 2D 游戏引擎。使用纯 Lua 脚本来进行游戏开发。目前支持的平台有 Windows, Mac OS X, Linux, Android 和 iOS。

    14 引用 • 53 回帖 • 520 关注
  • LeetCode

    LeetCode(力扣)是一个全球极客挚爱的高质量技术成长平台,想要学习和提升专业能力从这里开始,充足技术干货等你来啃,轻松拿下 Dream Offer!

    209 引用 • 72 回帖
  • Ant-Design

    Ant Design 是服务于企业级产品的设计体系,基于确定和自然的设计价值观上的模块化解决方案,让设计者和开发者专注于更好的用户体验。

    17 引用 • 23 回帖 • 3 关注
  • 运维

    互联网运维工作,以服务为中心,以稳定、安全、高效为三个基本点,确保公司的互联网业务能够 7×24 小时为用户提供高质量的服务。

    148 引用 • 257 回帖
  • Openfire

    Openfire 是开源的、基于可拓展通讯和表示协议 (XMPP)、采用 Java 编程语言开发的实时协作服务器。Openfire 的效率很高,单台服务器可支持上万并发用户。

    6 引用 • 7 回帖 • 94 关注