android 自定义动画按钮

简介: 引用:http://blog.csdn.net/GEOLO/article/details/6221350       [c-sharp] view plaincopyprint? package com.

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

 

 

 

[c-sharp]  view plain copy print ?
  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. }  

 

相关文章
|
23天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
51 1
|
4月前
|
XML Android开发 数据安全/隐私保护
Android 自定义开源库 EasyView
Android 自定义开源库 EasyView
|
4月前
|
XML Java Android开发
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
42 1
|
4月前
|
XML 搜索推荐 Java
Android App开发之自定义图形中位图与图形互转、剪裁图形内部区域、给图形添加部件的讲解及实战(附源码 简单易懂)
Android App开发之自定义图形中位图与图形互转、剪裁图形内部区域、给图形添加部件的讲解及实战(附源码 简单易懂)
33 0
|
4月前
|
XML 前端开发 Java
Android Studio App自定义控件中自定义视图的绘制讲解及实战(附源码 包括自定义绘制各种图形)
Android Studio App自定义控件中自定义视图的绘制讲解及实战(附源码 包括自定义绘制各种图形)
37 1
|
2天前
|
移动开发 Java Unix
Android系统 自动加载自定义JAR文件
Android系统 自动加载自定义JAR文件
19 1
|
2天前
|
Shell Android开发 开发者
Android系统 自定义动态修改init.custom.rc
Android系统 自定义动态修改init.custom.rc
19 0
|
2天前
|
存储 安全 Android开发
Android系统 自定义系统和应用权限
Android系统 自定义系统和应用权限
16 0
|
4月前
|
XML API Android开发
Android 自定义View 之 圆环进度条
Android 自定义View 之 圆环进度条
|
4月前
|
XML Java Android开发
Android Studio App开发之实现底部标签栏BottomNavigationView和自定义标签按钮实战(附源码 超详细必看)
Android Studio App开发之实现底部标签栏BottomNavigationView和自定义标签按钮实战(附源码 超详细必看)
76 0