Teach you how to use Android application gesture programming

Android Gesture Programming Case Teaching

1. Introduction

Modern smart phones all support touch screen gesture operation, which can conveniently realize various functions, and the most common one is to realize the switching function by sliding gestures up, down, left, and right.

2. Explanation

(1) Android gesture operation principle

  • In the Android system, each gesture interaction will be executed in the following order, which can be said to be the principle of Android gesture operation.
  • The moment the screen is touched, a MotionEvent event is triggered. The event is monitored by OnTouchListener, and the MotionEvent object is obtained in its onTouch() method.
  • Pass this MotionEvent object to OnGestureListener through GestureDetector (gesture detector).
  • The OnGestureListener listener obtains the event object, and then makes appropriate processing according to the information encapsulated by the object.

(2) Android gesture classes and interfaces

1、MotionEvent
  • The action event class is used to encapsulate action events such as gestures, touch pens, trackballs, etc. It internally encapsulates two important attributes X and Y, which are used to record the coordinates of the horizontal and vertical axes respectively.
2、GestureDetector
  • Gesture detector, used to recognize various gestures.
3、OnGestureListener
  • The gesture listener is a gesture interaction monitoring interface, which provides multiple abstract methods, and calls the corresponding method according to the gesture recognition result of GestureDetector.

1. Create an Android application

  • here based onEmpty ActivityTemplates to create Android apps -SwtichBelleImageByGesture
  • Click Finish
    insert image description here
    insert image description here

Prepare image material

  • Copy the prepared image material todrawablein the directory
    insert image description here

3. String resource file

  • String resource file −strings.xml
    insert image description here
<resources>
    <string name="app_name">通过手势切换奥特曼图片</string>
</resources>

4. Main layout resource file

  • Main layout resource fileactivity_main.xml

insert image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/img1"
    android:orientation="vertical"
    tools:context=".MainActivity">
    
</LinearLayout>
  • Run to view the preview effect
    insert image description here

5. The main interface class realizes the function

  • Main interface class -MainActivity
    insert image description here

  • Define constants and variables
    insert image description here

  • Get control instance by resource identifier
    insert image description here

  • Initialize an array of image resource identifiers
    insert image description here

  • Instantiate the gesture detector, each event processing method outputs a debug message
    insert image description here

  • For gesture sliding, we write the onFling() event processing method to switch pictures through gestures. This event processing method has four parameters, and the sliding starting point information is encapsulated ine1In, the sliding end point information is encapsulated ine2Here, the third parameter is the horizontal sliding speed of the gesture, and the fourth parameter is the vertical sliding speed of the gesture
    insert image description here

  • Handle the touch event of the window to the gesture detector
    insert image description here

6. Start the application and check the effect

  • Use gestures to swipe left and right to switch pictures
    insert image description here

Guess you like

Origin blog.csdn.net/qq_64505257/article/details/127671837