life02

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  197 随笔 :: 3 文章 :: 37 评论 :: 0 Trackbacks
http://hao3100590.iteye.com/blog/1317151


    
* 博客分类: Android开发

androidProgressBar播放器进度

最近由于需要,做了一个音乐播放控制view,在上面需要能

*控制播放

*显示剩余时间

*显示进度(整个view就是一个进度条)

*实现播放暂停,以及ProgressBar的第一二进度功能

开始想到用组合的方式实现,然后重写ProgressBar的方式实现,但是发现很困难而且文字显示也不行

最后只有自己动手写一个新的控制条

主要的原理就是绘制视图的时候控制onDraw,然后在上面画图片和文字

然后在进度变化的时候不断重绘View就可以了

先上图:






 
 
 其主要的View部分:

 
Java代码  收藏代码

   
1package com.hao;  
   
2.   
   
3import android.content.Context;  
   
4import android.graphics.Bitmap;  
   
5import android.graphics.BitmapFactory;  
   
6import android.graphics.Canvas;  
   
7import android.graphics.Color;  
   
8import android.graphics.Paint;  
   
9import android.graphics.PixelFormat;  
  
10import android.graphics.Rect;  
  
11import android.graphics.drawable.BitmapDrawable;  
  
12import android.graphics.drawable.Drawable;  
  
13import android.util.AttributeSet;  
  
14import android.util.Log;  
  
15import android.view.MotionEvent;  
  
16import android.view.View;  
  
17import android.view.View.MeasureSpec;  
  
18import android.view.View.OnClickListener;  
  
19import android.widget.ImageButton;  
  
20import android.widget.ImageView;  
  
21import android.widget.TextView;  
  
22.   
  
23public class ProgressButton extends View{  
  
24.     private Bitmap begin , bm_gray, bm_yellow, bm_second, end_gray, end_yellow, line,begin_gray;  
  
25.     private Bitmap pausePressedImg;  
  
26.     private Bitmap playPressedImg;  
  
27.     private int bitmapWidth = 0 , bitmapHeight = 0, btWidth = 0, btHeight = 0;    
  
28.     private int progress = 0, secondProgress = 0;  
  
29.     private double perLen = 0, max = 0, maxSize = 0;  
  
30.     private OnProgressChanged mOnProgressChanged;  
  
31.     private boolean isPlaying = false;  
  
32.     private Paint mTextPaint;  
  
33.     private String time = "00:00";  
  
34.     private int color = Color.BLUE;  
  
35.   
  
36.     public ProgressButton(Context context) {  
  
37.         super(context);  
  
38.         // TODO Auto-generated constructor stub  
  39.         init();  
  
40.     }  
  
41.       
  
42.     public ProgressButton(Context context, AttributeSet attrs, int defStyle){  
  
43.         super(context, attrs, defStyle);  
  
44.         init();  
  
45.     }  
  
46.       
  
47.     public ProgressButton(Context context, AttributeSet attrs){  
  
48.         super(context, attrs);  
  
49.         init();  
  
50.     }  
  
51.       
  
52.     private void init(){  
  
53.         begin = drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_left_yellow));  
  
54.         begin_gray = drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_left_gray));  
  
55.         bm_gray =  drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_gray));  
  
56.         bm_yellow =  drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_yellow));  
  
57.         bm_second =  drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_second_yellow));  
  
58.         end_gray =  drawableToBitmap(getResources().getDrawable( R.drawable.rectangle_right_gray));  
  
59.         end_yellow =  drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_right_yellow));  
  
60.         line = drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_line));  
  
61.         pausePressedImg = BitmapFactory.decodeResource(getResources(), R.drawable.pause_button_pressed);  
  
62.         playPressedImg = BitmapFactory.decodeResource(getResources(), R.drawable.play_button_pressed);  
  
63.         bitmapHeight = begin.getHeight();  
  
64.         bitmapWidth = begin.getWidth();  
  
65.         btWidth = pausePressedImg.getWidth();  
  
66.         btHeight = pausePressedImg.getHeight();  
  
67.         mTextPaint = new Paint();  
  
68.         mTextPaint.setAntiAlias(true);  
  
69.         mTextPaint.setTextSize(14);  
  
70.         mTextPaint.setColor(color);  
  
71.         setPadding(3333);  
  
72.     }  
  
73.       
  
