• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            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 閱讀(474) 評論(0)  編輯 收藏 引用 所屬分類: android ndk開發
            偷窥少妇久久久久久久久| 少妇高潮惨叫久久久久久| 浪潮AV色综合久久天堂| 欧美亚洲另类久久综合婷婷 | 欧美久久久久久精选9999| 99久久国语露脸精品国产| 97精品依人久久久大香线蕉97| 亚洲精品无码久久久久AV麻豆| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区| 色综合久久久久无码专区| 久久精品国产99国产精品亚洲| 人妻少妇精品久久| 久久精品无码一区二区三区免费| 国内精品久久久久久久久| 国产精品欧美久久久久无广告| 久久国产精品成人免费| 99久久精品免费观看国产| 亚洲国产天堂久久综合网站 | 国产99久久久国产精免费| 欧美亚洲国产精品久久蜜芽| 青青青伊人色综合久久| 91精品国产色综久久| 久久久久国产精品三级网| 日韩亚洲国产综合久久久| 精品人妻伦九区久久AAA片69| 久久久一本精品99久久精品88| 一本久久a久久精品亚洲| 久久精品亚洲中文字幕无码麻豆| 国产一区二区三区久久精品| 成人精品一区二区久久| 亚洲国产精品狼友中文久久久| 久久久久免费精品国产| 久久国产色AV免费看| 久久成人18免费网站| 中文字幕久久久久人妻| 久久久国产精品福利免费| 日日狠狠久久偷偷色综合免费| 熟妇人妻久久中文字幕| 国产69精品久久久久9999| 中文字幕人妻色偷偷久久| 国产91久久综合|