首页 > 代码库 > 实例代码(1)

实例代码(1)

此例主要是为了展示怎么在画图的指定区域获取监听事件。

 

  1 package com.threew;  2   3 import android.content.Context;  4 import android.graphics.Bitmap;  5 import android.graphics.BitmapFactory;  6 import android.graphics.Canvas;  7 import android.graphics.Color;  8 import android.graphics.Paint;  9 import android.graphics.Rect; 10 import android.graphics.RectF; 11 import android.util.AttributeSet; 12 import android.view.GestureDetector; 13 import android.view.MotionEvent; 14 import android.view.View; 15 import android.view.GestureDetector.SimpleOnGestureListener; 16 import android.view.View.OnTouchListener; 17 import android.widget.Toast; 18  19 /** 20  *  21  * @Title: Mwedgit.java  22  * @author Rubert 23  * @date 2014-8-20 AM 11:24:17  24  * @version V1.0  25  */ 26 public class MWedgit extends View implements OnTouchListener{ 27      28     private GestureDetector detector;//手势运动委托器 29  30     private GestureAction gestureAction ; 31      32     private OnBitmapClickListener mOnClickListener; 33      34     private Rect mRect = null; 35      36     private Context mContext = null; 37      38      39     public MWedgit(Context context) { 40         super(context, null); 41         init(context); 42     } 43      44     public MWedgit(Context context, AttributeSet attrs) { 45         super(context, attrs); 46         init(context); 47     } 48      49     public void init(Context context) { 50         mContext = context; 51         gestureAction = GestureAction.GESTURE_DEFAULT; 52         setOnTouchListener(this); 53         detector = new GestureDetector(context,new MyGestureListener()); 54         setLongClickable(true); 55     } 56                         57      58      59      60     @Override 61     protected void onDraw(Canvas canvas) { 62         super.onDraw(canvas); 63         Paint pt = new Paint(); 64         pt.setStyle(Paint.Style.FILL_AND_STROKE); 65         pt.setColor(Color.YELLOW); 66         Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 67         mRect = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); 68         canvas.drawBitmap(mBitmap, mRect, new RectF(mRect), pt); 69     } 70      71      72     @Override 73     public boolean onTouch(View v, MotionEvent e) { 74         detector.onTouchEvent(e); 75         if(e.getAction() == MotionEvent.ACTION_UP) 76         { 77             if(gestureAction == GestureAction.GESTURE_NONE) 78             { 79                 //TODO 80             } 81             if(gestureAction == GestureAction.GESTURE_TOUCH) 82             { 83                 mOnClickListener.onClick(); 84             } 85         } 86         return false; 87     } 88      89      90     class MyGestureListener extends SimpleOnGestureListener { 91          92         //用户按下触摸屏,尚未松开或拖动,由一个MotionEvent.ACTION_DOWN触发. 93         //注意和onDown的区别,强调的是没有松开或者拖动的状态 94         @Override 95         public void onShowPress(MotionEvent arg0) 96         { 97             super.onShowPress(arg0); 98         } 99         //用户按下触摸屏,由一个MotionEvent.ACTION_DOWN触发100         @Override101         public boolean onDown(MotionEvent e) {102             103             if(mRect != null && mRect.contains((int)e.getX(), (int)e.getY())) {104                 gestureAction = GestureAction.GESTURE_TOUCH;105             }106             return super.onDown(e);107         }108         //用户(用户按下触摸屏后)松开,由MotionEvent.ACTION_UP触发109         //如果在onDown和onFling之中2选1,那么使用这个事件是可以区分的110         @Override111         public boolean onSingleTapUp(MotionEvent arg0)112         {113             return false;114         }115         //长按 多个ACTION_DOWN触发116         @Override117         public void onLongPress(MotionEvent arg0)118         {119             super.onLongPress(arg0);120         }121         //用户按下触摸屏,快速移动后松开,由一个MotionEvent.ACTION_DOWN,多个ACTION_MOVE,一个ACTION_UP触发.122         //velocityX和velocityY分别表示在X和Y方向上移动的速度123         //e1 第一个Down,e2最后一个move124         @Override125         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)126         {127             //向左,速度每秒>200个像素128             if( (e1.getX()-e2.getX() > getWidth()/2) && (Math.abs(velocityX)) > 200)129             {130                 gestureAction = GestureAction.GESTURE_LEFT;131                 Toast.makeText(mContext, "向左", Toast.LENGTH_SHORT).show();132             }133             //向右,速度每秒>200个像素134             else if( (e2.getX()-e1.getX() > getWidth()/2) && (Math.abs(velocityX)) > 200)135             {136                 gestureAction = GestureAction.GESTURE_RIGHT;137                 Toast.makeText(mContext, "向右", Toast.LENGTH_SHORT).show();138             } 139             else if((e1.getY()-e2.getY() > getHeight()/2) && (Math.abs(velocityY)) > 200) 140             {141                 gestureAction = GestureAction.GESTURE_TOP;142                 Toast.makeText(mContext, "向上", Toast.LENGTH_SHORT).show();143             }144             else if((e2.getY()-e1.getY() > getHeight()/2) && (Math.abs(velocityY)) > 200) 145             {146                 gestureAction = GestureAction.GESTURE_DOWN;147                 Toast.makeText(mContext, "向下", Toast.LENGTH_SHORT).show();148             }149             return false;150         }151         //按下屏幕,并拖动,一个DOWM,多个MOVE触发152         //e1 第一个Down,e2当前移动的point153         //distance表示Scroll过程中的移动的像素。distanceX >0 表示向左,distanceY >0 表示向上,两个都大就是左上154         @Override155         public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY)156         {157             //向右158             /*if(e2.getX() - e1.getX() > getWidth()/2)159             {160                 gestureAction = GestureAction.GESTURE_RIGHT;161                 Toast.makeText(mContext, "向右", Toast.LENGTH_SHORT).show();162             }163             //向左164             else if(e1.getX() - e2.getX() > getWidth()/2)165             {166                 gestureAction = GestureAction.GESTURE_LEFT;167                 Toast.makeText(mContext, "向左", Toast.LENGTH_SHORT).show();168             }169             else if(e2.getY() - e1.getY() > getHeight()/2) 170             {171                 gestureAction = GestureAction.GESTURE_DOWN;172                 Toast.makeText(mContext, "向下", Toast.LENGTH_SHORT).show();173             }174             else if(e1.getY() - e2.getY() > getHeight()/2) 175             {176                 gestureAction = GestureAction.GESTURE_TOP;177                 Toast.makeText(mContext, "向上", Toast.LENGTH_SHORT).show();178             }*/179             return false;180         }181     }182     183     public void setOnClickListener(OnBitmapClickListener mOnClickListener) {184         this.mOnClickListener = mOnClickListener;185     }186     187     enum GestureAction188     {189         GESTURE_DEFAULT,190         GESTURE_NONE,191         GESTURE_TOUCH,192         GESTURE_LEFT,193         GESTURE_RIGHT,194         GESTURE_TOP,195         GESTURE_DOWN196     }197     198     199     public interface OnBitmapClickListener{200         void onClick();201     }202     203 }
View Code

 

DrawDemo.rar