android 自定义动画按钮
2012-05-31
625
简介:
引用:http://blog.csdn.net/GEOLO/article/details/6221350
[c-sharp] view plaincopyprint?
package com.
引用:http://blog.csdn.net/GEOLO/article/details/6221350

- package com.geolo.android;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.Config;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Matrix;
- import android.graphics.Paint;
- import android.graphics.PorterDuff.Mode;
- import android.graphics.PorterDuffXfermode;
- import android.graphics.Rect;
- import android.graphics.RectF;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- import android.widget.ImageView;
- public class MyButtonView extends ImageView{
- private Bitmap animBitmap = null;
- private Bitmap buttonBitmap = null;
- private int moveX = 0;
- private boolean isPress = false;
- private int timeID = 0;
- private Integer myButtonID[] = new Integer[]{
- R.drawable.mybutton01,
- R.drawable.mybutton02,
- R.drawable.mybutton03,
- R.drawable.mybutton04,
- R.drawable.mybutton05,
- R.drawable.mybutton06,
- R.drawable.mybutton07,
- R.drawable.mybutton08,
- R.drawable.mybutton09,
- R.drawable.mybutton10,
- R.drawable.mybutton11,
- R.drawable.mybutton12,
- R.drawable.mybutton13,
- R.drawable.mybutton14,
- R.drawable.mybutton15,
- R.drawable.mybutton16,
- R.drawable.mybutton17,
- R.drawable.mybutton18,
- R.drawable.mybutton19,
- R.drawable.mybutton20
- };
- public MyButtonView(Context context) {
- this(context, null);
- }
- public MyButtonView(Context context, AttributeSet attrs) {
- super(context,attrs);
- animBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.myutil);
- buttonBitmap = BitmapFactory.decodeResource(getResources(),myButtonID[0]);
- new Thread(new MyThread()).start();
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- drawImage(canvas,0,0,animBitmap,moveX,0,200,100);
- canvas.drawBitmap(buttonBitmap,20,15, null);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- setMeasuredDimension(131, 53);
- }
-
- private void drawImage(Canvas canvas , int x, int y,Bitmap oldBitmap , int sx,int sy,int width ,int height){
- Rect rect_x = new Rect();
- rect_x.left = sx;
- rect_x.right = sx + width;
- rect_x.top = sy;
- rect_x.bottom = sy + height;
- Rect rect_y = new Rect();
- rect_y.left = x;
- rect_y.right = x + width;
- rect_y.top = y;
- rect_y.bottom = y + height;
-
-
- Bitmap output = Bitmap.createBitmap(oldBitmap.getWidth(),
- oldBitmap.getHeight(), Config.ARGB_8888);
- Canvas canvas2 = new Canvas(output);
- Paint paint = new Paint();
- paint.setAntiAlias(true);
-
-
-
-
- canvas2.drawRoundRect(new RectF(1,0,130,50), 15.0f, 15.0f, paint);
- paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
- canvas2.drawBitmap(oldBitmap, rect_x, rect_y, paint);
- canvas.drawBitmap(output, 0 , 0, null);
- rect_x = null;
- rect_y = null;
- }
-
- @Override
- public boolean dispatchTouchEvent(MotionEvent event) {
-
-
-
-
-
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- isPress = true;
- break;
- case MotionEvent.ACTION_UP:
- isPress = false;
- break;
- default:
- break;
- }
- return true;
- }
- private class MyThread implements Runnable{
- @Override
- public void run() {
- while(!Thread.currentThread().isInterrupted()){
- try {
- moveX++;
- if(moveX > 100){
- moveX = 0;
- }
- if(isPress){
- timeID ++ ;
- if(timeID <20){
- buttonBitmap = BitmapFactory.decodeResource(getResources(), myButtonID[timeID]);
- }else{
- timeID =0 ;
- }
- }
- Thread.sleep(50);
- } catch (Exception e) {
- Thread.currentThread().interrupt();
- }
- postInvalidate();
- }
- }
- }
- }
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。