The Velocitytracker of Android learning

Source: Internet
Author: User

I learned a little bit about it today. Velocitytracker, which is used to monitor gesture speed, can have a buffered movement during the view movement. I think the initial use is very simple.
The first move depends on the Scroller class, Scroller has a fling method, the motionevent up after the detection of the gesture speed provided to scroller, you can complete their own scrolling. 1. Initialize some of the variables:Private voidinit (Context context) { Mscroller = new Scroller (GetContext ()); Final viewconfiguration configuration = viewconfiguration. Get  (context); mminimumvelocity = configuration.getscaledminimumflingvelocity (); mmaximumvelocity = configuration.getscaledmaximumflingvelocity ();    } Configuration.getscaledtouchslop ()//Get the distance you can swipe with gestures
configuration.getscaledminimumflingvelocity ()//Get the minimum speed value allowed to perform a fling gesture action
configuration.getscaledmaximumflingvelocity ()//Get the maximum speed value allowed to perform a fling gesture action
2. Handling In touch events:@Override  Public boolean ontouchevent(motionevent event) {        Obtainvelocitytracker (event); InitializeMvelocitytracker         int action = event.getaction (); Switch (action) {Case motionevent. Action_down: Starty = event.gety (); if (mscroller. isfinished ()) { mscroller. abortanimation ();                }Break ; Case motionevent. action_move: //pseudo-instantaneous speed float movey = event.gety ();scrollTo (0, (int) (getscrolly () + (starty -Movey)); Starty = event.gety ();Break ; Case motionevent. action_up: final velocitytracker velocitytracker = Mvelocitytracker; velocitytracker.computecurrentvelocity ( mmaximumvelocity); int initialvelocity = (int) velocitytracker.getyvelocity (); Get the speed of the y-axis if (Math. ABS(initialvelocity) > mminimumvelocity) && getchildcount () > 0) {Fling (-initialvelocity); Start scrolling                }  Case motionevent. action_cancel:Releasevelocitytracker (); To releaseBreak ;         }   return true;     }  
/*** ReleaseMvelocitytracker */ private void releasevelocitytracker () { if(null ! = Mvelocitytracker) { Mvelocitytracker. Clear (); mvelocitytracker. Recycle (); Mvelocitytracker = null;             }        }  
/*** Release Initialization */ private void Obtainvelocitytracker (motionevent event) { if(null = = Mvelocitytracker) { Mvelocitytracker = Velocitytracker. obtain  ();         }   mvelocitytracker. Addmovement (event);     }  
The down and move events are simple follow gestures moving, and the main event handling is in the up. The scrolling event handling is in the Fling method: public void Fling (int velocityy) { Mscroller. Fling (Getscrollx (), getscrolly (), 0, velocityy, 0, 0,-1080, 0); is a way of scroller, very simple. awakenscrollbars (mscroller. getduration ()); invalidate ();   }
Fling (Getscrollx (), getscrolly (), 0, velocityy, 0, 0, -1080, 0);
Min y Max y
My demo is just a simple scroll on the y-axis, so I set the minimum maximum movement distance on the y axis. The minimum y Max y is said to be scrolly this property. There are scroller this class. The Computscroll method is indispensable. public void computescroll () { if (mscroller. Computescrolloffset ()) { int x = mscroller. Getcurrx (); int y = mscroller. Getcurry (); scrollTo (x, y); postinvalidate ();         }  }

My github address: https://github.com/flyme2012 My blog address: http://www.cnblogs.com/flyme2012/



From for notes (Wiz)

The Velocitytracker of Android learning

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.