android自定义进度条渐变圆形

在安全卫生上,经常看到有圆形的进度条在转动,效果非常好看,于是就尝试去实现一下,具体实现过程不多说了,直接上效果图,先炫耀下。

效果图:

分析:比较常见于扫描结果、进度条等场景

利用canvas.drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)绘制圆弧

Paint的一些属性定义粗细、颜色、样式等

LinearGradient实现颜色的线型渐变

同样的道理,可以画出长条进度条,扇图饼图等,感兴趣可以试下..

package com.liujing.progressviewdemo; /*** * 自定义圆弧进度条 * * @author liujing */ public class ProgressView extends View { //分段颜色 private static final int[] SECTION_COLORS = { Color.GREEN, Color.YELLOW, Color.RED }; private static final String[] ALARM_LEVEL = { "安全", "低危", "中危", "高危" }; private float maxCount; private float currentCount; private int score; private String crrentLevel; private Paint mPaint; private Paint mTextPaint; private int mWidth, mHeight; public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } public ProgressView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ProgressView(Context context) { this(context, null); } private void init(Context context) { mPaint = new Paint(); mTextPaint = new Paint(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); initPaint(); RectF rectBlackBg = new RectF(20, 20, mWidth - 20, mHeight - 20); canvas.drawArc(rectBlackBg, 0, 360, false, mPaint); mPaint.setColor(Color.BLACK); canvas.drawText(score + "分", mWidth / 2, mHeight / 2, mTextPaint); mTextPaint.setTextSize(40); if (crrentLevel != null) { canvas.drawText(crrentLevel, mWidth / 2, mHeight / 2 + 50, mTextPaint); } float section = currentCount / maxCount; if (section <= 1.0f / 3.0f) { if (section != 0.0f) { mPaint.setColor(SECTION_COLORS[0]); } else { mPaint.setColor(Color.TRANSPARENT); } } else { int count = (section <= 1.0f / 3.0f * 2) ? 2 : 3; int[] colors = new int[count]; System.arraycopy(SECTION_COLORS, 0, colors, 0, count); float[] positions = new float[count]; if (count == 2) { positions[0] = 0.0f; positions[1] = 1.0f - positions[0]; } else { positions[0] = 0.0f; positions[1] = (maxCount / 3) / currentCount; positions[2] = 1.0f - positions[0] * 2; } positions[positions.length - 1] = 1.0f; LinearGradient shader = new LinearGradient(3, 3, (mWidth - 3) * section, mHeight - 3, colors, null, Shader.TileMode.MIRROR); mPaint.setShader(shader); } canvas.drawArc(rectBlackBg, 180, section * 360, false, mPaint); } private void initPaint() { mPaint.setAntiAlias(true); mPaint.setStrokeWidth((float) 40.0); mPaint.setStyle(Style.STROKE); mPaint.setStrokeCap(Cap.ROUND); mPaint.setColor(Color.TRANSPARENT); mTextPaint.setAntiAlias(true); mTextPaint.setStrokeWidth((float) 3.0); mTextPaint.setTextAlign(Paint.Align.CENTER); mTextPaint.setTextSize(50); mTextPaint.setColor(Color.BLACK); } private int dipToPx(int dip) { float scale = getContext().getResources().getDisplayMetrics().density; return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1)); } public int getScore() { return score; } public String getCrrentLevel() { return crrentLevel; } public void setCrrentLevel(String crrentLevel) { this.crrentLevel = crrentLevel; } public float getMaxCount() { return maxCount; } public float getCurrentCount() { return currentCount; } public void setScore(int score) { this.score = score; if (score == 100) { this.crrentLevel = ALARM_LEVEL[0]; } else if (score >= 70 && score < 100) { this.crrentLevel = ALARM_LEVEL[1]; } else if (score >= 30 && score < 70) { this.crrentLevel = ALARM_LEVEL[2]; } else { this.crrentLevel = ALARM_LEVEL[3]; } invalidate(); } /*** * 设置最大的进度值 * * @param maxCount */ public void setMaxCount(float maxCount) { this.maxCount = maxCount; } /*** * 设置当前的进度值 * * @param currentCount */ public void setCurrentCount(float currentCount) { this.currentCount = currentCount > maxCount ? maxCount : currentCount; invalidate(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) { mWidth = widthSpecSize; } else { mWidth = 0; } if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) { mHeight = dipToPx(15); } else { mHeight = heightSpecSize; } setMeasuredDimension(mWidth, mHeight); } }

Demo:http://xiazai.jb51.net/201507/yuanma/ProgressViewDemo(jb51.net).rar

以上内容就是实现android自定义进度条渐变圆形的代码,希望对大家有所帮助。

时间: 2024-07-31 13:03:50

android自定义进度条渐变圆形的相关文章

android自定义进度条渐变圆形_Android

在安全卫生上,经常看到有圆形的进度条在转动,效果非常好看,于是就尝试去实现一下,具体实现过程不多说了,直接上效果图,先炫耀下. 效果图: 分析:比较常见于扫描结果.进度条等场景 利用canvas.drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)绘制圆弧 Paint的一些属性定义粗细.颜色.样式等 LinearGradient实现颜色的线型渐变 同样的道理,可以画出长条进度

