Android Custom View swipe Event processing summary

Source: Internet
Author: User

The various tool classes required for sliding processing:


Android.view.VelocityTracker

Android.view.OverScroller

Android.view.ViewConfiguration

Velocitytracker class

Mainly used to track the rate of touch screen events (flinging events and other gestures gesture events).

Use the addmovement (motionevent) function to add motion event to the Velocitytracker class instance. You can use Getxvelocity () or getxvelocity () to get the horizontal and vertical rates, but before using them, call computecurrentvelocity (int units) to initialize the units of the rate.

computecurrentvelocity (int units, float maxvelocity)

The Unitis represents the base time unit of the rate. The Unitis value of 1 is how many pixels are moving within a millisecond unit of time, and a Unitis value of 1000 indicates the maximum number of pixels that are moving within a second (1000 milliseconds) unit of time maxvelocity the rate
1 @Override2      Public Booleanontouchevent (Motionevent event) {3         //get the maximum speed value allowed to perform a fling gesture action4         intMmaximumvelocity =viewconfiguration.get (context)5 . getscaledmaximumflingvelocity ();6 7         //instantiation of8Velocitytracker Mvelocitytracker =Velocitytracker.obtain ();9 mvelocitytracker.addmovement (event);Ten         intAction =event.getaction (); One         floaty =event.gety (); A  -         Switch(action) { -          CaseMotionevent.action_down: the  -             return true; -          CaseMotionevent.action_move: -  +              Break; -          CaseMotionevent.action_cancel: +             //Recycling A mvelocitytracker.recycle (); at  -              Break; -          Casemotionevent.action_up: -             //unit of initialization rate -Mvelocitytracker.computecurrentvelocity (1000, mmaximumvelocity); -             intVelocityy = (int) mvelocitytracker.getyvelocity (); in  -             //Recycling to mvelocitytracker.recycle (); +  -              Break; the         } *  $         return Super. Ontouchevent (event);Panax Notoginseng}

Overscroller class

is a helper class to implement view smooth scrolling. Its role is to assist in recording and calculating the distance and velocity of our sliding, which is used to help us to deal with the computational part of mathematics when we customize the movement.

1Overscroller Mscroller =NewOverscroller (context);2 //Scroll, StartX, starty to start scrolling position, dx,dy for scrolling offset, duration for time to complete scrolling3Mscroller.startscroll (intStartX,intStarty,intDxintdy)4 //Use default finish time 250ms5Mscroller.startscroll (intStartX,intStarty,intDxintDyintduration)6 //a return value of Boolean,true indicates that scrolling has not been completed and false indicates that scrolling is complete. 7Mscroller.computescrolloffset ()

Setting the Mscroller scrolling position does not cause the view to scroll, usually using Mscroller to record/calculate the position of the view scroll, and then rewrite the view's Computescroll () to complete the actual scrolling.

Viewconfiguration class

This class mainly defines the standard constants used in the UI, such as timeouts, dimensions, distances, and if we need to get the data for these constants, we can use this class to get

 1  //  2  int  mtouchslop = Viewconfiguration.get (context). Getscaledtouchslop ();  3   Gets the maximum speed value allowed to perform a fling gesture action  4  int  mmaximumvelocity = Viewconfiguration.get (context). Getscaledmaximumflingvelocity ();  5   Gets the minimum speed value that is allowed to perform a fling gesture action  6  int  Mminimumvelocity = Viewconfiguration.get (context). Getscaledminimumflingvelocity (); 

If the layout control and the size of its child controls are precisely controlled, then rewrite the following method.

onmeasure (int widthmeasurespec, int heightmeasurespec)

Android Custom View swipe Event processing summary

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.