74.     public static Bitmap drawableToBitmap(Drawable drawable) {  
  
75.         int width = drawable.getIntrinsicWidth();  
  
76.         int height = drawable.getIntrinsicHeight();  
  
77.         Bitmap bitmap = Bitmap.createBitmap(width, height, drawable  
  
78.                 .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
  
79.                 : Bitmap.Config.RGB_565);  
  
80.         Canvas canvas = new Canvas(bitmap);  
  
81.         drawable.setBounds(00, width, height);  
  
82.         drawable.draw(canvas);  
  
83.         return bitmap;  
  
84.     }     
  
85.       
  
86.       
  
87.       
  
88.     @Override  
  
89.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  
90.         // TODO Auto-generated method stub  
  91.         Log.e("*******""onMeasure");  
  
92.         setMeasuredDimension(measureWidth(widthMeasureSpec),  
  
93.                 measureHeight(heightMeasureSpec));  
  
94.         perLen = maxSize/max;   
  
95.     }  
  
96.   
  
97.     @Override  
  
98.     protected void onDraw(Canvas canvas) {  
  
99.         // TODO Auto-generated method stub  
 100.         Log.e("*******""onDraw");  
 
101.         int middle1 = (int) (progress*perLen), middle2 = (int) (secondProgress*perLen) ,end = (int) maxSize-4;  
 
102.         if(progress == 0 && secondProgress == 0){  
 
103.             //draw background  
 104.             canvas.drawBitmap(begin_gray, new Rect(0,0,bitmapWidth,bitmapHeight),   
 
105.                                     new Rect(00, bitmapWidth, bitmapHeight), null);  
 
106.             canvas.drawBitmap(bm_gray, new Rect(0,0,end-middle1,bitmapHeight),   
 
107.                     new Rect(bitmapWidth, 0, end, bitmapHeight), null);  
 
108.             canvas.drawBitmap(end_gray, new Rect(0,0,4,bitmapHeight),   
 
109.                     new Rect(end, 0, end+4, bitmapHeight), null);  
 
110.             //draw button and line  
 111.             canvas.drawBitmap(playPressedImg, new Rect(00, btWidth, btHeight),   
 
112.                     new Rect(00, btWidth, bitmapHeight), null);  
 
113.             canvas.drawBitmap(line, new Rect(002, bitmapHeight),   
 
114.                     new Rect(btWidth, 0, btWidth+2, bitmapHeight), null);  
 
115.             //draw time and line  
 116.             if(time.length() == 5){  
 
117.                 canvas.drawBitmap(line, new Rect(002, bitmapHeight),   
 
118.                         new Rect(end - 500, end-48, bitmapHeight), null);  
 
119.                 canvas.drawText("-"+time, end-45, bitmapHeight/2+5, mTextPaint);  
 
120.             }else{  
 
121.                 canvas.drawBitmap(line, new Rect(002, bitmapHeight),   
 
122.                         new Rect(end - 600, end-58, bitmapHeight), null);  
 
123.                 canvas.drawText("-"+time, end-55, bitmapHeight/2+5, mTextPaint);  
 
124.             }  
 
125.         }else{  
 
126.             //begin  
 127.             canvas.drawBitmap(begin, new Rect(0,0,bitmapWidth,bitmapHeight),   
 
128.                         new Rect(00, bitmapWidth, bitmapHeight), null);  
 
129.             canvas.drawBitmap(bm_yellow, new Rect(0,0,middle1-bitmapWidth,bitmapHeight),   
 
130.                         new Rect(bitmapWidth, 0, middle1, bitmapHeight), null);  
 
131.             //middle  
 132.             if(secondProgress != 0 && secondProgress > progress){  
 
133.                 canvas.drawBitmap(bm_second, new Rect(0,0,bitmapWidth,bitmapHeight),   
 
134.                         new Rect(middle1, 0, middle2, bitmapHeight), null);  
 
135.                 canvas.drawBitmap(bm_gray, new Rect(0,0,bitmapWidth,bitmapHeight),   
 
136.                         new Rect(middle2, 0, end, bitmapHeight), null);  
 
137.             }else{  
 
138.                 canvas.drawBitmap(bm_gray, new Rect(0,0,end-middle1,bitmapHeight),   
 
139.                         new Rect(middle1, 0, end, bitmapHeight), null);  
 
140.             }  
 
141.             //end  
 142.             canvas.drawBitmap(end_gray, new Rect(0,0,4,bitmapHeight),   
 
143.                     new Rect(end, 0, end+4, bitmapHeight), null);  
 
144.             if(middle2 >= end || middle1 >= end){  
 
145.                 canvas.drawBitmap(end_yellow, new Rect(0,0,4,bitmapHeight),   
 
146.                         new Rect(end, 0, end+4, bitmapHeight), null);  
 
147.             }  
 
148.             //draw button  
 149.             if(!isPlaying) {  
 
150.                 canvas.drawBitmap(playPressedImg, new Rect(00, btWidth, btHeight),   
 
151.                         new Rect(00, btWidth, bitmapHeight), null);  
 
152.             }else{  
 
153.                 canvas.drawBitmap(pausePressedImg, new Rect(00, btWidth, btHeight),   
 
154.                         new Rect(00, btWidth, bitmapHeight), null);  
 
155.             }  
 
156.             //draw line and time  
 157.             canvas.drawBitmap(line, new Rect(002, bitmapHeight),   
 
158.                     new Rect(btWidth, 0, btWidth+2, bitmapHeight), null);  
 
159.             if(time.length() == 5){  
 
160.                 canvas.drawBitmap(line, new Rect(002, bitmapHeight),   
 
161.                         new Rect(end - 500, end-48, bitmapHeight), null);  
 
162.                 canvas.drawText("-"+time, end-45, bitmapHeight/2+5, mTextPaint);  
 
163.             }else{  
 
164.                 canvas.drawBitmap(line, new Rect(002, bitmapHeight),   
 
165.                         new Rect(end - 600, end-58, bitmapHeight), null);  
 
166.                 canvas.drawText("-"+time, end-55, bitmapHeight/2+5, mTextPaint);  
 
167.             }  
 
168.         }  
 
169.         super.onDraw(canvas);  
 
170.     }  
 
