Chapter III: The Event System of view

Source: Internet
Author: User

The basics of 3.1 view

      Main: View positional parameters, Motionevent and Touchslop objects, Velocitytracker,gesturedetector and Scroller objects

    3.1.1 View

      View is the base class for all controls in Android and is an abstraction of an interface layer of controls.

ViewGroup (control group), which contains a number of controls, a set of view

    3.1.2 View's positional parameters

      The location of the view is mainly determined by its four vertices: top (upper-left ordinate), left (upper horizontal axis), right (bottom corner horizontal), bottom (lower right corner ordinate)

left = GetLeft ();

right = GetRight ();

top = GetTop ();

Bottom = Getbottom ();

Starting with android3.0, added: X,y,translationx and translationy,x and Y are the left of the upper left corner of the view, while Translationx and Translationy are the offsets of the upper left corner of the view relative to the parent container.

x = left + Translationx;

y = right + Translationy;

    3.1.3 Motionevent and Touchslop

      1. Motionevent

The finger touches the screen and produces some column events, typical events are as follows

          • Action_down: Finger just touching the screen
          • Action_move: Finger moves on the screen
          • ACTION_UP: The moment the finger is released from the screen

2. Touchslop

is the minimum distance that the system can identify as sliding. If the distance between the two slides is smaller than the constant, the system doesn't think you're doing a slide operation.

This constant is obtained by Viewconfiguration.get (GetContext ()). Getscaledtouchslop ().

   3.1.4 Velocitytracker,gesturedetector and Scroller

      1.VelocityTracker

Speed tracking: Used to track the speed of your finger during the sliding process

First, track the speed of the current click event in the view's Ontouchevent method

Velocitytracker Velocitytracker = Velocitytracker.obtain ();

Velocitytracker.addmovement (event);

Note Two: First, the speed must be computed before the speed is obtained, that is, before the getxvelocity () and Getyvelocity () methods, the Computecurrentvelocity method is called first, the 2nd, The speed here refers to the number of pixels that the fingers have been passing over time.

Velocitytracker.computecurrentvelocity (1000);//time interval is 1000ms;

int xvelocity = (int) velocitytracker.getxvelocity ();

int yvelocity = (int) velocitytracker.getyvelocity ();

      2. Gesturedetector

      Gesture Detection

First you need to create a Gesturedetector object and implement the Ongesturelistener interface

Gesturedetector mgesturedetector = new Gesturedetector (this);

Resolve a phenomenon that cannot be dragged after long pressing the screen

Mgesturedetector.setislongpressenabled (FALSE);

Then, the Ontouchevent method of taking over the target view is implemented in the Ontouchevent method of the view to be monitored.

Boolean consume = Mgesturedetector.ontouchevent (event);

Retrun consume;

     3. Scroller

The elastic sliding object is used to realize the elastic sliding of the view, and the process is instantaneous when using the Scrollto/scrollby method to slide.

3.2 View Slide

      3.2.1 Using Scrollto/scrollby

       Scrollby actually called the Scrollto method, Scrollby it to implement relative sliding based on the current position, Scrollto is based on the absolute sliding of the parameters passed

The two attributes within the view mscrollx (equal to the horizontal distance of the left edge of the view and the view content) and the mscrolly (equal to the distance between the top edge of the view and the top edge of the view content in the vertical direction), Obtained through GETSCROLLX and getscrolly

Swipe from left to right, mscrollx negative, inverse positive, and if swipe from top down, the mscrolly is negative and the inverse is positive.

      3.2.2 Using animations

        Use the view animation, but will cause the view position has not changed, after the animation completes, the view still will stop in the original position, therefore must use the property animation

       3.2.3 Changing layout parameters

       Layout parameters: Layoutparams

        

Marginlayoutparams params =+ =+ + +; mbutton1.requestlayout (); // or Mbutton1.setlayoutparams (params);

        

    3.3 Elastic Slide

     3.3.1 Using Scroller

       

Scroller Scroller =NewScroller (mcontext);//Scroll slowly to the specified positionPrivate voidSmoothscrollto (intDESTX,intdesty) {    intSCROLLX =Getscrollx (); intDeltaX = Destx-Scrollx; //1000ms sliding destx, the effect is to slowly slideMscroller.startscroller (scrollx, 0, deltax, 0, 1000); Invalidate ();} @Override Public voidComputescroller () {if(Mscroller.computescrollofset ()) {Mscrollto (MSCROLLER.GETSCROLLX, Mscroller.getcurry ());    Postinvalidate (); }}

      

Scroller itself does not realize the sliding of the view, he needs to cooperate with the view's Computescroll method to complete the effect of the elastic slide. It keeps the view redrawn, and each redraw starts with a time interval from the sliding start time. The current sliding position of the view can be obtained by this time interval, and the sliding position can be realized by the Scrollto method.

  3.3.2 By animation

    Animated Objectanimator.offloat (TargetView, "Translationx", 0, +). Setduration (+). Start ();

   3.3.3 using the delay policy

     Use handler to send a message SendMessage () to draw the view in the Handlemessage () method   

     

   Event distribution mechanism for 3.4 view

     3.4.1 Pass rule for click events

