The following are all converted from the android game programming entry-level classic. For more information, see the source.
Touch screen is the most important way to obtain user input. Multi-touch is not introduced until android2.0.
First, test the single-touch event. It applies to all android versions. We register an ontouchlistener interface in the view and pass the touch time to this interface for implementation. The ontouchlistener interface has only one method: Public Abstract Boolean ontouch (view V, motionevent event)
The first parameter is the view for dispatching the touch event, and the second parameter is the parameter for obtaining the touch event.
Ontouchlistener can be registered using the view. setontouchlistener method in any view implementation. The ontouchlistener method is called before the motionevent is assigned to the view itself. We can return true in the ontouch () method to notify the view that we have handled the event. If the returned value is false, the view automatically processes the event.
The motionevent instance contains the following three methods:
Motionevent. getx () and motionevent. Gety (); these two methods report the X and Y coordinates of the touch event relative to the view. The coordinate origin is located in the upper left corner of the view. The X axis points to the right and the Y axis points to the bottom. Coordinates are measured in pixels. This method returns floating point data, so the coordinates have sub-pixel precision.
Motionevent. getaction (): Type of the returned touch event. It is an integer with one of the following values: motionevent. action_down,
Motionevent. action_move, motionevent. action_cancel, and motionevent. action_up.
As the name suggests, when you touch the screen with your finger, the motionevent. action_down event is triggered.
When the finger moves, the motionevent. action_move event is triggered. As long as the finger is not completely out of the screen, the motionevent. action_move event can always be obtained.
When the finger leaves the screen again, the motionevent. action_up event is triggered.
The motionevent. action_cancel event is a little mysterious. This document describes the event triggered when the current gesture is canceled. We still assume it is a motionevent. action_up event.
The following is the test code:
package org.example.ch04_android_basics;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.TextView;public class SingleTouchTest extends Activity implements OnTouchListener{StringBuilder builder = new StringBuilder();TextView textView;@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubbuilder.setLength(0);switch(event.getAction()){case MotionEvent.ACTION_DOWN:builder.append("down, ");break;case MotionEvent.ACTION_MOVE:builder.append("move, ");break;case MotionEvent.ACTION_CANCEL:builder.append("cancle, ");break;case MotionEvent.ACTION_UP:builder.append("up, ");break;}builder.append(event.getX());builder.append(", ");builder.append(event.getY());String text = builder.toString();Log.d("TouchTest", text);textView.setText(text);return true;}@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);textView = new TextView(this);textView.setText("Touch and drag (one finger only)!");textView.setOnTouchListener(this);setContentView(textView);}}
The running effect is to display the trigger event and corresponding coordinates,
The processing of multi-touch events is complicated. In Android, the multi-touch events are modified, new methods and constants are added, and constants are even renamed. These changes may make it easier to process multiple touch points. However, only Versions later than Android are supported. To support Android -- Android 2.21, we use the android API.
When processing multi-touch events, we use the overload method, which carries a so-called pointer index, such as event. getx (pointerindex );
Pointindex is an index in the internal array of motionevent. It contains the coordinates of a screen event with a specified finger. One finger on the screen is the pointer ID. Pointer ID is an instance of a pointer that uniquely identifies a touch screen. There is a method motionevent. getpointeridentifier (INT pointerindex), which returns a pointer id based on the pointer index. As long as the finger is still on the screen, a pointer id remains the same as a finger, and the pointer index is not necessarily like this. Let's take a look at how to obtain the pointer index:
Int pointerindex = (event. getaction () & motionevent. action_pointer_id_mask)> motionevent. action_pointer_id_shift;
The constant value of action_pointer_id_mask is 0xff00, so the low 8 bits are 0, and the high 8 bits are 15, which is used to save the pointer index of the event.
The 8-bit lower integer value can be returned from the event. getaction () method to save the event type value. We use motionevent. action_pointer_id_shift to shift. This value is 8, so we actually shift 15th to 8th bits and 7th to 0th bits. Note that we obtain pointerindex, but the constant is xxx_pointer_id_xxx instead of xxx_pointer_index_xxx.
To obtain the event type, we only need to block the pointer index:
Int action = event. getaction () & motionevent. action_mask;
Here we will encounter a new event type,
Motionevent. action_pointer_down: This event occurs when any finger except the first finger touches the screen, and the first finger still produces the motionevent. action_down event.
Motionevent. action_pointer_up: This event occurs when multiple fingers touch the screen and one finger leaves the screen. When the last finger leaves the screen, the motionevent. action_up event is generated, and this finger is not necessarily the first finger to touch the screen. To check that a single motionevent contains several events, you can use the motionevent. getpointercount () method, which tells us the coordinates of how many fingers are contained in the motionevent. Then we can use motionevent. getx (),
The motionevent. Gety () and motionevent. getpointerid () methods are used to obtain the pointer ID and coordinate values from the pointer index 0 to motionevent. getpointercount ()-1.
The test code is as follows:
package org.example.ch04_android_basics;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.TextView;public class MultiTouchTest extends Activity implements OnTouchListener {StringBuilder builder = new StringBuilder();TextView textView;float[] x = new float[10];float[] y = new float[10];boolean[] touched = new boolean[10];int[] id = new int[10];private void updateTextView(){builder.setLength(0);for(int i = 0; i < 10; i++){builder.append(touched[i]);builder.append(", ");builder.append(id[i]);builder.append(", ");builder.append(x[i]);builder.append(", ");builder.append(y[i]);builder.append("\n");}textView.setText(builder);}@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);textView = new TextView(this);textView.setText("Touch and drag (multiple fingers supported)!");textView.setOnTouchListener(this);setContentView(textView);for(int i = 0; i < 10; i++){id[i] = -1;}updateTextView();}@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubint action = event.getAction() & MotionEvent.ACTION_MASK;int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >>MotionEvent.ACTION_POINTER_ID_SHIFT;int pointerCount = event.getPointerCount();for(int i = 0; i < 10; i++){if(i >= pointerCount){touched[i] = false;id[i] = -1;continue;}if(event.getAction() != MotionEvent.ACTION_MOVE && i != pointerIndex){/* If it's an up/down/cancel/out event, mask the id to see if we should process it for this touch point */continue;}int pointerId = event.getPointerId(i);switch(action){case MotionEvent.ACTION_DOWN:case MotionEvent.ACTION_POINTER_DOWN:touched[i] = true;id[i] = pointerId;x[i] = (int)event.getX(i);y[i] = (int)event.getY(i);break;case MotionEvent.ACTION_UP:case MotionEvent.ACTION_POINTER_UP:case MotionEvent.ACTION_OUTSIDE:case MotionEvent.ACTION_CANCEL:touched[i] = false;id[i] = -1;x[i] = (int)event.getX(i);y[i] = (int)event.getY(i);break;case MotionEvent.ACTION_MOVE:touched[i] = true;id[i] = pointerId;x[i] = (int)event.getX(i);y[i] = (int)event.getY(i);break;}}updateTextView();return true;}}
Running effect: