Android 高手进阶自定义圆形进度条

背景介绍

Android 开发中,我们经常遇到各种各样绚丽的控件,所以,依靠我们Android本身所带的控件是远远不够的,很多时候需要我们自己定义控件,在开发的过程中,我们公司遇到了一种需要自己写的一个自定义带进度的圆形进度条,看起来非常的绚丽,当然还有一些其他的,比如:水纹形的圆形进度条等效果都是非常nice的。如果哪位朋友有实现,希望分享出来,我也好学习学习。好了多的不说,接下来,我们就来看看来如何实现圆形进度条。

原文地址:http://blog.csdn.net/xiaanming/article/details/10298163

 

一:先上效果图

                            

 

 

二:实例代码

1.自定义属性

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <declare-styleable name="RoundProgressBar">  
  5.         <attr name="roundColor" format="color" />  
  6.         <attr name="roundProgressColor" format="color" />  
  7.         <attr name="roundWidth" format="dimension"></attr>  
  8.         <attr name="textColor" format="color" />  
  9.         <attr name="textSize" format="dimension" />  
  10.         <attr name="max" format="integer"></attr>  
  11.         <attr name="textIsDisplayable" format="boolean"></attr>  
  12.         <attr name="style">  
  13.             <enum name="STROKE" value="0"></enum>  
  14.             <enum name="FILL" value="1"></enum>  
  15.         </attr>  
  16.     </declare-styleable>  
  17.   
  18. </resources></span>  


ps:自定义属性呢,其实大家或许不是很明白,有些小伙伴们或许知道,我这样用,可以调用它的一个值,可以获取值,或者说赋予值,比如:你用Android 系统本身所带的控件,textview其他等,你知道他有height,width,textsize,textcolor等,这些属性,你能赋值,但是有去理解过怎样去获取值么,如果不了解,没关系,我这有一篇鸿洋写的比较详细,推荐给你:http://blog.csdn.net/lmj623565791/article/details/45022631,还在等什么,赶紧戳进去,带你装逼带你飞。

 

 

2.自定义控件

