1. The basic

For details about the event distribution and processing mechanism of custom View, see Android Custom View(9) : Event Distribution & Processing


2. Introduction to gestures

  1. GestureDetector is a gesture monitoring class, which can monitor a series of actions after the finger touches the screen: press, short press, long press, slide, single finger lift, etc.

  2. ScaleGestureDetector is a zoom gesture listening class, which can listen for multiple zoom events. Its usage is basically the same as GestureDetector.

  3. The GestureDetector has two listening interfaces, OnGestureListener and OnDoubleTapListener, which are set in the constructor and setOnDoubleTapListener () respectively to get the callback for the corresponding event sequence.

  4. Long press the gesture to be separate set open listening, can corresponding event callback methods: mGestureDetector. SetIsLongpressEnabled (true)

  5. The zoom gesture usually only deals with onScale()

3. GestureDetector uses a trilogy

Please refer to the code remarks for specific usage explanation and points of attention

3.1 the initialization

In the View or Activity that needs to listen for gestures, declare the object and initialize it

/ / 1. The statement
GestureDetector mGestureDetector;
/ / 2. The initialization
mGestureDetector = new GestureDetector(this ,this);
mGestureDetector.setOnDoubleTapListener(this);
mGestureDetector.setIsLongpressEnabled(true);// Long press is allowed
/ / 3. The corresponding class implements the interface GestureDetector. OnGestureListener, GestureDetector. OnDoubleTapListener.Copy the code

3.2 Touch event handover

// Override the onTouchEvent() of the View or Activity
@Override
public boolean onTouchEvent(MotionEvent event) {
    mGestureDetector.onTouchEvent(event);
    return true;
}
Copy the code

The required functions are implemented in 3.3 interface callback methods

/ / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
/ / = = = = = = = = = = = = = = = = = = = = = = = = = = = OnDoubleTapListener method callback = = = = = = = = = = = = = = = = = = = = = = = = =
/ / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    @Override
    public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
        Log.e(TAG, "onSingleTapConfirmed " );
        mTvStatus.append("Click event \n");
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent motionEvent) {
        Log.e(TAG, "onDoubleTap: " );
        mTvStatus.append("Double click event \n");
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent motionEvent) {
        Log.e(TAG, "onDoubleTapEvent: " );
        // Double-click the Event callback in the middle of the interval
        return true;
    }
/ / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
/ / = = = = = = = = = = = = = = = = = = = = = = = = = = = OnGestureListener method callback = = = = = = = = = = = = = = = = = = = = = = = = =
/ / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    @Override
    public boolean onDown(MotionEvent motionEvent) {
        Log.e(TAG, "onDown: " );
        mTvStatus.append("====================\n");
        mTvStatus.append("Sequence events: Press \n");
        return true;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {
        Log.e(TAG, "onShowPress: " );
        mTvStatus.append("Sequence events: Press screen \n");
    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        Log.e(TAG, "onSingleTapUp: " );
        mTvStatus.append("Sequence event: single finger lift \n");
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        Log.e(TAG, "onScroll: " );
        mTvStatus.append("Sequence events: Finger slide \n");
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {
        Log.e(TAG, "onLongPress: " );
        mTvStatus.append("Sequence events: Long press \n");
    }

    // velocityX: X direction velocity, unit: px/s
    // velocityY: indicates the speed in the Y-axis, in px/s
    @Override
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float velocityX, float velocityY) {
        Log.e(TAG, "onFling: " );
        mTvStatus.append("Sequence event: Finger fly \n");
        return true;
    }
Copy the code

4. ScaleGestureDetector use

Pay attention to code comments

4.1 the initialization

    / / 1. The statement
    ScaleGestureDetector mScaleGestureDetector;
    / / 2. The initialization
    mScaleGestureDetector = new ScaleGestureDetector(this ,this);
    //3. The corresponding class implements the OnScaleGestureListener interface
Copy the code

4.2 Event Handover

  @Override
    public boolean onTouchEvent(MotionEvent event) {
        mGestureDetector.onTouchEvent(event);
        mScaleGestureDetector.onTouchEvent(event);
        return true;
    }
Copy the code

4.3 Callback processing


/ / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
/ / = = = = = = = = = = = = = = = = = = = = = = = = = = = OnScaleGestureListener method callback = = = = = = = = = = = = = = = = = = = = = = = = =
/ / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    @Override
    public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
        Log.e(TAG, "onScale: " );
        mTvStatus.append("Sequence events: Scale ing\n");
        return true;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
        Log.e(TAG, "onScaleBegin: " );
        mTvStatus.append("Sequence events: Scale begin\n");
        return true;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector scaleGestureDetector) {
        Log.e(TAG, "onScaleEnd: " );
        mTvStatus.append("Sequence events: Scale end\n");
    }
Copy the code

Practice of 5.

Best practice for gesture listening & handling: Implement a custom ImageView

  1. Double refers to zoom
  2. Drag and drop to move
  3. Click the event callback (see the larger image of the Activity to finish)
  4. Double-click the event callback to restore the image

6. Layout and other code


      
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/tv_status"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gesture monitor"
        />

</RelativeLayout>
Copy the code