171.   
 
172.   
 
173.     @Override  
 
174.     public boolean onTouchEvent(MotionEvent event) {  
 
175.         // TODO Auto-generated method stub  
 176.         //在这里因为要换按钮,故而需要更新整个视图  
 177.         if(event.getAction() == MotionEvent.ACTION_DOWN){  
 
178.             onClickListener.onClick(this);  
 
179.             invalidate();  
 
180.         }  
 
181.         return true;  
 
182.     }  
 
183.       
 
184.     /** 
 185.      * 这个方法必须设置,当播放的时候 
 186.      * 
@param isPlaying 
 187.      
*/  
 
188.     public void setStateChanged(boolean isPlaying){  
 
189.         this.isPlaying = isPlaying;  
 
190.     }  
 
191.       
 
192.     public void setTextColor(int color){  
 
193.         this.color = color;  
 
194.         invalidate();  
 
195.     }  
 
196.       
 
197.   
 
198.     /** 
 199.      * Determines the width of this view 
 200.      * 
@param measureSpec A measureSpec packed into an int 
 201.      * 
@return The width of the view, honoring constraints from measureSpec 
 202.      
*/  
 
203.     private int measureWidth(int measureSpec) {  
 
204.         int result = 0;  
 
205.         int specMode = MeasureSpec.getMode(measureSpec);  
 
206.         int specSize = MeasureSpec.getSize(measureSpec);  
 
207.         if (specMode == MeasureSpec.EXACTLY) {  
 
208.             // We were told how big to be  
 209.             result = specSize;  
 
210.         } else {  
 
211.             result = (int) ((int)max*perLen + getPaddingLeft() + getPaddingRight());  
 
212.             if (specMode == MeasureSpec.AT_MOST) {  
 
213.                 // Respect AT_MOST value if that was what is called for by measureSpec  
 214.                 result = Math.min(result, specSize);  
 
215.             }  
 
216.         }  
 
217.         System.out.println("width:"+result);  
 
218.         maxSize = result;  
 
219.         return result;  
 
220.     }  
 
221.       
 
222.     /** 
 223.      * Determines the height of this view 
 224.      * 
@param measureSpec A measureSpec packed into an int 
 225.      * 
@return The height of the view, honoring constraints from measureSpec 
 226.      
*/  
 
