Skill Analysis of onfling () function in Android screen gesture swipe

Source: Internet
Author: User

on how to handle gesture operations and the four basic fixed order I will not explain, here jump directly to we get a momentary swipe after the callback onfling () This abstract function, how to accurately determine the direction of the slide according to the parameters. If you don't have the basics in front of you, you can check out 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:

123456 @Override  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  float velocityY) {    //dosomething returnfalse;  }

The only problem that these articles can solve is to teach you how to capture this action when you have gestures, but not to analyze it.

In fact, to really be able to analyze gestures, need to deal with these four parameters motionevent E1, motionevent E2, float Velocityx, float velocityy

Let's look at an example:
123456789101112131415161718192021 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) {            // 切换Activity          // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);          // startActivity(intent);          Toast.makeText(this,"向左手势", Toast.LENGTH_SHORT).show();      }elseif (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {            // 切换Activity          // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);          // startActivity(intent);          Toast.makeText(this,"向右手势", Toast.LENGTH_SHORT).show();             returnfalse}

The four parameters of the onfling mean

E1:the first down motion events that started the fling. Move event at the beginning of the gesture
E2:the Move Motion Event This triggered the current onfling. Move events for the present gesture point
Velocityx:the velocity of this fling measured in pixels per second along the x axis pixels moving in the x-axis direction per second
Velocityy:the velocity of this fling measured in pixels per second along the Y axis. Pixels moving in y direction per second

The simpler point is that the mouse gesture is equivalent to a vector (of course, it is possible that the gesture is a curve), the E1 is the starting point of the vector, the E2 is the end of the vector, the Velocityx is the horizontal velocity of the vector, velocityy the velocity of the vector perpendicular direction

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

The above statement can know what the meaning of it, that is, the horizontal length of the vector (how long to slide) must be greater than verticalmindistance, and the horizontal direction of speed greater than minvelocity.

So we can judge whether a gesture satisfies a certain condition so that it responds appropriately, or it can write a more complex gesture based on this.

Although this article does not explore the basic steps of gesture manipulation, it is still necessary to talk about our listenner when overloaded with Ontouch () This function should consider the question:

123 public boolean onTouch(View v, MotionEvent event) {        returnmGestureDetector.onTouchEvent(event);   }

See the source code of the Gesturedetector class ontouchevent to know, enter the function will enter the case motionevent.action_up this path, so call onfling function.

This is what I want to say, because in my opinion gesturedetector may not be able to meet all the gesture needs, Ken can have one day, we need to throw away gesturedetector directly in the Ontouch () to complete the task.

Skill Analysis of onfling () function in Android screen gesture swipe

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.