I won't talk about how to handle gesture operation and the four basic fixed orders , Here we jump directly to the callback after we get the instant slide onFling() This abstract function , How to judge the sliding direction accurately according to the parameters . If you don't have the previous basic knowledge , You can read this article :http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1020/448.html

I see most of the information on the Internet , The implementation of this abstract function is quite simple :

1
2
3
4
5
6
@Override  
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  float velocityY) {  
   
//dosomething 
return false ;  
}

There is only one problem that these articles can solve , That's how to teach you when you have gestures , This action was captured , But I didn't analyze it .

Actually, if you can really analyze gestures , We need to deal with these four parameters MotionEvent e1, MotionEvent e2, float velocityX, float velocityY

  Let's start with an example :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private int verticalMinDistance = 20; 
private int minVelocity         = 0; 
    
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
    
     if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) { 
    
         // Switch Activity 
         // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class); 
         // startActivity(intent); 
         Toast.makeText( this , " Gesture left " , Toast.LENGTH_SHORT).show(); 
     } else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) { 
    
         // Switch Activity 
         // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class); 
         // startActivity(intent); 
         Toast.makeText( this , " Gesture to the right " , Toast.LENGTH_SHORT).show(); 
    
    
     return false
}

OnFling The four parameters of are

e1: The first down motion event that started the fling. The movement event of the starting point of the gesture
e2: The move motion event that triggered the current onFling. The movement event of the current gesture point
velocityX: The velocity of this fling measured in pixels per second along the x axis. Per second x Pixels moving along the axis
velocityY: The velocity of this fling measured in pixels per second along the y axis. Per second y Pixels moving along the axis

