android自定义进度条渐变圆形

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

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

效果图:

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

利用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自定义进度条渐变圆形的代码,希望对大家有所帮助。

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

老生常谈Android HapticFeedback(震动反馈)

下面小编就为大家带来一篇老生常谈Android HapticFeedback(震动反馈)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详谈OnTouchListener与OnGestureListener的区别

下面小编就为大家带来一篇详谈OnTouchListener与OnGestureListener的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android仿知乎悬浮功能按钮FloatingActionButton效果

前段时间在看属性动画,恰巧这个按钮的效果可以用属性动画实现,下面通过本文给大家分享adroid仿知乎悬浮功能按钮FloatingActionButton效果,需要的朋友参考下吧
收藏 0 赞 0 分享

解决Android V7后自定义Toolbar、ActionBar左侧有空白问题

这篇文章主要介绍的Android V7后自定义Toolbar、ActionBar左侧有空白问题的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android常见控件使用详解

这篇文章主要为大家详细介绍了Android常见控件的使用方法,包括ProgressBar进度条控件、AlertDialog对话框控件等,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android实现简洁的APP更新dialog数字进度条

这篇文章主要为大家详细介绍了Android实现简洁的APP更新dialog数字进度条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 判断当前语言环境是否是中文环境

本文主要介绍了Android 判断当前语言环境是否是中文环境的方法。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

详谈Android中Matrix的set、pre、post的区别

下面小编就为大家带来一篇详谈Android中Matrix的set、pre、post的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现登录界面记住密码的存储

这篇文章主要为大家详细介绍了Android SharedPreferrences实现登录界面记住密码的存储,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 使用SharedPreferrences储存密码登录界面记住密码功能

Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences, 其采用了Map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入,下面通过实例代码给大家讲解下,需要的朋友参考下吧
收藏 0 赞 0 分享
查看更多