Multi-Touch
1. Multi-touch literally means that you use a finger that is greater than or equal to 2 to touch the phone screen.
The listening touch event in Android is the Ontouchevent method, its parameters are motionevent, and the following are some common methods of motionevent:
Getpointercount () Gets the number of points in the touch screen.
Getpointerid (int pointerindex) returns the indication of a touch point, PointIndex is the index of the first few touch points
GetX () gets the x-coordinate value of the touch screen
GetY () Gets the y-coordinate value of the touch screen
Getaction () Get touch screen action
Action_down: Start by pressing the action, such as pressing the screen with your finger.
ACTION_UP: Press the action to complete, such as the finger stops pressing the screen, leaving the screen.
Action_move: The movement between the beginning and completion of an action, such as a finger sliding on the screen.
Motionevent.action_pointer_1_down: First touch Point Click event
Motionevent.action_pointer_2_down: Second touch point Click event
MOTIONEVENT.ACTION_POINTER_1_UP: First touch Point release event
MOTIONEVENT.ACTION_POINTER_2_UP: Second touch point release event
2. An example is given below to illustrate:
Note: Ontouchevent returns true if it is not an effect
1 Public classMyViewextendsview{2 3 PublicMyView (context context, AttributeSet attrs,intDefstyle) { 4 Super(context, attrs, Defstyle); 5 //TODO auto-generated Constructor stub6 } 7 8 PublicMyView (Context context, AttributeSet attrs) {9 Super(context, attrs); Ten //TODO auto-generated Constructor stub One } A - PublicMyView (Context context) { - Super(context); the //TODO auto-generated Constructor stub - } - - @Override + protected voidOnDraw (canvas canvas) { -Paint Mpaint =NewPaint (); + Mpaint.setcolor (Color.gray); AMpaint.settextsize (30); atCanvas.drawtext ("hehe", 100, 200, Mpaint); - } - - @Override - Public Booleanontouchevent (Motionevent event) { - in intPointcount =Event.getpointercount (); - System.out.println (Pointcount); to if(Pointcount = = 2) { + Final floatx0 = event.getx (Event.getpointerid (0)); - Final floaty0 = event.gety (Event.getpointerid (0)); the * Final floatX1 = Event.getx (Event.getpointerid (1)); $ Final floatY1 = Event.gety (Event.getpointerid (1)); Panax Notoginseng - Switch(Event.getaction ()) { the CaseMotionevent.action_pointer_2_down: +System.out.println ("Action_pointer_2_down"); A Break; the CaseMotionevent.action_pointer_1_down: +System.out.println ("Action_pointer_1_down"); - Break; $ Casemotionevent.action_pointer_1_up: $System.out.println ("Action_pointer_1_up"); - Break; - Casemotionevent.action_pointer_2_up: theSystem.out.println ("Action_pointer_2_up"); - Break; Wuyi CaseMotionevent.action_move: { theSystem.out.println ("Action_move"); - Break; Wu } - } About $System.out.println (x0+ "" +x1+ "" +y0+ "" +y1); - } - - return true; A } + the - $ the}
Android Multi-Touch