Gesture detection developed by Android and method for realizing page turning function through gesture

This paper describes the method of gesture detection and page turning function through gesture developed by Android. Share with you for your reference, as follows:

Gesture refers to the continuous touch of the user's finger or stylus on the touch screen. For example, an action drawn from left to right on the screen is a gesture. For example, drawing a circle on the screen is also a gesture. The continuous touch of gesture will form a moving trend in a certain direction and an irregular geometry. Android supports both gesture behaviors:

1. For the first gesture behavior, Android provides gesture detection and a corresponding listener for gesture detection.

2. For the second gesture behavior, Android allows developers to add gestures and provides corresponding APIs to recognize users' gestures.

Gesture detection

Android provides a gesturedetector class for gesture detection. The gesturedetector instance represents a gesture detector. When creating a gesturedetector, you need to pass in a gesturedetector.ongestrelistener instance. Gesturedetector.ongestrelistener is a listener responsible for providing responses to user gesture behavior.

The event handling methods contained in gestruedetector.ongesturelistener are as follows.

Boolean ondown (motionevent E): this method is triggered when a touch event is pressed. Boolean onfling (motionevent E1, motionevent E2, float velocitx, floatvelocity): this method is triggered when the user drags across the touch screen. Where velocityx and velocityy represent the horizontal and vertical speeds of the drag action. Abstract void onlongpress (motionevent E): this method is triggered when the user is on the screen for a long time. Boolean onscroll (motionevent E1, float distance, float distance): this method is triggered when the user scrolls on the screen. Void onshowpress (motionevent E): this method is triggered when the user presses on the touch screen and has not moved or released. Boolean onsingletapup (motionevent E): the user's tap event on the touch screen will trigger this method.

Gesture detection using Android requires only two steps:

1. Create a gesturedetector. When creating this object, you must implement a gesturedetector.ongesturelistener listener listener instance.

2. Bind the listener for the touchevent event of the activity of the application, and specify in the event processing that the touchevent event on the activity should be handed over to the gesturedetector for processing.

After the above two steps, the touchevent event on the activity will be handed over to the gesturedetector for processing, and the gesturedetector will detect whether a specific gesture action is triggered.

Example: realize page turning effect through gesture

Idea: hand over the touchevent of activity to gesturedetector. The special feature of this program is that it uses a viewflipper component, which is actually a container component. Therefore, addview (view v) can be called to add multiple components. Once multiple components are added to viewflipper, Viewflipper can use animation to control the switching effect between multiple components.

This example uses the gesturedetector to detect the user's gesture action, and controls the switching of the view components contained in the viewflipper according to the gesture action, so as to realize the page turning effect.

The key codes are as follows:

Of which:

animations[0]=AnimaionUtils.loadAnimation(this,R.anim.left_in); animations[1]=AnimaionUtils.loadAnimation(this,R.anim.left_out); animations[2]=AnimaionUtils.loadAnimation(this,R.anim.right_in); animations[3]=AnimaionUtils.loadAnimation(this,R.anim.right_out);

More readers interested in Android related content can view the special topics of this site: summary of Android gesture operation skills, summary of Android basic component usage, introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solutions, summary of Android view skills, summary of Android layout skills, and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>