In a simple way, the gesture zoom-in and zoom-out function is implemented on android.
Package com. example. eventtest; import java. util. arrayList; import android. OS. bundle; import android. app. activity; import android. gesture. gestureOverlayView; import android. gesture. gestureOverlayView. onGestureListener; import android. graphics. point; import android. view. menu; import android. view. motionEvent; import android. view. view; import android. view. view. onClickListener; import android. widget. toast; public class MainActivity extends Activity {double nLenStart = 0; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main) ;}@ Overridepublic boolean onTouchEvent (MotionEvent event) {int nCnt = event. getPointerCount (); int n = event. getAction (); if (event. getAction () & MotionEvent. ACTION_MASK) = MotionEvent. ACTION_POINTER_DOWN & 2 = nCnt) // 2 indicates two fingers {for (int I = 0; I <nCnt; I ++) {float x = event. getX (I); float y = event. getY (I); Point pt = new Point (int) x, (int) y);} int xlen = Math. abs (int) event. getX (0)-(int) event. getX (1); int ylen = Math. abs (int) event. getY (0)-(int) event. getY (1); nLenStart = Math. sqrt (double) xlen * xlen + (double) ylen * ylen);} else if (event. getAction () & MotionEvent. ACTION_MASK) = MotionEvent. ACTION_POINTER_UP & 2 = nCnt) {for (int I = 0; I <nCnt; I ++) {float x = event. getX (I); float y = event. getY (I); Point pt = new Point (int) x, (int) y);} int xlen = Math. abs (int) event. getX (0)-(int) event. getX (1); int ylen = Math. abs (int) event. getY (0)-(int) event. getY (1); double nLenEnd = Math. sqrt (double) xlen * xlen + (double) ylen * ylen); if (nLenEnd> nLenStart) // start distance and end distance by two fingers, to determine whether to zoom in or out {Toast. makeText (getApplicationContext (), "zoom in", 3000 ). show ();} else {Toast. makeText (getApplicationContext (), "narrow down", 3000 ). show () ;}} return super. onTouchEvent (event );}}
Basic knowledge:
The getAction () method returns the int type and uses only the lower 16 bits. The lower eight bits are the action type, the top 8 bits indicate the index value of the touch point (single point is 0, double point is 1)
Obtain the action type: int action = event. getAction () & ACTION_MASK or use getActionMasked ()
Obtain the index type of the touch point: int pointerIndex = (event. getAction () & ACTION_POINTER_INDEX_MASK)> ACTION_POINTER_INDEX_SHIFT
Or use getActionIndex ()