227.     private int measureHeight(int measureSpec) {  
 
228.         int result = 0;  
 
229.         int specMode = MeasureSpec.getMode(measureSpec);  
 
230.         int specSize = MeasureSpec.getSize(measureSpec);  
 
231.   
 
232.         if (specMode == MeasureSpec.EXACTLY) {  
 
233.             // We were told how big to be  
 234.             result = specSize;  
 
235.         } else {  
 
236.             // Measure the text (beware: ascent is a negative number)  
 237.             result = (int) getPaddingTop() + getPaddingBottom() + bitmapHeight;  
 
238.             if (specMode == MeasureSpec.AT_MOST) {  
 
239.                 // Respect AT_MOST value if that was what is called for by measureSpec  
 240.                 result = Math.min(result, specSize);  
 
241.             }  
 
242.         }  
 
243.         System.out.println("Height:"+result);  
 
244.         return result;  
 
245.     }  
 
246.       
 
247.     /** 
 248.      * set the time 
 249.      * 
@param currentTime 当前播放时间 
 250.      * 
@param totalTime 总播放时间 
 251.      
*/  
 
252.     public void setTime(int currentTime, int totalTime){  
 
253.         int time = totalTime - currentTime;  
 
254.         if(time <= 1000){  
 
255.             this.time="00:00";  
 
256.             return;  
 
257.         }  
 
258.         time/=1000;  
 
259.         int minute = time/60;  
 
260.         int hour = minute/60;  
 
261.         int second = time%60;  
 
262.         minute %= 60;  
 
263.         if(hour == 0){  
 
264.             this.time = String.format("%02d:%02d", minute,second);  
 
265.         }else{  
 
266.             this.time = String.format("%02d:%02d:%02d", hour, minute,second);  
 
267.         }  
 
268.     }  
 
269.   
 
270.     /** 
 271.      *  
 272.      * 
@param viewWidth 组件的宽度 
 273.      
*/  
 
274.     public void setMax(int max){  
 
275.         this.max = max;  
 
276.     }  
 
277.       
 
278.     public int getMax(){  
 
279.         return (int)max;  
 
280.     }  
 
281.       
 
282.     /** 
 283.      * 设置第一进度 
 284.      * 
@param progress 
 285.      
*/  
 
286.     public void setProgress(int progress){  
 
287.         if(progress>max){  
 
288.             progress = (int) max;  
 
289.         }  
 
290.         else if(progress<0){  
 
291.             progress = 0;  
 
292.         }  
 
293.         if(mOnProgressChanged!=null){  
 
294.             mOnProgressChanged.onProgressUpdated();  
 
295.         }  
 
296.         this.progress = progress;  
 
297.         invalidate();  
 
298.     }  
 
299.       
 
300.     /** 
 301.      * 设置第二进度 
 302.      * 
@param secondProgress 
 303.      
*/  
 
304.     public void setSecondProgress(int secondProgress){  
 
305.         if(secondProgress>max){  
 
306.             secondProgress = (int) max;  
 
307.         }  
 
308.         else if(secondProgress<0){  
 
309.             secondProgress = 0;  
 
310.         }  
 
311.         if(mOnProgressChanged!=null){  
 
312.             mOnProgressChanged.onSecondProgressUpdated();  
 
313.         }  
 
314.         this.secondProgress = secondProgress;  
 
315.         invalidate();  
 
316.     }  
 
317.       
 
318.     /** 
 319.      * 设置进度监听器 
 320.      * 
@param mOnProgressChanged 
 321.      
*/  
 
322.     public void setmOnProgressChanged(OnProgressChanged mOnProgressChanged) {  
 
323.         this.mOnProgressChanged = mOnProgressChanged;  
 
324.     }  
 
325.   
 
326.   
 
327.     public interface OnProgressChanged{  
 
328.         void onProgressUpdated();  
 
329.         void onSecondProgressUpdated();  
 
330.     }  
 
331.       
 
332.     @Override  
 
333.     public void setOnClickListener(OnClickListener l) {  
 
334.         // TODO Auto-generated method stub  
 335.         if(l != null) onClickListener = l;  
 
336.         super.setOnClickListener(l);  
 
337.     }  
 
338.   
 
339.     private View.OnClickListener onClickListener;  
 
340.   
 
341. }  

