Android按钮式进度条

  1 package com.example.progress.demo;
  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.Paint.FontMetrics;
 10 import android.graphics.RectF;
 11 import android.util.AttributeSet;
 12 import android.util.Log;
 13 import android.view.MotionEvent;
 14 import android.view.View;
 15 
 16 import com.example.firstapp.R;
 17 
 18 @SuppressLint("DrawAllocation")
 19 public class ProgressButton extends View {
 20     private FontMetrics fm;
 21     private int progress = 0;
 22     private int textColor = Color.WHITE;
 23     private Paint paint;
 24     private float textSize = 10;
 25     private int foreground;
 26     private int backgroundColor;
 27     private String text;
 28     private int max = 100;
 29     private int corner = 5;// 圆角的弧度
 30     private OnProgressButtonClickListener buttonClickListener;
 31 
 32     public ProgressButton(Context context, AttributeSet attrs) {
 33         super(context, attrs);
 34         init(context, attrs);
 35     }
 36 
 37     public ProgressButton(Context context, AttributeSet attrs, int defStyle) {
 38         super(context, attrs, defStyle);
 39         init(context, attrs);
 40     }
 41 
 42     private void init(Context context, AttributeSet attrs) {
 43         TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ProgressButton);
 44         this.backgroundColor = typedArray.getInteger(R.styleable.ProgressButton_background, Color.parseColor("#C6C6C6"));
 45         this.foreground = typedArray.getInteger(R.styleable.ProgressButton_foreground,  Color.rgb(20,131,214));
 46         this.textColor = typedArray.getInteger(R.styleable.ProgressButton_textcolor, Color.WHITE);
 47         this.max = typedArray.getInteger(R.styleable.ProgressButton_max, 100);
 48         this.progress = typedArray.getInteger(R.styleable.ProgressButton_progress, 0);
 49         this.text = typedArray.getString(R.styleable.ProgressButton_text);
 50         this.textSize = typedArray.getDimension(R.styleable.ProgressButton_textSize, 20);
 51         typedArray.recycle();
 52     }
 53 
 54     @Override
 55     protected void onDraw(Canvas canvas) {
 56         super.onDraw(canvas);
 57         paint = new Paint();
 58         paint.setAntiAlias(true);
 59         paint.setStrokeWidth(5);
 60 
 61         /**
 62          * 绘制背景
 63          */
 64         RectF oval = new RectF(0, 0, getWidth(), getHeight());
 65 
 66         paint.setColor(this.backgroundColor);
 67         canvas.drawRoundRect(oval, corner, corner, paint);
 68 
 69         /***
 70          * 绘制进度值
 71          */
 72         
 73         paint.setColor(foreground);
 74         if (progress <= corner) {
 75             oval = new RectF(0, corner - progress, getWidth() * this.progress / this.max, getHeight()
 76                     - corner + progress);
 77             canvas.drawRoundRect(oval, progress,progress, paint);
 78         } else {
 79             oval = new RectF(0, 0, getWidth() * this.progress / this.max, getHeight());
 80             canvas.drawRoundRect(oval, corner, corner, paint);
 81         }
 82 
 83         /***
 84          * 绘制文本
 85          */
 86         if ("".equals(text) || text == null) {
 87             return;
 88         }
 89         paint.setTextSize(this.textSize);
 90         fm = paint.getFontMetrics();
 91         paint.setColor(this.textColor);
 92 
 93         float textCenterVerticalBaselineY = getHeight() / 2 - fm.descent + (fm.descent - fm.ascent) / 2;
 94         canvas.drawText(this.text, (getMeasuredWidth() - paint.measureText(this.text)) / 2, textCenterVerticalBaselineY,
 95                 paint);
 96 
 97         
 98     }
 99 
100     /**
101      * 设置最大值
102      * 
103      * @param max
104      */
105     public void setMax(int max) {
106         this.max = max;
107     }
108 
109     /**
110      * 设置文本提示信息
111      * 
112      * @param text
113      */
114     public void setText(String text) {
115         this.text = text;
116     }
117 
118     /**
119      * 设置进度条的颜色值
120      * 
121      * @param color
122      */
123     public void setForeground(int color) {
124         this.foreground = color;
125     }
126 
127     /**
128      * 设置进度条的背景色
129      */
130     public void setBackgroundColor(int color) {
131         this.backgroundColor = color;
132     }
133 
134     /***
135      * 设置文本的大小
136      */
137     public void setTextSize(int size) {
138         this.textSize = size;
139     }
140 
141     /**
142      * 设置文本的颜色值
143      * 
144      * @param color
145      */
146     public void setTextColor(int color) {
147         this.textColor = color;
148     }
149 
150     /**
151      * 设置进度值
152      * 
153      * @param progress
154      */
155     public void setProgress(int progress) {
156         if(progress>max){
157             return;
158         }
159         this.progress=progress;
160         //设置进度之后,要求UI强制进行重绘
161         postInvalidate();
162     }
163     
164     public int getMax(){
165         return max;
166     }
167     
168     public int getProgress(){
169         return progress;
170     }
171     
172     @SuppressLint("ClickableViewAccessibility")
173     @Override
174     public boolean onTouchEvent(MotionEvent event) {
175         switch (event.getAction()) {
176         case MotionEvent.ACTION_UP:
177             buttonClickListener.onClickListener();
178             break;
179         default:
180             break;
181         }
182         return true;
183     }
184     
185     public void setOnProgressButtonClickListener(OnProgressButtonClickListener clickListener){
186         buttonClickListener = clickListener;
187     }
188     
189     public interface OnProgressButtonClickListener{
190         public void onClickListener();
191     }
192
<declare-styleable name="ProgressButton">
  <attr name="background" format="color" />
  <attr name="foreground" format="color" />
  <attr name="textcolor" format="color" />
  <attr name="max" />
  <attr name="progress" />
  <attr name="textSize" />
  <attr name="text" format="string" />
</declare-styleable>

  

193 }


 

 

posted on 2015-05-28 11:07  I love Android  阅读(3682)  评论(3编辑  收藏  举报