In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to use gesture detection and page turning function through gestures in Android development". The editor shows you the operation process through actual cases, the operation method is simple and fast, and practical. I hope that this article "how to use gesture detection and page turning function through gestures in Android development" can help you solve the problem.
The details are as follows:
Gesture refers to the continuous touching of a user's finger or stylus on the touch screen, such as a gesture drawn from left to right on the screen, or drawing a circle on the screen. The continuous touch of gestures will not only form a movement trend in a certain direction, but also form an irregular geometric figure. Android provides support for both gesture behaviors:
1. For the first gesture behavior, Android provides gesture detection and corresponding listeners for gesture detection.
two。 For the second gesture behavior, Android allows developers to add gestures and provides the corresponding API gestures that identify the user.
Gesture detection
Android provides a GestureDetector class for gesture detection, and the GestrueDetector instance represents a gesture detector. When you create a GestureDetector, you need to pass an GestureDetector.OnGestrureListener instance. GestureDetector.OnGestrueListener is a listener that is responsible for responding to the user's gesture behavior.
The event handling methods included in GestrueDetector.OnGestureListener are as follows.
Boolean onDown (MotionEvent e): this method is triggered when the touch event is pressed.
Boolean onFling (MotionEvent E1 MotionEvent E2 float velocitX,floatvelocity): this method is triggered when the user drags over the touch screen. Where velocityX,velocityY represents the speed of dragging the movement horizontally and vertically.
Abstract void onLongPress (MotionEvent e): this method is triggered on time when the user is long on the screen.
Boolean onScroll (MotionEvent E1 MotionEvent E2 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 touchscreen and has not yet moved or released.
Boolean onSingleTapUp (MotionEvent e): the user's tapping event on the touchscreen will trigger this method.
Gesture detection using Android requires only two steps:
1. Create a GestureDetector. You must implement an instance of a GestureDetector.OnGestureListener listener when creating this object.
two。 Bind the listener to the TouchEvent event of the application's Activity, specifying in the event handling that the TouchEvent event on the Activity is handed over to the GestureDetector for processing.
After the above two steps, the TouchEvent event on Activity is handed over to GestureDetector, and GestureDetector detects whether a specific gesture action has been triggered.
Example: realize the effect of turning pages through gestures
Idea: give the TouchEvent of Activity to GestureDetector for processing. What is special about this program is that the program uses a ViewFlipper component, and the ViewFlipper component is actually a container component, so you can call addView (View v) to add multiple components. Once multiple components are added to the ViewFlipper, ViewFlipper can use animation to control the switching effect between multiple components.
In this example, the user's gesture is detected by GestureDetector, and the switching of View components contained in ViewFlipper is controlled according to the gesture, so as to achieve the effect of page turning.
The key code is as follows:
Public Boolean onFling (MotionEvent event1,MotionEvent event2,float velocityX,velocity) {if (event1.getX ()-event2.getX () > FLIP_DISTANCE) {flipper.setInAnimation (animations [0]); flipper.setOutAnimation (animations [1]); flipper.showPrevious (); return true;} else if (event2.getX ()-event1.getX () > FLIP_DISTANCE) {flipper.setInAnimation (animations [2]); flipper.setOutAnimation (animation [3]); flipper.showNext () Return true;} return false;}
Where:
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)
This is the end of the content on "how to use gestures to detect and turn pages through gestures in Android development". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 278
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.