Android View Touchdelegate

Source: Internet
Author: User

to be honest, I didn't know there was a touchdelegate until recently, when I checked the source of the view, we found the new continent.
There is a private touchdelegate variable in view:
Private touchdelegate mtouchdelegate = null;
The public method in view Settouchdelegate can set a Touchdelegate object for this view, the source code is as follows:
    public void Settouchdelegate (Touchdelegate delegate) {        mtouchdelegate = delegate;    }

When you get here, you may ask: what exactly can mtouchdelegate in the view do? To answer this question, here are three steps to explain.


First step: first Look at the Touchdelegate class of a paragraph of the original description, in the document Touchdelegate.java has the following description:

Helper class to handle situations where is want a view to has a larger touch area than its
Actual view bounds. The view whose touch area was changed is called the delegate view. This
Class should is used by an ancestor of the delegate.
The idea is that touchdelegate is a tool class that is designed to have a view in a specific location that has a greater touch area than the actual touch area. The view that the touch area was changed is called "Delegate View". This tool class should be " Delegate View "is used by the parent view.
By means of the above, we can understand that the main purpose of touchdelegate is to enlarge the touch area of a view.


The second step : To understand deeply, I think need to look at the source code to see how he achieved.

See touchdelegate you know, Touchdelegate is a very simple class with four variables and two methods:
 /** * View that should receive forwarded touch events * * Private view mdelegateview;//need to expand the touch area of view/** * Bounds in local coordinates of the containing view that should is mapped to the delegate * view.     This rect was used for initial the hit testing. */Private Rect mbounds;//defines this expanded striking area/** * Mbounds inflated to include some slop.     This rect was to track whether the motion events * should was considered to being be was within the delegate view.        */Private Rect mslopbounds;//This is a touch area relative to the mbounds overflow: In fact, the area larger than the mbounds (width and height respectively 8), its purpose is to eliminate the touch error. Public touchdelegate (Rect bounds, view delegateview) {//constructor: bounds represents the area of the touch; Delegateview need to expand the touch area of View mbounds = Bo        Unds; Mslop = Viewconfiguration.get (Delegateview.getcontext ()). Getscaledtouchslop ();//Here is the judgment of the touch sliding distance:        is to touch the sliding distance for Mslop when the referee is in touch sliding move. Its default value is 8 mslopbounds = new Rect (bounds);     Mslopbounds.inset (-mslop,-mslop);//This expands the touch-sliding area (width-height expansion by 8), and its purpose is to eliminate the error when touching. Achieve a touch-safe handling.   Mdelegateview = Delegateview; Need to enlarge the touch area of view (need to change the touch area of view)} public boolean ontouchevent (Motionevent event) {//This is the core code: Determine whether the current touch is in this area: Mbounds.        If so, then the touch event is really in the Mdelegateview real area.        int x = (int) event.getx (); int y = (int) event.gety ();//record This touch point position boolean sendtodelegate = false;//Mark whether this touch is valid (should be passed to Mdelegateview) Boolea N hit = true;        Mark whether this touch is within this area (mbounds) Boolean handled = FALSE;            Switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:Rect bounds = mbounds; if (Bounds.contains (x, y)) {//action_down is within this region mdelegatetargeted = true;//Tag This time the touch event is in this area (within Mdelegateview            ) Sendtodelegate = true;        } break;            Case MotionEvent.ACTION_UP:case MotionEvent.ACTION_MOVE:sendToDelegate = mdelegatetargeted;                if (sendtodelegate) {Rect slopbounds = mslopbounds;               if (!slopbounds.contains (x, y)) {     hit = false;        }} break;            Case MotionEvent.ACTION_CANCEL:sendToDelegate = mdelegatetargeted;            mdelegatetargeted = false;        Break            if (sendtodelegate) {//This touch is valid: The touch event is passed to Mdelegateview final View delegateview = Mdelegateview;                Simulating this touch is really within the Mdelegateview area: The touch point of the new calculation event to ensure that the touch point of this event is within the Mdelegateview real area if (hit) { Offset event coordinates to be inside the target view event.setlocation (Delegateview.getwidth ()/2, Del            Egateview.getheight ()/2); } else {//Offset event coordinates to being outside the target view (in case it does//Somet                Hing like tracking pressed state) int slop = Mslop;            Event.setlocation (-(SLOP * 2),-(SLOP * 2));    } handled = Delegateview.dispatchtouchevent (event);//pass the computed touch event to Mdelegateview's dispatchtouchevent to make it touch the event accordingly.    } return handled; }

Step Three:Finally, let's see when the view is using Mtouchdelegate.
It says that a variable mtouchdelegate is defined in the view to hold the current view's Touchdelegate object, which is designed to ensure that the view has the function that the touch event in its own area actually corresponds to something else (elsewhere) 's view.
How is view implemented? See the source code can know, in the view of the Ontouchevent method, the first will be to determine whether there is a usable touchdelegate object, if there is, then Ontouchevent method inside will first go to perform mtouchdelegate
Ontouchevent method. Here is the source code:
   public boolean ontouchevent (Motionevent event) {final int viewflags = mviewflags;                    if (dbg_motion) {xlog.d (View_log_tag, "(VIEW) ontouchevent 1:event =" + Event + ", Mtouchdelegate =" + Mtouchdelegate + ", enable =" + isenabled () + ", clickable =" + isclickable () + ", ISLONGCL        ickable = "+ islongclickable () +", this = "+ this); } if ((ViewFlags & enabled_mask) = = DISABLED) {//M:we need to reset the pressed state or remove            Prepressed callback either up or cancel event happens.            Final int action = Event.getaction (); if (action = = MOTIONEVENT.ACTION_UP | | action = motionevent.action_cancel) {if (Mprivateflags & PFLA                g_pressed)! = 0) {setpressed (false); } else if ((Mprivateflags & pflag_prepressed)! = 0) {xlog.d (View_log_tag, "VIEW OnTouch event, if View is DISABLED & pflag_prepressed, REmove Callback mprivateflags = "+ Mprivateflags +", this = "+ this);                Removetapcallback (); }}//A disabled view is clickable still consumes the touch/events, it just does            N ' t respond to them.                    Return (((viewflags & clickable) = = Clickable | |        (ViewFlags & long_clickable) = = long_clickable)); if (mtouchdelegate! = null) {//Determine if there is a Touchdelegate object if (Mtouchdelegate.ontouchevent (event)) {Execute Ontouch        Event return true;//If this touch event is within the area set by Touchdelegate, this returns. No other}} ...


Finally, take a look at an example. Here's how a parent view exaggerates its view touch area:

    public static void Enlargebtntouchscope (View parent, Button btn, rect rect, float width, float height) {rect.top = Btn.gett OP (); rect.bottom = Btn.getbottom (); rect.left = Btn.getleft (); rect.right = Btn.getright (); rect.top-= height; Rect.bottom + = height;rect.left-= width;rect.right + width;parent.settouchdelegate (new Touchdelegate (rect, BTN));    }
The above code allows the button in the Parent_view to be better touched (clicked).

Android View Touchdelegate

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.