package com.hao;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class ProgressButton extends View{
    
private Bitmap begin , bm_gray, bm_yellow, bm_second, end_gray, end_yellow, line,begin_gray;
    
private Bitmap pausePressedImg;
    
private Bitmap playPressedImg;
    
private int bitmapWidth = 0 , bitmapHeight = 0, btWidth = 0, btHeight = 0;  
    
private int progress = 0, secondProgress = 0;
    
private double perLen = 0, max = 0, maxSize = 0;
    
private OnProgressChanged mOnProgressChanged;
    
private boolean isPlaying = false;
    
private Paint mTextPaint;
    
private String time = "00:00";
    
private int color = Color.BLUE;

    
public ProgressButton(Context context) {
        
super(context);
        
// TODO Auto-generated constructor stub
        init();
    }
    
    
public ProgressButton(Context context, AttributeSet attrs, int defStyle){
        
super(context, attrs, defStyle);
        init();
    }
    
    
public ProgressButton(Context context, AttributeSet attrs){
        
super(context, attrs);
        init();
    }
    
    
private void init(){
        begin 
= drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_left_yellow));
        begin_gray 
= drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_left_gray));
        bm_gray 
=  drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_gray));
        bm_yellow 
=  drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_yellow));
        bm_second 
=  drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_second_yellow));
        end_gray 
=  drawableToBitmap(getResources().getDrawable( R.drawable.rectangle_right_gray));
        end_yellow 
=  drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_right_yellow));
        line 
= drawableToBitmap(getResources().getDrawable(R.drawable.rectangle_line));
        pausePressedImg 
= BitmapFactory.decodeResource(getResources(), R.drawable.pause_button_pressed);
        playPressedImg 
= BitmapFactory.decodeResource(getResources(), R.drawable.play_button_pressed);
        bitmapHeight 
= begin.getHeight();
        bitmapWidth 
= begin.getWidth();
        btWidth 
= pausePressedImg.getWidth();
        btHeight 
= pausePressedImg.getHeight();
        mTextPaint 
= new Paint();
        mTextPaint.setAntiAlias(
true);
        mTextPaint.setTextSize(
14);
        mTextPaint.setColor(color);
        setPadding(
3333);
    }
    
    
public static Bitmap drawableToBitmap(Drawable drawable) {
        
int width = drawable.getIntrinsicWidth();
        
int height = drawable.getIntrinsicHeight();
        Bitmap bitmap 
= Bitmap.createBitmap(width, height, drawable
                .getOpacity() 
!= PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565);
        Canvas canvas 
= new Canvas(bitmap);
        drawable.setBounds(
00, width, height);
        drawable.draw(canvas);
        
return bitmap;
    }    
    
    
    
    @Override
    
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        
// TODO Auto-generated method stub
        Log.e("*******""onMeasure");
        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
        perLen 
= maxSize/max; 
    }

    @Override
    