源码1:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">package com.example.testdemo.view;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.content.Context;  
  5. import android.content.res.TypedArray;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Color;  
  8. import android.graphics.Paint;  
  9. import android.graphics.RectF;  
  10. import android.graphics.Typeface;  
  11. import android.util.AttributeSet;  
  12. import android.view.View;  
  13.   
  14. import com.example.testdemo.R;  
  15.   
  16. /** 
  17.  * 仿iphone带进度的进度条,线程安全的View,可直接在线程中更新进度 
  18.  *  
  19.  * @author zengtao 2015年5月12日下午7:43:32 
  20.  * 
  21.  * 
  22.  */  
  23. @SuppressLint("DrawAllocation")  
  24. public class RoundProgressBar extends View {  
  25.     /** 
  26.      * 画笔对象的引用 
  27.      */  
  28.     private Paint paint;  
  29.   
  30.     /** 
  31.      * 圆环的颜色 
  32.      */  
  33.     private int roundColor;  
  34.   
  35.     /** 
  36.      * 圆环进度的颜色 
  37.      */  
  38.     private int roundProgressColor;  
  39.   
  40.     /** 
  41.      * 中间进度百分比的字符串的颜色 
  42.      */  
  43.     private int textColor;  
  44.   
  45.     /** 
  46.      * 中间进度百分比的字符串的字体 
  47.      */  
  48.     private float textSize;  
  49.   
  50.     /** 
  51.      * 圆环的宽度 
  52.      */  
  53.     private float roundWidth;  
  54.   
  55.     /** 
  56.      * 最大进度 
  57.      */  
  58.     private int max;  
  59.   
  60.     /** 
  61.      * 当前进度 
  62.      */  
  63.     private int progress;  
  64.     /** 
  65.      * 是否显示中间的进度 
  66.      */  
  67.     private boolean textIsDisplayable;  
  68.   
  69.     /** 
  70.      * 进度的风格,实心或者空心 
  71.      */  
  72.     private int style;  
  73.   
  74.     public static final int STROKE = 0;  
  75.     public static final int FILL = 1;  
  76.   
  77.     public RoundProgressBar(Context context) {  
  78.         this(context, null);  
  79.     }  
  80.   
  81.     public RoundProgressBar(Context context, AttributeSet attrs) {  
  82.         this(context, attrs, 0);  
  83.     }  
  84.   
  85.     public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {  
  86.         super(context, attrs, defStyle);  
  87.   
  88.         paint = new Paint();  
  89.   
  90.         TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);  
  91.   
  92.         // 获取自定义属性和默认值  
  93.         roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.rgb(228, 232, 237));  
  94.         roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.rgb(216, 6, 7));  
  95.         textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.rgb(216, 6, 7));  
  96.         textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 18);  
  97.         roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 3);  
  98.         max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);  
  99.         textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);  
  100.         style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);  
  101.   
  102.         mTypedArray.recycle();  
  103.     }  
  104.   
  105.     @Override  
  106.     protected void onDraw(Canvas canvas) {  
  107.         super.onDraw(canvas);  
  108.   
  109.         /** 
  110.          * 1.画最外层的大圆环 
  111.          */  
  112.         int centre = getWidth() / 2; // 获取圆心的x坐标  
  113.         int radius = (int) (centre - roundWidth / 2); // 圆环的半径  
  114.         paint.setColor(roundColor); // 设置圆环的颜色  
  115.         paint.setStyle(Paint.Style.STROKE); // 设置空心  
  116.         paint.setStrokeWidth(roundWidth); // 设置圆环的宽度  
  117.         paint.setAntiAlias(true); // 消除锯齿  
  118.         canvas.drawCircle(centre, centre, radius, paint); // 画出圆环  
  119.   
  120.         /** 
  121.          * 2.画进度百分比 
  122.          */  
  123.         paint.setStrokeWidth(0);  
  124.         paint.setColor(textColor);  
  125.         paint.setTextSize(textSize);  
  126.         paint.setTypeface(Typeface.DEFAULT); // 设置字体  
  127.         // 中间的进度百分比,先转换成float在进行除法运算,不然都为0  
  128.         int percent = (int) (((float) progress / (float) max) * 100);  
  129.   
  130.         float textWidth = paint.measureText(percent + "%"); // 测量字体宽度,我们需要根据字体的宽度设置在圆环中间  
  131.   
  132.         if (textIsDisplayable && style == STROKE) {  
  133.              canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize/2, paint); //画出进度百分比    
  134.         }  
  135.   
  136.         /** 
  137.          * 3.画圆弧 ,画圆环的进度 
  138.          */  
  139.   
  140.         // 设置进度是实心还是空心  
  141.         paint.setStrokeWidth(roundWidth); // 设置圆环的宽度  
  142.         paint.setColor(roundProgressColor); // 设置进度的颜色  
  143.         RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限  
  144.   
  145.         switch (style) {  
  146.         case STROKE: {  
  147.             paint.setStyle(Paint.Style.STROKE);  
  148.             canvas.drawArc(oval, -90, 360 * progress / max, false, paint); // 根据进度画圆弧  
  149.             break;  
  150.         }  
  151.         case FILL: {  
  152.             paint.setStyle(Paint.Style.FILL_AND_STROKE);  
  153.             if (progress != 0)  
  154.                 canvas.drawArc(oval, -90, 360 * progress / max, true, paint); // 根据进度画圆弧  
  155.             break;  
  156.         }  
  157.         }  
  158.   
  159.     }  
  160.   
  161.     public synchronized int getMax() {  
  162.         return max;  
  163.     }  
  164.   
  165.     /** 
  166.      * 设置进度的最大值 
  167.      *  
  168.      * @param max 
  169.      */  
  170.     public synchronized void setMax(int max) {  
  171.         if (max < 0) {  
  172.             throw new IllegalArgumentException("max not less than 0");  
  173.         }  
  174.         this.max = max;  
  175.     }  
  176.   
  177.     /** 
  178.      * 获取进度.需要同步 
  179.      *  
  180.      * @return 
  181.      */  
  182.     public synchronized int getProgress() {  
  183.         return progress;  
  184.     }  
  185.   
  186.     /** 
  187.      * 设置进度,此为线程安全控件,由于考虑多线的问题,需要同步 刷新界面调用postInvalidate()能在非UI线程刷新 
  188.      *  
  189.      * @param progress 
  190.      */  
  191.     public synchronized void setProgress(int progress) {  
  192.         if (progress < 0) {  
  193.             throw new IllegalArgumentException("progress not less than 0");  
  194.         }  
  195.         if (progress > max) {  
  196.             progress = max;  
  197.         }  
  198.         if (progress <= max) {  
  199.             this.progress = progress;  
  200.             postInvalidate();  
  201.         }  
  202.   
  203.     }  
  204.   
  205.     public int getCricleColor() {  
  206.         return roundColor;  
  207.     }  
  208.   
  209.     public void setCricleColor(int cricleColor) {  
  210.         this.roundColor = cricleColor;  
  211.     }  
  212.   
  213.     public int getCricleProgressColor() {  
  214.         return roundProgressColor;  
  215.     }  
  216.   
  217.     public void setCricleProgressColor(int cricleProgressColor) {  
  218.         this.roundProgressColor = cricleProgressColor;  
  219.     }  
  220.   
  221.     public int getTextColor() {  
  222.         return textColor;  
  223.     }  
  224.   
  225.     public void setTextColor(int textColor) {  
  226.         this.textColor = textColor;  
  227.     }  
  228.   
  229.     public float getTextSize() {  
  230.         return textSize;  
  231.     }  
  232.   
  233.     public void setTextSize(float textSize) {  
  234.         this.textSize = textSize;  
  235.     }  
  236.   
  237.     public float getRoundWidth() {  
  238.         return roundWidth;  
  239.     }  
  240.   
  241.     public void setRoundWidth(float roundWidth) {  
  242.         this.roundWidth = roundWidth;  
  243.     }  
  244.   
  245.     public int getStyle() {  
  246.         return style;  
  247.     }  
  248.       
  249.     public void setStyle(int style) {  
  250.         this.style = style;  
  251.     }  
  252. }  
  253. </span>  


