In a simple way, the gesture zoom-in and zoom-out function is implemented on android.

Source: Internet
Author: User
Tags gety

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 ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.