There are three ways of doing this:

public boolean dispatchtouchevent (motionevent ev): Used for the distribution of events. If the event can be passed to the current view, the second method must be called. The results are affected by the Dispatchtouchevent method of the ontouchevent and downlevel view of the current view, indicating whether the current event is consumed.

public boolean onintercepttouchevent (Motionevent event): Used to determine whether to intercept an event, if the current view intercepts an event, then in the same sequence of events, this method will not be called again, Returns the result indicating whether to intercept the current event

public boolean ontouchevent (Motionevent event): Called in the Dispatchtouchevent method to handle the Click event, the return result indicates whether the current event is consumed, or, if not consumed, in the same sequence of events, The current view cannot receive the event again.

      

Once a click event is generated, its delivery process follows the following sequence: Activity->window->view.

          1. uniform sequence of events is the one that touches the screen from your finger At the end of the moment when the finger leaves the screen (starting with the down event, the middle contains a variable number of move events and ends with the up event).
          2. Normally, a time series can only be intercepted and consumed by one view.
          3. when a view decides to intercept, the sequence of events can only be handled by it, and its onintercepttouchevent will not be called again.
          4. Once a view starts processing an event, if he does not consume the Action_down event (Ontouchevent returns False), then no other time in the same sequence of events will be given to it for invocation.
          5. If the view does not consume any time other than Action_down, then this click event will be the hour when the ontouchevent of the parent element is not called and the current view can continue to be subject to subsequent time. Eventually, these vanishing click events are passed to activity processing.
          6. ViewGroup does not intercept any time by default
          7. view has no Onintercepttouchevent method, and once a click event is passed to it, its Ontouchevent method is called
          8. The ontouchevent of
          9. view consumes the event (returns True) by default, unless it is not clickable (clickable and clickable are also false).
          10. The Enable property of the
          11. view does not affect the default return value of Ontouchevent.
          12. The
          13. onclick occurs if the current view is clickable, and it receives the down and up events
          14. The
          15. event delivery process is internal, that is, the event is always passed to the parent element first, and then distributed by the parent element to the child view, through the Requestdisallowintercepttouchevent method can be dared to the child element of the parent element of the event distribution process, However, the Action_down event is excluded.

      3.4.2 Event Distribution

        1.Activity distribution process for click events

          Click events with Motionevent, the event is first passed to the current activity, the activity of the dispatchtouchevent to carry out the event, the specific work of the activity within the window to complete.

window will pass the event to decor view, decor view is generally the underlying container of the current interface, available through Activity.getWindow.getDecorView ().

Window is an abstract class in which the implementation class is phonewindow,phonewindow to pass time directly to Decorview

By ((ViewGroup) GetWindow (). Getdecorview (). Findviewbyid (Android. r.id.content). Getchildat (0) This way gets the view set by the activity

And the view we set through Setcontentview is a sub-view of it.

       2. Top view-to-click event distribution Process

When the Click event reaches the top view (typically a viewgroup), the ViewGroup's Dispatchtouchevent method is called

If the top-level ViewGroup intercepts an event, that is, Onintercepttouchevent returns True, the event is handled by ViewGroup, and if ViewGroup's Montouchlistener is set, The ontouchevent will be called, otherwise the ontouchevent will be called.

Ontouch will shield off Ontouchevent,

In Ontouchevent, if Monclicklistener is set, the onclick is called.

If the top view does not intercept, the event is passed to the child view on the chain of click events on which it is located.

        3. View-to-click event handling Process

           First it will be determined if there is no setting ontouchlistener, if the Ontouch method in Ontouchlistener returns True, then Ontouchevent will not be called, Ontouchlistener priority is higher than ontouchevent

As long as the view's clickable and long_clickable have a true, then he consumes the event, that is, the Ontouchevent method returns True

      

      3.5 View's sliding conflict

1. External sliding direction inconsistent with internal sliding direction

2. The external sliding direction is consistent with the internal sliding direction

3. Nesting of the above two cases

      3.5.2 rules for handling sliding conflicts

Determine who will intercept the event by sliding horizontally or vertically according to the slide

Find specific rules based on your specific business

     The solution of 3.5.3 sliding conflict

          1. External interception method

Click events are first intercepted by the parent container, and if the parent container needs this event to be intercepted, do not intercept if this event is not required

External interception needs to rewrite the parent container's Onintercepttouchevent method, in-house to do the appropriate interception can

Need to intercept, intercepted returns true, does not intercept return false

         2. Internal interception method

Refers to the parent container does not intercept any events, all the events are passed to the child element, if the child element needs this event is consumed directly, otherwise the parent container to handle, need to cooperate with the Requestdisallowinterpttouchevent method

You need to override the Dispatchtouchevent method of the child element.

            

Chapter III: The Event System of view

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.