源码2:

 

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">package com.example.testdemo.view;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.content.Context;  
  5. import android.content.res.TypedArray;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Color;  
  8. import android.graphics.Paint;  
  9. import android.graphics.RectF;  
  10. import android.graphics.Typeface;  
  11. import android.util.AttributeSet;  
  12. import android.view.View;  
  13.   
  14. import com.example.testdemo.R;  
  15.   
  16. /** 
  17.  * 仿iphone带进度的进度条,线程安全的View,可直接在线程中更新进度 
  18.  *  
  19.  * @author zengtao 2015年5月12日下午7:43:32 
  20.  * 
  21.  * 
  22.  */  
  23. @SuppressLint("DrawAllocation")  
  24. public class RoundProgressBar2 extends View {  
  25.     /** 
  26.      * 画笔对象的引用 
  27.      */  
  28.     private Paint paint;  
  29.   
  30.     /** 
  31.      * 圆环的颜色 
  32.      */  
  33.     private int roundColor;  
  34.   
  35.     /** 
  36.      * 圆环进度的颜色 
  37.      */  
  38.     private int roundProgressColor;  
  39.   
  40.     /** 
  41.      * 中间进度百分比的字符串的颜色 
  42.      */  
  43.     private int textColor;  
  44.   
  45.     /** 
  46.      * 中间进度百分比的字符串的字体 
  47.      */  
  48.     private float textSize;  
  49.   
  50.     /** 
  51.      * 圆环的宽度 
  52.      */  
  53.     private float roundWidth;  
  54.   
  55.     /** 
  56.      * 最大进度 
  57.      */  
  58.     private int max;  
  59.   
  60.     /** 
  61.      * 当前进度 
  62.      */  
  63.     private int progress;  
  64.     /** 
  65.      * 是否显示中间的进度 
  66.      */  
  67.     private boolean textIsDisplayable;  
  68.   
  69.     /** 
  70.      * 进度的风格,实心或者空心 
  71.      */  
  72.     private int style;  
  73.   
  74.     public static final int STROKE = 0;  
  75.     public static final int FILL = 1;  
  76.   
  77.     public RoundProgressBar2(Context context) {  
  78.         this(context, null);  
  79.     }  
  80.   
  81.     public RoundProgressBar2(Context context, AttributeSet attrs) {  
  82.         this(context, attrs, 0);  
  83.     }  
  84.   
  85.     public RoundProgressBar2(Context context, AttributeSet attrs, int defStyle) {  
  86.         super(context, attrs, defStyle);  
  87.   
  88.         paint = new Paint();  
  89.   
  90.         TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);  
  91.   
  92.         // 获取自定义属性和默认值  
  93.         roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.rgb(228, 232, 237));  
  94.         roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.rgb(216, 6, 7));  
  95.         textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.rgb(216, 6, 7));  
  96.         textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 18);  
  97.         roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 3);  
  98.         max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);  
  99.         textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);  
  100.         style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);  
  101.   
  102.         mTypedArray.recycle();  
  103.     }  
  104.   
  105.     @Override  
  106.     protected void onDraw(Canvas canvas) {  
  107.         super.onDraw(canvas);  
  108.   
  109.         /** 
  110.          * 画最外层的大圆环 
  111.          */  
  112.         int centre = getWidth() / 2; // 获取圆心的x坐标  
  113.         int radius = (int) (centre - roundWidth / 2); // 圆环的半径  
  114.         paint.setColor(roundColor); // 设置圆环的颜色  
  115.         paint.setStyle(Paint.Style.STROKE); // 设置空心  
  116.         paint.setStrokeWidth(roundWidth); // 设置圆环的宽度  
  117.         paint.setAntiAlias(true); // 消除锯齿  
  118.         canvas.drawCircle(centre, centre, radius, paint); // 画出圆环  
  119.   
  120.         /** 
  121.          * 画进度百分比 
  122.          */  
  123.         paint.setStrokeWidth(0);  
  124.         paint.setColor(textColor);  
  125.         paint.setTextSize(textSize);  
  126.         paint.setTypeface(Typeface.DEFAULT); // 设置字体  
  127.         // int percent = (int) (((float) progress / (float) max) * 100); //  
  128.         // 中间的进度百分比,先转换成float在进行除法运算,不然都为0  
  129.   
  130.         float textWidth = paint.measureText("抢"); // 测量字体宽度,我们需要根据字体的宽度设置在圆环中间  
  131.   
  132.         if (textIsDisplayable && style == STROKE) {  
  133.             canvas.drawText("抢", centre - textWidth / 2, centre + textSize / 2 - 4, paint); // 画出进度百分比  
  134.         }  
  135.   
  136.         /** 
  137.          * 画圆弧 ,画圆环的进度 
  138.          */  
  139.   
  140.         // 设置进度是实心还是空心  
  141.         paint.setStrokeWidth(roundWidth); // 设置圆环的宽度  
  142.         paint.setColor(roundProgressColor); // 设置进度的颜色  
  143.         RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限  
  144.   
  145.         switch (style) {  
  146.         case STROKE: {  
  147.             paint.setStyle(Paint.Style.STROKE);  
  148.             canvas.drawArc(oval, -90, 360 * progress / max, false, paint); // 根据进度画圆弧  
  149.             break;  
  150.         }  
  151.         case FILL: {  
  152.             paint.setStyle(Paint.Style.FILL_AND_STROKE);  
  153.             if (progress != 0)  
  154.                 canvas.drawArc(oval, -90, 360 * progress / max, true, paint); // 根据进度画圆弧  
  155.             break;  
  156.         }  
  157.         }  
  158.   
  159.     }  
  160.   
  161.     public synchronized int getMax() {  
  162.         return max;  
  163.     }  
  164.   
  165.     /** 
  166.      * 设置进度的最大值 
  167.      *  
  168.      * @param max 
  169.      */  
  170.     public synchronized void setMax(int max) {  
  171.         if (max < 0) {  
  172.             throw new IllegalArgumentException("max not less than 0");  
  173.         }  
  174.         this.max = max;  
  175.     }  
  176.   
  177.     /** 
  178.      * 获取进度.需要同步 
  179.      *  
  180.      * @return 
  181.      */  
  182.     public synchronized int getProgress() {  
  183.         return progress;  
  184.     }  
  185.   
  186.     /** 
  187.      * 设置进度,此为线程安全控件,由于考虑多线的问题,需要同步 刷新界面调用postInvalidate()能在非UI线程刷新 
  188.      *  
  189.      * @param progress 
  190.      */  
  191.     public synchronized void setProgress(int progress) {  
  192.         if (progress < 0) {  
  193.             throw new IllegalArgumentException("progress not less than 0");  
  194.         }  
  195.         if (progress > max) {  
  196.             progress = max;  
  197.         }  
  198.         if (progress <= max) {  
  199.             this.progress = progress;  
  200.             postInvalidate();  
  201.         }  
  202.   
  203.     }  
  204.   
  205.     public int getCricleColor() {  
  206.         return roundColor;  
  207.     }  
  208.   
  209.     public void setCricleColor(int cricleColor) {  
  210.         this.roundColor = cricleColor;  
  211.     }  
  212.   
  213.     public int getCricleProgressColor() {  
  214.         return roundProgressColor;  
  215.     }  
  216.   
  217.     public void setCricleProgressColor(int cricleProgressColor) {  
  218.         this.roundProgressColor = cricleProgressColor;  
  219.     }  
  220.   
  221.     public int getTextColor() {  
  222.         return textColor;  
  223.     }  
  224.   
  225.     public void setTextColor(int textColor) {  
  226.         this.textColor = textColor;  
  227.     }  
  228.   
  229.     public float getTextSize() {  
  230.         return textSize;  
  231.     }  
  232.   
  233.     public void setTextSize(float textSize) {  
  234.         this.textSize = textSize;  
  235.     }  
  236.   
  237.     public float getRoundWidth() {  
  238.         return roundWidth;  
  239.     }  
  240.   
  241.     public void setRoundWidth(float roundWidth) {  
  242.         this.roundWidth = roundWidth;  
  243.     }  
  244.   
  245. }  
  246. </span>  

 

 