Android自定义进度条的圆角横向进度条实例详解_Android

1.本文将向你介绍自定义进度条的写法,比较简单,但还是有些知识点是需要注意的: invalidate()方法 RectF方法的应用 onMeasure方法的应用 2.原理 画3层圆角矩形,底层为黑色,第二层为灰色,最上一层为进度条颜色,示例图如下: 3.效果图   实现圆角进度条还有很多其他方法,比如在Progress控件里填充圆角图片,通过拉伸图片来达到预想的效果,虽然听起来很简单,但实现起来还是有些麻烦的. 4.解说方法 (1)invalidate()方法 invalidate()是用来刷新

Android自定义进度条的圆角横向进度条实例详解

1.本文将向你介绍自定义进度条的写法,比较简单,但还是有些知识点是需要注意的: invalidate()方法 RectF方法的应用 onMeasure方法的应用 2.原理 画3层圆角矩形,底层为黑色,第二层为灰色,最上一层为进度条颜色,示例图如下: 3.效果图 实现圆角进度条还有很多其他方法,比如在Progress控件里填充圆角图片,通过拉伸图片来达到预想的效果,虽然听起来很简单,但实现起来还是有些麻烦的. 4.解说方法 (1)invalidate()方法 invalidate()是用来刷新Vi

android自定义进度条渐变色View的实例代码

这个自定义的view,完全脱离了android自带的ProgressView,并且没使用一张图片,这样就能更好的降低程序代码上的耦合性! 下面我贴出代码  ,大概讲解一下实现思路吧! package com.spring.progressview; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Linear

android自定义进度条渐变色View的实例代码_Android

最近在公司,项目不是很忙了,偶尔看见一个兄台在CSDN求助,帮忙要一个自定义的渐变色进度条,我当时看了一下进度条,感觉挺漂亮的,就尝试的去自定义view实现了一个,废话不说,先上图吧!   这个自定义的view,完全脱离了android自带的ProgressView,并且没使用一张图片,这样就能更好的降低程序代码上的耦合性! 下面我贴出代码  ,大概讲解一下实现思路吧! 复制代码 代码如下: package com.spring.progressview; import android.cont

Android编程实现自定义进度条颜色的方法_Android

本文实例讲述了Android编程实现自定义进度条颜色的方法.分享给大家供大家参考,具体如下: android 自定义进度条颜色 先看图 基于产品经理各种自定义需求,经过查阅了解,下面是自己对android自定义进度条的学习过程! 这个没法了只能看源码了,还好下载了源码, sources\base\core\res\res\  下应有尽有,修改进度条颜色只能找progress ,因为是改变样式,首先找styles.xml 找到xml后,进去找到 <style name="Widget.Pro

Android常用进度条效果分享_Android

先看看效果: activity_main.xml主页布局就2个button,分别弹出不同效果的2个进度条 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layou

Android实现带数字的圆形进度条(自定义进度条)

开发 设计搞了一个带圆形进度的进度条,在GitHub上逛了一圈,发现没有,自己撸吧. 先看界面效果: 主要思路是写一个继承ProgressBar的自定义View,不废话,直接上代码: package com.fun.progressbarwithnumber; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.g

自定义Android六边形进度条(附源码)_Android

本文实例讲述了Android自定义圆形进度条,分享给大家供大家参考.具体如下: 大家也可以参考这两篇文章进行学习: <自定义Android圆形进度条(附源码)>   <Android带进度的圆形进度条> 运行效果截图如下: 主要代码: package com.sxc.hexagonprogress; import java.util.Random; import android.content.Context; import android.content.res.ColorSta