protected void onDraw(Canvas canvas) {
        
// TODO Auto-generated method stub
        Log.e("*******""onDraw");
        
int middle1 = (int) (progress*perLen), middle2 = (int) (secondProgress*perLen) ,end = (int) maxSize-4;
        
if(progress == 0 && secondProgress == 0){
            
//draw background
            canvas.drawBitmap(begin_gray, new Rect(0,0,bitmapWidth,bitmapHeight), 
                                    
new Rect(00, bitmapWidth, bitmapHeight), null);
            canvas.drawBitmap(bm_gray, 
new Rect(0,0,end-middle1,bitmapHeight), 
                    
new Rect(bitmapWidth, 0, end, bitmapHeight), null);
            canvas.drawBitmap(end_gray, 
new Rect(0,0,4,bitmapHeight), 
                    
new Rect(end, 0, end+4, bitmapHeight), null);
            
//draw button and line
            canvas.drawBitmap(playPressedImg, new Rect(00, btWidth, btHeight), 
                    
new Rect(00, btWidth, bitmapHeight), null);
            canvas.drawBitmap(line, 
new Rect(002, bitmapHeight), 
                    
new Rect(btWidth, 0, btWidth+2, bitmapHeight), null);
            
//draw time and line
            if(time.length() == 5){
                canvas.drawBitmap(line, 
new Rect(002, bitmapHeight), 
                        
new Rect(end - 500, end-48, bitmapHeight), null);
                canvas.drawText(
"-"+time, end-45, bitmapHeight/2+5, mTextPaint);
            }
else{
                canvas.drawBitmap(line, 
new Rect(002, bitmapHeight), 
                        
new Rect(end - 600, end-58, bitmapHeight), null);
                canvas.drawText(
"-"+time, end-55, bitmapHeight/2+5, mTextPaint);
            }
        }
else{
            
//begin
            canvas.drawBitmap(begin, new Rect(0,0,bitmapWidth,bitmapHeight), 
                        
new Rect(00, bitmapWidth, bitmapHeight), null);
            canvas.drawBitmap(bm_yellow, 
new Rect(0,0,middle1-bitmapWidth,bitmapHeight), 
                        
new Rect(bitmapWidth, 0, middle1, bitmapHeight), null);
            
//middle
            if(secondProgress != 0 && secondProgress > progress){
                canvas.drawBitmap(bm_second, 
new Rect(0,0,bitmapWidth,bitmapHeight), 
                        
new Rect(middle1, 0, middle2, bitmapHeight), null);
                canvas.drawBitmap(bm_gray, 
new Rect(0,0,bitmapWidth,bitmapHeight), 
                        
new Rect(middle2, 0, end, bitmapHeight), null);
            }
else{
                canvas.drawBitmap(bm_gray, 
new Rect(0,0,end-middle1,bitmapHeight), 
                        
new Rect(middle1, 0, end, bitmapHeight), null);
            }
            
//end
            canvas.drawBitmap(end_gray, new Rect(0,0,4,bitmapHeight), 
                    
new Rect(end, 0, end+4, bitmapHeight), null);
            
if(middle2 >= end || middle1 >= end){
                canvas.drawBitmap(end_yellow, 
new Rect(0,0,4,bitmapHeight), 
                        
new Rect(end, 0, end+4, bitmapHeight), null);
            }
            
//draw button
            if(!isPlaying) {
                canvas.drawBitmap(playPressedImg, 
new Rect(00, btWidth, btHeight), 
                        
new Rect(00, btWidth, bitmapHeight), null);
            }
else{
                canvas.drawBitmap(pausePressedImg, 
new Rect(00, btWidth, btHeight), 
                        
new Rect(00, btWidth, bitmapHeight), null);
            }
            
//draw line and time
            canvas.drawBitmap(line, new Rect(002, bitmapHeight), 
                    
new Rect(btWidth, 0, btWidth+2, bitmapHeight), null);
            
if(time.length() == 5){
                canvas.drawBitmap(line, 
new Rect(002, bitmapHeight), 
                        
new Rect(end - 500, end-48, bitmapHeight), null);
                canvas.drawText(
"-"+time, end-45, bitmapHeight/2+5, mTextPaint);
            }
else{
                canvas.drawBitmap(line, 
new Rect(002, bitmapHeight), 
                        
new Rect(end - 600, end-58, bitmapHeight), null);
                canvas.drawText(
"-"+time, end-55, bitmapHeight/2+5, mTextPaint);
            }
        }
        
super.onDraw(canvas);
    }


    @Override
    
public boolean onTouchEvent(MotionEvent event) {
        
// TODO Auto-generated method stub
        
//在这里因为要换按钮,故而需要更新整个视图
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            onClickListener.onClick(
this);
            invalidate();
        }
        
return true;
    }
    
    
/**
     * 这个方法必须设置,当播放的时候
     * 
@param isPlaying
     
*/
    
public void setStateChanged(boolean isPlaying){
        
this.isPlaying = isPlaying;
    }
    
    
public void setTextColor(int color){
        
this.color = color;
        invalidate();
    }
    

    
/**
     * Determines the width of this view
     * 
@param measureSpec A measureSpec packed into an int
     * 
@return The width of the view, honoring constraints from measureSpec
     
*/
    