To put it more simply , The mouse gesture is equivalent to a vector ( Of course, it's possible that the gesture is a curve ),e1 Is the starting point of the vector ,e2 Is the end of the vector ,velocityX Is the horizontal velocity of the vector ,velocityY Is the velocity perpendicular to the vector

1
if (e1.getX()
- e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity)

The above sentence can know what it means , That is, the horizontal length of the vector ( How long did it slide ) Must be greater than verticalMinDistance, And the horizontal velocity is greater than minVelocity.

So we can judge whether the gesture meets certain conditions and respond accordingly , You can also write more complex gestures based on this .

Although I'm not going to explore the basic steps of gesture manipulation in this article , But it's still necessary to talk about our listenner Under heavy load onTouch() The problem that should be considered when using this function :

1
2
3
public
boolean onTouch(View v, MotionEvent event) {   
     return mGestureDetector.onTouchEvent(event);   
}

see GestureDetector Class onTouchEvent Source code can know , After entering this function, you will enter case MotionEvent.ACTION_UP This path , To call onFling function .

That's what I'm going to say , Because it seems to me GestureDetector It may not be enough to handle all gestures , Ken can one day , We need to get rid of GestureDetector   Directly in onTouch() It's inside to finish the task .

Android The screen is sliding onFling() Function skills analysis of more related articles

  1. Android: Implementation of gesture sliding event processing method

            First of all Activity Must be realized OnGestureListener Interface , This interface provides some methods about gesture manipulation , onDown Method :onDown yes , Once the touch screen is pressed , Right away onDown event ...

  2. Android Summary of common experience and skills in mobile application development

    turn :http://wwwdevstorecn/essay/essayInfo/6128.html 1. The solution to conversation maintenance . requirement : 1.app Use in webview Visit specific website content , however app And services ...

  3. javaScript in with Function usage example analysis

    javaScript Medium with function , That is to say with sentence , It can be easily used to refer to existing properties in a specific object , But it can't be used to add properties to an object , To create new properties for an object , The object must be explicitly referenced . with Letter ...

  4. javascript gesture (swipeLeft,swipeRight) Use in sliding css3 Cartoon carton , Turn on hardware acceleration

    today , Working on a mobile project , Yes css3 The phenomenon of cartoon Caton . Case diagram : In the gesture slide (swipeLeft,swipeRight) Encountered the phenomenon of animation stuck , Finally used css3 Animation -webkit-transform ...

  5. Android Gesture slide of development ( Slide gesture monitoring ) Detailed explanation

    Android Gesture slide of development ( Slide gesture monitoring ) Detailed explanation stay Android Application , We often need gesture sliding operation , Like sliding up and down , Or slide left and right , There are usually two ways to deal with gesture sliding : One is to implement it alone setOnTouchListen ...

  6. Android The two screens slide each other

    Android The two screens slide each other The following is from : http://blog.csdn.net/song_shi_chao/article/details/7081664 ----------------- ...

  7. Eclipse Pass through Android Simulator calls OpenGL ES2.0 Function operation steps

    Original address : Eclipse Pass through Android Simulator calls OpenGL ES2.0 Function operation steps - Network resources are infinite - Blog channel - CSDN.NET http://blog.csdn.net/fen ...

  8. Android Gesture slide Tab

    Android Gesture slide Tab //MainActivity.java public class MainActivity extends TabActivity { ; ; ; private Ges ...

  9. Android Studio How to see which classes resources or functions are referenced in

    A lot of people are doing Keymap Match to Eclispe After the shortcut key , Find shortcuts to see where resources or functions are referenced "Ctrl+Shift+G" Not working 了 . When you select a function , Use this shortcut . It's just that ...

Random recommendation

  1. ubuntu On the use of eclipse build java、python development environment

    The last article talked about how to windwos On the use of eclipse build java.python development environment , This lecture will focus on how to ubuntu On the Internet , I use virtual machine to install ubuntu System , The system version is :14.04 lts ...

  2. iOS Develop zero base --Swift course optional type

    Introduction of optional types Be careful : When type is optional swift One of the more difficult points to understand in English Let's get to know for a while , Make more use of Xcode To use With the deepening of learning , Slowly understand the principles and benefits Concept : stay OC In development , If a variable is suspended and not used , can ...

  3. Android Wireless debugging

    Method 1 : 1. Use USB The cable connects the device . 2. Command input adb tcpip 5555 ( 5555 Is the port number , You are free to specify ). 3. To break off USB data , Now you can connect what you need to connect |USB equipment . 4. Again ...

  4. ubuntu apt-get install php

    sudo add-apt-repository ppa:ondrej/php apt-get upgrade apt-get update

  5. Custom workflow activities report errors : You can't log in to the system . The reason may be that your user record or your business department is in Microsoft Dynamics 365 Has been disabled in .

    My official account is WeChat and Yi Shun. : Microsoft News CRM Expert Luo Yong , reply 265 perhaps 20170926 This article is easy to access , At the same time, I can get the latest blog information in the first room ,follow me! My website is www.luoyong.me ...

  6. ASP.NET MVC Framework development series

    This series of tutorials are used in my work and recorded , If there are mistakes , Please correct me List of articles MVC4 The opening Chapter one First time to know MVC4 Chapter two When you go down the mountain, you meet a tiger (@helper) The third chapter Models Module properties Chapter four stay MVC ...

  7. Baidu Maps API Example : Use vue Add or remove covers

    1.index.html <script type="text/javascript" src="http://api.map.baidu.com/api?v=2. ...

  8. Activity Service -1 DynamicBpmnService Service

    The service is 5.19 A new service added after the release , and RepositoryService It's similar , It's all about process definition , But it's totally different . It's dynamic by name BPMN service , Look at the methods inside. They are all about changing the relevant properties of the process . This side ...

  9. linux Use in rm Command to move files to the recycle bin

    Today, under the terminal , I see a... In my user directory - Folder (maven Generate ), To delete the reclaim point space , Habitually using orders rm -rf ~ , A return train , It's too late to suddenly think of it , All of a sudden, the world is quiet , I want to die ! you 're right , ...

  10. Page elements CSS Rendering priority

    The priority of the style Multiple styles (Multiple Styles): If the external style . Inner styles and inline styles apply to the same element at the same time , It's the case of multiple styles . In general , The priorities are as follows :( External style )External style ...