Android自定义view实现圆环进度条效果
更新时间:2022年2月16日 16:47 点击:262 作者:风云正
本文实例为大家分享了Android自定义view实现圆环进度条效果的具体代码,供大家参考,具体内容如下
一、实现效果图
二、核心代码
自定义view的属性
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="RingProgressBar"> <attr name="ringColor" format="color" /> <attr name="ringProgressColor" format="color" /> <attr name="ringWidth" format="dimension"></attr> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> <attr name="max" format="integer"></attr> <attr name="textIsDisplayable" format="boolean"></attr> <attr name="style"> <enum name="STROKE" value="0"></enum> <enum name="FILL" value="1"></enum> </attr> </declare-styleable> </resources>
自定义view
package com.czhappy.ringprogressdemo.view; 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.util.AttributeSet; import android.util.Log; import android.view.View; import com.czhappy.ringprogressdemo.R; /** * Description: * User: chenzheng * Date: 2017/2/10 0010 * Time: 15:11 */ public class RingProgressBar extends View { /** * 画笔对象的引用 */ private Paint ringPaint; private Paint ringProgressPaint; private Paint txtPaint; /** * 圆环的颜色 */ private int ringColor; /** * 圆环进度的颜色 */ private int ringProgressColor; /** * 中间进度百分比的字符串的颜色 */ private int textColor; /** * 中间进度百分比的字符串的字体 */ private float textSize; /** * 圆环的宽度 */ private float ringWidth; /** * 最大进度 */ private int max; /** * 当前进度 */ private int progress; /** * 是否显示中间的进度 */ private boolean textIsDisplayable; private Context mContext; /** * 进度的风格,实心或者空心 */ private int style; public static final int STROKE = 0; public static final int FILL = 1; public RingProgressBar(Context context) { this(context, null); } public RingProgressBar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RingProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.mContext = context; // 获取自定义的属性 initAttrs(context, attrs); initPaint(); } private void initAttrs(Context context, AttributeSet attrs) { TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RingProgressBar); //获取自定义属性和默认值 ringColor = mTypedArray.getColor(R.styleable.RingProgressBar_ringColor, Color.GRAY); ringProgressColor = mTypedArray.getColor(R.styleable.RingProgressBar_ringProgressColor, Color.GREEN); textColor = mTypedArray.getColor(R.styleable.RingProgressBar_textColor, Color.GREEN); textSize = mTypedArray.getDimension(R.styleable.RingProgressBar_textSize, 16); ringWidth = mTypedArray.getDimension(R.styleable.RingProgressBar_ringWidth, 5); max = mTypedArray.getInteger(R.styleable.RingProgressBar_max, 100); textIsDisplayable = mTypedArray.getBoolean(R.styleable.RingProgressBar_textIsDisplayable, true); style = mTypedArray.getInt(R.styleable.RingProgressBar_style, 0); //资源回收 mTypedArray.recycle(); } /** * 初始化画笔 */ private void initPaint() { //圆环画笔 ringPaint = new Paint(); ringPaint.setColor(ringColor); //设置圆环的颜色 ringPaint.setStyle(Paint.Style.STROKE); //设置空心 ringPaint.setStrokeWidth(ringWidth); //设置圆环的宽度 ringPaint.setAntiAlias(true); //消除锯齿 //圆环进度画笔 ringProgressPaint = new Paint(); ringProgressPaint.setColor(ringProgressColor); //设置圆环的颜色 ringProgressPaint.setStrokeWidth(ringWidth); //设置圆环的宽度 ringProgressPaint.setAntiAlias(true); //消除锯齿 switch (style) { case STROKE: ringProgressPaint.setStyle(Paint.Style.STROKE); break; case FILL: ringProgressPaint.setStyle(Paint.Style.FILL_AND_STROKE); break; } //百分比文字画笔 txtPaint = new Paint(); txtPaint.setColor(textColor); txtPaint.setTextSize(textSize); txtPaint.setTypeface(Typeface.DEFAULT_BOLD); //设置字体 } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //圆心坐标 int mXCenter = getWidth() / 2; int mYCenter = getHeight() / 2; int radius = (int) (mXCenter - ringWidth / 2); //圆环的半径 //绘制圆环 canvas.drawCircle(mXCenter, mYCenter, radius, ringPaint); //绘制圆环进度 RectF oval = new RectF(mXCenter - radius, mYCenter - radius, mXCenter + radius, mYCenter + radius); //用于定义的圆弧的形状和大小的界限 switch (style) { case STROKE: canvas.drawArc(oval, -90, 360 * progress / max, false, ringProgressPaint); //根据进度画圆弧 break; case FILL: if (progress != 0) canvas.drawArc(oval, -90, 360 * progress / max, true, ringProgressPaint); //根据进度画圆弧 break; } //绘制百分比数字 //文字绘制 String txt = progress + "%"; //文字的长度 float mTxtWidth = txtPaint.measureText(txt, 0, txt.length()); Log.e("tag", textIsDisplayable+","+progress+(style==STROKE)); if(textIsDisplayable && progress!=0 && style==STROKE){ canvas.drawText(txt, mXCenter - mTxtWidth / 2, mYCenter+textSize/2, txtPaint); } } public synchronized int getMax() { return max; } /** * 设置进度的最大值 * @param max */ public synchronized void setMax(int max) { if(max < 0){ throw new IllegalArgumentException("max not less than 0"); } this.max = max; } /** * 获取进度.需要同步 * @return */ public synchronized int getProgress() { return progress; } /** * 设置进度,此为线程安全控件,由于考虑多线程的问题,需要同步 * 刷新界面调用postInvalidate()能在非UI线程刷新 * @param progress */ public synchronized void setProgress(int progress) { if(progress < 0){ throw new IllegalArgumentException("progress not less than 0"); } if(progress > max){ progress = max; } if(progress <= max){ this.progress = progress; postInvalidate(); } } public int getRingColor() { return ringColor; } public void setRingColor(int ringColor) { this.ringColor = ringColor; } public int getRingProgressColor() { return ringProgressColor; } public void setRingProgressColor(int ringProgressColor) { this.ringProgressColor = ringProgressColor; } public int getTextColor() { return textColor; } public void setTextColor(int textColor) { this.textColor = textColor; } public float getTextSize() { return textSize; } public void setTextSize(float textSize) { this.textSize = textSize; } public float getRingWidth() { return ringWidth; } public void setRingWidth(float roundWidth) { this.ringWidth = roundWidth; } }
MainActivity.java
package com.czhappy.ringprogressdemo.activity; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import com.czhappy.ringprogressdemo.R; import com.czhappy.ringprogressdemo.view.RingProgressBar; public class MainActivity extends AppCompatActivity { private RingProgressBar myProgress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myProgress = (RingProgressBar) findViewById(R.id.myProgress); } public void beginAnim(View view){ new Thread(){ @Override public void run() { super.run(); for(int i=0; i<=60; i++){ try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } myProgress.setProgress(i); } } }.start(); } }
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ring="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="match_parent" android:onClick="beginAnim" android:layout_height="wrap_content" android:text="开始动画"/> <com.czhappy.ringprogressdemo.view.RingProgressBar android:id="@+id/myProgress" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="20dp" ring:ringColor="@color/ring_color" ring:ringProgressColor="@color/ring_progress_color" ring:textColor="@color/ring_progress_color" ring:textIsDisplayable="true" ring:ringWidth="6dp" ring:textSize="16sp"/> </LinearLayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
原文出处:https://blog.csdn.net/chenzheng8975/article/details/54973023
相关文章
- 下面我们来看一篇关于Android子控件超出父控件的范围显示出来方法,希望这篇文章能够帮助到各位朋友,有碰到此问题的朋友可以进来看看哦。 <RelativeLayout xmlns:an...2016-10-02
- 这篇文章主要介绍了解决echarts 一条柱状图显示两个值,类似进度条的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-20
Android开发中findViewById()函数用法与简化
findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20- 如果我们的项目需要做来电及短信的功能,那么我们就得在Android模拟器开发这些功能,本来就来告诉我们如何在Android模拟器上模拟来电及来短信的功能。 在Android模拟...2016-09-20
- 本文实例讲述了jQuery实现文件上传进度条效果的代码。分享给大家供大家参考。具体如下: 运行效果截图如下:具体代码如下:<!DOCTYPE html><html><head><meta charset="utf-8"><title>upload</title><link rel="stylesheet...2015-11-24
- 夜神android模拟器如何设置代理呢?对于这个问题其实操作起来是非常的简单,下面小编来为各位详细介绍夜神android模拟器设置代理的方法,希望例子能够帮助到各位。 app...2016-09-20
- 为了增强android应用的用户体验,我们可以在一些Button按钮上自定义动态的设置一些样式,比如交互时改变字体、颜色、背景图等。 今天来看一个通过重写Button来动态实...2016-09-20
- 如果我们要在Android应用APP中加载html5页面,我们可以使用WebView,本文我们分享两个WebView加载html5页面实例应用。 实例一:WebView加载html5实现炫酷引导页面大多...2016-09-20
ant design中upload组件上传大文件,显示进度条进度的实例
这篇文章主要介绍了ant design中upload组件上传大文件,显示进度条进度的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-10-29- 本文实例讲述了jquery实现模拟百分比进度条渐变效果代码。分享给大家供大家参考,具体如下:这里为了便于看到加载百分比,对代码进行了处理,实际使用时并不需要这样。运行效果截图如下:在线演示地址如下:http://demo.jb51.net...2015-10-30
- 深入理解Android中View和ViewGroup从组成架构上看,似乎ViewGroup在View之上,View需要继承ViewGroup,但实际上不是这样的。View是基类,ViewGroup是它的子类。本教程我们深...2016-09-20
- 下面我们来看一篇关于Android自定义WebView网络视频播放控件开发例子,这个文章写得非常的不错下面给各位共享一下吧。 因为业务需要,以下代码均以Youtube网站在线视...2016-10-02
- java开发的Android应用,性能一直是一个大问题,,或许是Java语言本身比较消耗内存。本文我们来谈谈Android 性能优化之MemoryFile文件读写。 Android匿名共享内存对外A...2016-09-20
- TextView默认是横着显示了,今天我们一起来看看Android设置TextView竖着显示如何来实现吧,今天我们就一起来看看操作细节,具体的如下所示。 在开发Android程序的时候,...2016-10-02
android.os.BinderProxy cannot be cast to com解决办法
本文章来给大家介绍关于android.os.BinderProxy cannot be cast to com解决办法,希望此文章对各位有帮助呀。 Android在绑定服务的时候出现java.lang.ClassCastExc...2016-09-20- 这篇文章主要介绍了Android 实现钉钉自动打卡功能的步骤,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下...2021-03-15
- 下面我们来看一篇关于Android 开发之布局细节对比:RTL模式 ,希望这篇文章对各位同学会带来帮助,具体的细节如下介绍。 前言 讲真,好久没写博客了,2016都过了一半了,赶紧...2016-10-02
- 这篇文章主要介绍了C#实现带进度条的ListView 的相关资料,需要的朋友可以参考下...2020-06-25
- 首先如果要在程序中使用sdcard进行存储,我们必须要在AndroidManifset.xml文件进行下面的权限设置: 在AndroidManifest.xml中加入访问SDCard的权限如下: <!--...2016-09-20
- 下面来给各位简单的介绍一下关于Android开发之PhoneGap打包及错误解决办法,希望碰到此类问题的同学可进入参考一下哦。 在我安装、配置好PhoneGap项目的所有依赖...2016-09-20