ps:以上两份自定义源码,其实你仔细看,差别不是很大,就改变了一个地方,这个更改的地方,其实是在我们现在的项目中所遇到的,所以,发不这个圆形控件,我想你以后会用到的,值得收藏。

三.具体调用

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">package com.example.testdemo;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.app.Activity;  
  5. import android.graphics.Color;  
  6. import android.os.Bundle;  
  7. import android.os.Handler;  
  8. import android.os.Message;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12.   
  13. import com.example.testdemo.view.RoundProgressBar;  
  14. import com.example.testdemo.view.RoundProgressBar2;  
  15.   
  16. /** 
  17.  * 主界面 
  18.  * @author zengtao 2015年6月10日 下午4:02:13 
  19.  * 
  20.  */  
  21. public class MainActivity extends Activity {  
  22.     private RoundProgressBar2 r3;  
  23.     private RoundProgressBar r1, r2, r4, r5;  
  24.     private int pro1 = 80; // 想要显示的进度值:如项目中,从服务器返回的数据  
  25.     private int progress = 0;  
  26.     private boolean flag = false;  
  27.     private MyThread thread1;  
  28.     private MyThread2 thread2;  
  29.     private Button start;  
  30.   
  31.     @Override  
  32.     protected void onCreate(Bundle savedInstanceState) {  
  33.         super.onCreate(savedInstanceState);  
  34.         setContentView(R.layout.activity_main);  
  35.   
  36.         initView();  
  37.         setRoundAttribute(); // 可设置可不设置  
  38.     }  
  39.   
  40.     /** 
  41.      * 开始方法 
  42.      */  
  43.     private void start() {  
  44.         // 线程1  
  45.         thread1 = new MyThread();  
  46.         thread1.start();  
  47.   
  48.         // 线程2  
  49.         thread2 = new MyThread2();  
  50.         thread2.start();  
  51.     }  
  52.   
  53.     /** 
  54.      * 初始化控件 
  55.      */  
  56.     private void initView() {  
  57.         r1 = (RoundProgressBar) findViewById(R.id.prpgress1);  
  58.         r2 = (RoundProgressBar) findViewById(R.id.prpgress2);  
  59.         r3 = (RoundProgressBar2) findViewById(R.id.prpgress3);  
  60.         r4 = (RoundProgressBar) findViewById(R.id.prpgress4);  
  61.         r5 = (RoundProgressBar) findViewById(R.id.prpgress5);  
  62.   
  63.         start = (Button) findViewById(R.id.start);  
  64.         start.setOnClickListener(listener);  
  65.     }  
  66.   
  67.     /** 
  68.      * 设置圆的属性 
  69.      */  
  70.     private void setRoundAttribute() {  
  71.         r1.setRoundWidth(10);  
  72.         r1.setTextColor(Color.parseColor("#00ff00"));  
  73.         r1.setCricleColor(Color.parseColor("#ff0000"));  
  74.   
  75.         r2.setRoundWidth(20);  
  76.         r2.setTextColor(Color.parseColor("#0000ff"));  
  77.         r2.setCricleColor(Color.parseColor("#ff00ff"));  
  78.   
  79.         r4.setRoundWidth(20);  
  80.         r4.setTextColor(Color.parseColor("#ff00ff"));  
  81.         r4.setCricleColor(Color.parseColor("#ff0000"));  
  82.         r4.setStyle(0);  
  83.   
  84.         r4.setRoundWidth(20);  
  85.         r4.setTextColor(Color.parseColor("#000000"));  
  86.         r4.setCricleColor(Color.parseColor("#ffff00"));  
  87.         r4.setStyle(1);  
  88.     }  
  89.   
  90.     /** 
  91.      * 点击事件 
  92.      */  
  93.     OnClickListener listener = new OnClickListener() {  
  94.   
  95.         @Override  
  96.         public void onClick(View v) {  
  97.             if (v == start) {  
  98.                 start();  
  99.             }  
  100.         }  
  101.     };  
  102.   
  103.     /** 
  104.      * 用于更新ui 
  105.      */  
  106.     @SuppressLint("HandlerLeak")  
  107.     private Handler mHandler = new Handler() {  
  108.         public void handleMessage(android.os.Message msg) {  
  109.             final int x = msg.what;  
  110.             final int temp = (int) msg.obj;  
  111.             if (x == 0 * 1) {  
  112.                 if (pro1 - temp > 0) {  
  113.                     r1.setProgress(temp);  
  114.                     r2.setProgress(temp);  
  115.                     r3.setProgress(temp);  
  116.                     r4.setProgress(temp);  
  117.                     r5.setProgress(temp);  
  118.                 } else {  
  119.                     r1.setProgress(pro1);  
  120.                     r2.setProgress(pro1);  
  121.                     r3.setProgress(pro1);  
  122.                     r4.setProgress(pro1);  
  123.                     r5.setProgress(pro1);  
  124.                     thread1.stopThread();  
  125.                 }  
  126.             } else if (x == 0 * 2) {  
  127.                 if (pro1 - temp > 0) {  
  128.                     r1.setProgress(temp);  
  129.                     r2.setProgress(temp);  
  130.                     r3.setProgress(temp);  
  131.                     r4.setProgress(temp);  
  132.                     r5.setProgress(temp);  
  133.                 } else {  
  134.                     r1.setProgress(pro1);  
  135.                     r2.setProgress(pro1);  
  136.                     r3.setProgress(pro1);  
  137.                     r4.setProgress(pro1);  
  138.                     r5.setProgress(pro1);  
  139.                     thread2.stopThread();  
  140.                 }  
  141.             }  
  142.         };  
  143.     };  
  144.   
  145.     /** 
  146.      * 线程,控制进度动画 
  147.      * @author zengtao 2015年6月10日 下午4:31:11 
  148.      * 
  149.      */  
  150.     class MyThread extends Thread {  
  151.   
  152.         @Override  
  153.         public void run() {  
  154.             while (!flag) {  
  155.                 try {  
  156.                     progress += 1;  
  157.                     Message msg = new Message();  
  158.                     msg.what = 0 * 1;  
  159.                     msg.obj = progress;  
  160.                     Thread.sleep(100);  
  161.                     mHandler.sendMessage(msg);  
  162.                 } catch (InterruptedException e) {  
  163.                     e.printStackTrace();  
  164.                 }  
  165.             }  
  166.         }  
  167.   
  168.         public void stopThread() {  
  169.             flag = true;  
  170.         }  
  171.     }  
  172.   
  173.     /** 
  174.      * 线程,控制进度动画 
  175.      * @author zengtao 2015年6月10日 下午4:31:11 
  176.      * 
  177.      */  
  178.     class MyThread2 extends Thread {  
  179.   
  180.         @Override  
  181.         public void run() {  
  182.             while (!flag) {  
  183.                 try {  
  184.                     progress += 2;  
  185.                     Message msg = new Message();  
  186.                     msg.what = 0 * 2;  
  187.                     msg.obj = progress;  
  188.                     Thread.sleep(200);  
  189.                     mHandler.sendMessage(msg);  
  190.                 } catch (InterruptedException e) {  
  191.                     e.printStackTrace();  
  192.                 }  
  193.             }  
  194.         }  
  195.   
  196.         public void stopThread() {  
  197.             flag = true;  
  198.         }  
  199.     }  
  200.   
  201.     @Override  
  202.     protected void onDestroy() {  
  203.         super.onDestroy();  
  204.         if (thread1 != null) {  
  205.             thread1.stopThread();  
  206.         }  
  207.         if (thread2 != null) {  
  208.             thread2.stopThread();  
  209.         }  
  210.     }  
  211. }  
  212. </span>  


四.总结

实现以上步骤,就实现了一个安全可靠的自定义控件的实现,看起来简简单单,不说分分钟,但是稍微花点时间这就是你的。
 
posted @ 2017-03-12 19:07  天涯海角路  阅读(249)  评论(0)    收藏  举报