android 自定义动画按钮

引用:http://blog.csdn.net/GEOLO/article/details/6221350

 

 

 

  1. package com.geolo.android;  
  2. import android.content.Context;  
  3. import android.graphics.Bitmap;  
  4. import android.graphics.Bitmap.Config;  
  5. import android.graphics.BitmapFactory;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Color;  
  8. import android.graphics.Matrix;  
  9. import android.graphics.Paint;  
  10. import android.graphics.PorterDuff.Mode;  
  11. import android.graphics.PorterDuffXfermode;  
  12. import android.graphics.Rect;  
  13. import android.graphics.RectF;  
  14. import android.util.AttributeSet;  
  15. import android.view.MotionEvent;  
  16. import android.widget.ImageView;  
  17. public class MyButtonView extends ImageView{  
  18.     private Bitmap animBitmap = null;  
  19.     private Bitmap buttonBitmap = null;  
  20.     private int moveX = 0;  
  21.     private boolean isPress = false;  
  22.     private int timeID = 0;  
  23.     private Integer myButtonID[] = new Integer[]{  
  24.             R.drawable.mybutton01,  
  25.             R.drawable.mybutton02,  
  26.             R.drawable.mybutton03,  
  27.             R.drawable.mybutton04,  
  28.             R.drawable.mybutton05,  
  29.             R.drawable.mybutton06,  
  30.             R.drawable.mybutton07,  
  31.             R.drawable.mybutton08,  
  32.             R.drawable.mybutton09,  
  33.             R.drawable.mybutton10,  
  34.             R.drawable.mybutton11,  
  35.             R.drawable.mybutton12,  
  36.             R.drawable.mybutton13,  
  37.             R.drawable.mybutton14,  
  38.             R.drawable.mybutton15,  
  39.             R.drawable.mybutton16,  
  40.             R.drawable.mybutton17,  
  41.             R.drawable.mybutton18,  
  42.             R.drawable.mybutton19,  
  43.             R.drawable.mybutton20  
  44.     };  
  45.     public MyButtonView(Context context) {  
  46.         this(context, null);  
  47.     }  
  48.     public MyButtonView(Context context, AttributeSet attrs) {  
  49.         super(context,attrs);  
  50.         animBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.myutil);  
  51.         buttonBitmap = BitmapFactory.decodeResource(getResources(),myButtonID[0]);  
  52.         new Thread(new MyThread()).start();  
  53.     }  
  54.   
  55.     @Override  
  56.     protected void onDraw(Canvas canvas) {  
  57.         super.onDraw(canvas);  
  58.         drawImage(canvas,0,0,animBitmap,moveX,0,200,100);  
  59.         canvas.drawBitmap(buttonBitmap,20,15, null);  
  60.     }  
  61.     @Override  
  62.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  63.         setMeasuredDimension(131, 53);  
  64.     }  
  65.   
  66.     private void drawImage(Canvas canvas , int x, int y,Bitmap oldBitmap , int sx,int sy,int width ,int height){  
  67.         Rect rect_x = new Rect();  
  68.         rect_x.left = sx;  
  69.         rect_x.right = sx + width;  
  70.         rect_x.top = sy;  
  71.         rect_x.bottom = sy + height;  
  72.         Rect rect_y = new Rect();  
  73.         rect_y.left = x;  
  74.         rect_y.right = x + width;  
  75.         rect_y.top = y;  
  76.         rect_y.bottom = y + height;  
  77.         //按指定的裁剪矩形来绘制图片  
  78.         //分别是屏幕上的矩形和图片上要绘制的矩形裁剪  
  79.         Bitmap output = Bitmap.createBitmap(oldBitmap.getWidth(),  
  80.                 oldBitmap.getHeight(), Config.ARGB_8888);  
  81.         Canvas canvas2 = new Canvas(output);  
  82.         Paint paint = new Paint();  
  83.         paint.setAntiAlias(true);  
  84.         //canvas.drawARGB(0, 0, 0, 0);  
  85.         //paint.setColor(0xff424242);  
  86.         //paint.setAlpha(140);  
  87.         //canvas2.drawRoundRect(new RectF(2,4,130,50), 10.0f, 10.0f, paint);  
  88.         canvas2.drawRoundRect(new RectF(1,0,130,50), 15.0f, 15.0f, paint);  
  89.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  90.         canvas2.drawBitmap(oldBitmap, rect_x, rect_y, paint);  
  91.         canvas.drawBitmap(output, 0 , 0, null);  
  92.         rect_x = null;  
  93.         rect_y = null;  
  94.     }  
  95.   
  96.     @Override  
  97.     public boolean dispatchTouchEvent(MotionEvent event) {  
  98.         /*  if(isReleased){ 
  99.             touchPoint = new Point(); 
  100.             touchPoint.x = (int)event.getX(); 
  101.             touchPoint.y = (int)event.getY();    
  102.         }*/  
  103.         switch (event.getAction()) {  
  104.         case MotionEvent.ACTION_DOWN:    
  105.             isPress = true;  
  106.             break;  
  107.         case MotionEvent.ACTION_UP:  
  108.             isPress = false;  
  109.             break;  
  110.         default:  
  111.             break;  
  112.         }  
  113.         return true;  
  114.     }  
  115.     private class MyThread implements Runnable{  
  116.         @Override  
  117.         public void run() {  
  118.             while(!Thread.currentThread().isInterrupted()){  
  119.                 try {  
  120.                     moveX++;  
  121.                     if(moveX > 100){  
  122.                         moveX = 0;  
  123.                     }  
  124.                     if(isPress){  
  125.                         timeID ++ ;  
  126.                         if(timeID <20){  
  127.                             buttonBitmap = BitmapFactory.decodeResource(getResources(), myButtonID[timeID]);  
  128.                         }else{  
  129.                             timeID =0 ;  
  130.                         }  
  131.                     }  
  132.                     Thread.sleep(50);  
  133.                 } catch (Exception e) {  
  134.                     Thread.currentThread().interrupt();  
  135.                 }  
  136.                 postInvalidate();  
  137.             }  
  138.         }  
  139.     }  
  140. }  

 

posted @ 2012-05-31 21:56  镇水古月  阅读(663)  评论(0编辑  收藏  举报