private int measureWidth(int measureSpec) {
        
int result = 0;
        
int specMode = MeasureSpec.getMode(measureSpec);
        
int specSize = MeasureSpec.getSize(measureSpec);
        
if (specMode == MeasureSpec.EXACTLY) {
            
// We were told how big to be
            result = specSize;
        } 
else {
            result 
= (int) ((int)max*perLen + getPaddingLeft() + getPaddingRight());
            
if (specMode == MeasureSpec.AT_MOST) {
                
// Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }
        System.out.println(
"width:"+result);
        maxSize 
= result;
        
return result;
    }
    
    
/**
     * Determines the height of this view
     * 
@param measureSpec A measureSpec packed into an int
     * 
@return The height of the view, honoring constraints from measureSpec
     
*/
    
private int measureHeight(int measureSpec) {
        
int result = 0;
        
int specMode = MeasureSpec.getMode(measureSpec);
        
int specSize = MeasureSpec.getSize(measureSpec);

        
if (specMode == MeasureSpec.EXACTLY) {
            
// We were told how big to be
            result = specSize;
        } 
else {
            
// Measure the text (beware: ascent is a negative number)
            result = (int) getPaddingTop() + getPaddingBottom() + bitmapHeight;
            
if (specMode == MeasureSpec.AT_MOST) {
                
// Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }
        System.out.println(
"Height:"+result);
        
return result;
    }
    
    
/**
     * set the time
     * 
@param currentTime 当前播放时间
     * 
@param totalTime 总播放时间
     
*/
    
public void setTime(int currentTime, int totalTime){
        
int time = totalTime - currentTime;
        
if(time <= 1000){
            
this.time="00:00";
            
return;
        }
        time
/=1000;
        
int minute = time/60;
        
int hour = minute/60;
        
int second = time%60;
        minute 
%= 60;
        
if(hour == 0){
            
this.time = String.format("%02d:%02d", minute,second);
        }
else{
            
this.time = String.format("%02d:%02d:%02d", hour, minute,second);
        }
    }

    
/**
     * 
     * 
@param viewWidth 组件的宽度
     
*/
    
public void setMax(int max){
        
this.max = max;
    }
    
    
public int getMax(){
        
return (int)max;
    }
    
    
/**
     * 设置第一进度
     * 
@param progress
     
*/
    
public void setProgress(int progress){
        
if(progress>max){
            progress 
= (int) max;
        }
        
else if(progress<0){
            progress 
= 0;
        }
        
if(mOnProgressChanged!=null){
            mOnProgressChanged.onProgressUpdated();
        }
        
this.progress = progress;
        invalidate();
    }
    
    
/**
     * 设置第二进度
     * 
@param secondProgress
     
*/
    
public void setSecondProgress(int secondProgress){
        
if(secondProgress>max){
            secondProgress 
= (int) max;
        }
        
else if(secondProgress<0){
            secondProgress 
= 0;
        }
        
if(mOnProgressChanged!=null){
            mOnProgressChanged.onSecondProgressUpdated();
        }
        
this.secondProgress = secondProgress;
        invalidate();
    }
    
    
/**
     * 设置进度监听器
     * 
@param mOnProgressChanged
     
*/
    
public void setmOnProgressChanged(OnProgressChanged mOnProgressChanged) {
        
this.mOnProgressChanged = mOnProgressChanged;
    }


    
public interface OnProgressChanged{
        
void onProgressUpdated();
        
void onSecondProgressUpdated();
    }
    
    @Override
    
public void setOnClickListener(OnClickListener l) {
        
// TODO Auto-generated method stub
        if(l != null) onClickListener = l;
        
super.setOnClickListener(l);
    }

    
private View.OnClickListener onClickListener;

}

 

 
 控制非常简单,在这里设置了第一和二进度:

Java代码  收藏代码

   
1package com.hao;  
   
2.   
   
3import java.util.Timer;  
   
4import java.util.TimerTask;  
   
5.   
   
6import com.hao.ProgressView.OnProgressChanged;  
   
7.   
   
8import android.app.Activity;  
   
9import android.graphics.drawable.Drawable;  
  
10import android.os.Bundle;  
  
11import android.os.Handler;  
  
12import android.os.Message;  
  
13import android.view.Gravity;  
  
14import android.view.View;  
  
15import android.view.View.OnClickListener;  
  
16import android.view.ViewGroup.LayoutParams;  
  
17import android.widget.ImageButton;  
  
18import android.widget.LinearLayout;  
  
19import android.widget.SeekBar;  
  
20import android.widget.SeekBar.OnSeekBarChangeListener;  
  
21.   
  
22public class SeekBarTestActivity extends Activity {  
  
23.     ProgressButton bp;  
  
24.     int time = 60000 , currentTime = 0;  
  
25.     boolean flag = true;  
  
26.     /** Called when the activity is first created. */  
  
27.     @Override  
  
28.     public void onCreate(Bundle savedInstanceState) {  
  
29.         super.onCreate(savedInstanceState);  
  
30.         setContentView(R.layout.my_progress_bar);  
  
31.         bp = (ProgressButton) findViewById(R.id.pbt);  
  
32.         bp.setOnClickListener(new OnClickListener() {  
  
33.               
  
34.             @Override  
  
35.             public void onClick(View v) {  
  
36.                 // TODO Auto-generated method stub  
  37.                 System.out.println("main onck");  
  
38.                 bp.setStateChanged(flag);  
  
39.                 flag = !flag;  
  
40.             }  
  
41.         });  
  
42.         bp.setMax(60000);  
  
43.         Timer timer = new Timer();  
  
44.         timer.schedule(new TimerTask() {  
  
45.               
  
46.             @Override  
  
47.             public void run() {  
  
48.                 // TODO Auto-generated method stub  
  49.                 handler.sendEmptyMessage(0);  
  
50.             }  
  
51.         }, 02000);  
  
52.           
  
53.           
  
54.     }  
  
55.       
  
56.     Handler handler = new Handler(){  
  
57.   
  
58.         @Override  
  
59.         public void handleMessage(Message msg) {  
  
60.             // TODO Auto-generated method stub  
  61.             currentTime +=1000;  
  
62.             bp.setProgress(currentTime);  
  
63.             bp.setSecondProgress(currentTime+1000);  
  
64.             bp.setTime(currentTime, time);  
  
65.         }  
  
66.           
  
67.     };  
  
68. }  

package com.hao;

import java.util.Timer;
import java.util.TimerTask;

import com.hao.ProgressView.OnProgressChanged;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class SeekBarTestActivity extends Activity {
    ProgressButton bp;
    
int time = 60000 , currentTime = 0;
    
boolean flag = true;
    
/** Called when the activity is first created. */
    @Override
    
public void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.my_progress_bar);
        bp 
= (ProgressButton) findViewById(R.id.pbt);
        bp.setOnClickListener(
new OnClickListener() {
            
            @Override
            
public void onClick(View v) {
                
// TODO Auto-generated method stub
                System.out.println("main onck");
                bp.setStateChanged(flag);
                flag 
= !flag;
            }
        });
        bp.setMax(
60000);
        Timer timer 
= new Timer();
        timer.schedule(
new TimerTask() {
            
            @Override
            
public void run() {
                
// TODO Auto-generated method stub
                handler.sendEmptyMessage(0);
            }
        }, 
02000);
        
        
    }
    
    Handler handler 
= new Handler(){

        @Override
        
public void handleMessage(Message msg) {
            
// TODO Auto-generated method stub
            currentTime +=1000;
            bp.setProgress(currentTime);
            bp.setSecondProgress(currentTime
+1000);
            bp.setTime(currentTime, time);
        }
        
    };
}

 

布局文件:

Java代码  收藏代码

   
1<?xml version="1.0" encoding="utf-8"?>  
   
2<LinearLayout   
   
3.     xmlns:android="http://schemas.android.com/apk/res/android"  
   
4.     android:layout_height="fill_parent"  
   
5.     android:layout_width="fill_parent"  
   
6.     android:orientation="vertical">  
   
7.     <TextView android:layout_width="fill_parent"  
   
8.             android:layout_height="wrap_content"  
   
9.             android:text="音乐播放"/>  
  
10.     <LinearLayout   
  
11.         android:layout_width="fill_parent"  
  
12.         android:layout_height="wrap_content"  
  
13.         android:paddingTop="5dip"  
  
14.         android:gravity="center_horizontal">  
  
15.         <com.hao.ProgressButton  
  
16.             android:id="@+id/pb1"  
  
17.             android:layout_width="300dp"  
  
18.             android:layout_height="45dp"  
  
19.             android:background="@drawable/button_left_gray_background"/>  
  
20.     </LinearLayout>  
  
21</LinearLayout>  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android
="http://schemas.android.com/apk/res/android"
    android:layout_height
="fill_parent"
    android:layout_width
="fill_parent"
    android:orientation
="vertical">
       
<TextView android:layout_width="fill_parent"
            android:layout_height
="wrap_content"
            android:text
="音乐播放"/>
       
<LinearLayout 
        android:layout_width
="fill_parent"
        android:layout_height
="wrap_content"
        android:paddingTop
="5dip"
        android:gravity
="center_horizontal">
        
<com.hao.ProgressButton
            android:id
="@+id/pb1"
            android:layout_width
="300dp"
            android:layout_height
="45dp"
            android:background
="@drawable/button_left_gray_background"/>
    
</LinearLayout>
</LinearLayout>


posted on 2012-03-31 16:38 life02 阅读(449) 评论(0)  编辑 收藏 引用 所属分类: android ndk开发

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理