Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to detect sliding gestures in Android

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to detect sliding gestures in Android". In daily operation, I believe many people have doubts about how to detect sliding gestures in Android. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to detect sliding gestures in Android"! Next, please follow the editor to study!

Android SDK provides a listener class to detect various gestures:

SimpleOnGestureListener. You only need to realize the gestures you care about.

Swipe is called Fling in android.

First create your own gesture detector class:

Class MyGestureDetector extends SimpleOnGestureListener {

@ Override

Public boolean onFling (MotionEvent E1, MotionEvent e2)

Float velocityX, float velocityY) {

}

An Android sliding gesture has several characteristics, such as a nearly straight line stroke on the x or y axis, the deviation of the midway path should not be too large, and the stroke requires a certain speed, so we define several quantitative measures:

Private static final int SWIPE_MIN_DISTANCE = 120

Private static final int SWIPE_MAX_OFF_PATH = 250

Private static final int

SWIPE_THRESHOLD_VELOCITY = 200

Then in the onFling method, determine whether it is a reasonable swipe action:

If (e1.getX ()-e2.getX ()) > SWIPE_MIN_DISTANCE & & Math.abs

(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

ViewFlipper.setInAnimation (slideLeftIn)

ViewFlipper.setOutAnimation (slideLeftOut)

ViewFlipper.showNext ()

} else if (e2.getX ()-e1.getX ()) > SWIPE_MIN_DISTANCE &

Math.abs (velocityX) > SWIPE_THRESHOLD_VELOCITY) {

ViewFlipper.setInAnimation (slideRightIn)

ViewFlipper.setOutAnimation (slideRightOut)

ViewFlipper.showPrevious ()

}

If (e1.getX ()-e2.getX ()) > SWIPE_MIN_DISTANCE &

Math.abs (velocityX) > SWIPE_THRESHOLD_VELOCITY) {

ViewFlipper.setInAnimation (slideLeftIn)

ViewFlipper.setOutAnimation (slideLeftOut)

ViewFlipper.showNext ()

} else if (e2.getX ()-e1.getX ()) > SWIPE_MIN_DISTANCE &

Math.abs (velocityX) > SWIPE_THRESHOLD_VELOCITY) {

ViewFlipper.setInAnimation (slideRightIn)

ViewFlipper.setOutAnimation (slideRightOut)

ViewFlipper.showPrevious ()

}

ViewFlipper here is a container with multiple view. You can easily call prev/next view. With animation animation, you can achieve some good results:

ViewFlipper = (ViewFlipper) findViewById (R.id.flipper)

SlideLeftIn = AnimationUtils.loadAnimation

(this, R.anim.slide_left_in)

SlideLeftOut = AnimationUtils.loadAnimation

(this, R.anim.slide_left_out)

SlideRightIn = AnimationUtils.loadAnimation

(this, R.anim.slide_right_in)

SlideRightOut = AnimationUtils.loadAnimation

(this, R.anim.slide_right_out)

A custom animation can view a specific XML, such as an animation coming in from the left:

< set xmlns:android="http://schemas. android.com/apk/res/android">

< translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="800"/>

< /set>

Of course * Don't forget to override onTouch the gesture action in your Activity:

@ Override public boolean onTouchEvent (MotionEvent event) {if (gestureDetector.onTouchEvent (event)) return true; else return false;} this is the end of the study on "how to detect sliding gestures in Android", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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: 266

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report