TouchDelegate and touchdelegate of android view

Source: Internet
Author: User

TouchDelegate and touchdelegate of android view
To be honest, I did not know that TouchDelegate was available until I recently checked the view source code.
There is a private TouchDelegate variable in the view:
Private TouchDelegate mTouchDelegate = null;
In the view, the public method setTouchDelegate can set a TouchDelegate object for this view. The source code is as follows:

    public void setTouchDelegate(TouchDelegate delegate) {        mTouchDelegate = delegate;    }

Here, you may ask: What Can mTouchDelegate in the view do? To answer this question, we will explain it in three steps.


Step 1:First, let's take a look at the original description of the TouchDelegate class. The TouchDelegate. java file has the following description:

Helper class to handle situations where you want a view to have a larger touch area than its
Actual view bounds. The view whose touch area is changed is called the delegate view. This
Class shocould be used by an ancestor of the delegate.
TouchDelegate is a tool class, which aims to give a view a more touchable area in a specific position than the actual touch area. the changed view in the touch area is called "delegate view ". this tool class should be used by the parent view of "delegate view.
Through the above meaning, we can understand that the main purpose of TouchDelegate is to expand the touch area of a view.


Step 2: To have a deep understanding, I think we need to check the source code to see how it is implemented.

Check TouchDelegate. As you can see, TouchDelegate is a very simple class with four variables and two methods:
/*** View that shoshould receive forwarded touch events */private View mDelegateView; // you need to expand the view/*** Bounds in local coordinates of the containing view that shocould be mapped to the delegate * view. this rect is used for initial hit testing. */private Rect mBounds; // defines the expanded targeting area/*** mBounds inflated to include some slop. this rect is to track whether the motion events * shocould be considered To be within the delegate view. */private Rect mSlopBounds; // This is a touch area that overflows with mBounds: in fact, it is a greater area than mBounds (8 larger in width and height respectively), and the touch error is eliminated for the purpose. public TouchDelegate (Rect bounds, View delegateView) {// constructor: bounds indicates the touch area; delegateView needs to expand the view mBounds = bounds; mSlop = ViewConfiguration. get (delegateView. getContext ()). getScaledTouchSlop (); // The result here is the touch sliding distance judgment: that is, when the touch sliding distance is mSlop, the referee is moving by touch. the default value is 8 mSlopBounds = new R. Ect (bounds); mSlopBounds. inset (-mSlop,-mSlop); // here the sliding area of the touch is enlarged (8 in width and height), and the error during the touch is eliminated when the target is expanded. to achieve a touch safe processing. mDelegateView = delegateView; // view of the touch area to be expanded (view of the touch area to be changed)} public boolean onTouchEvent (MotionEvent event) {// This is the core code: determines whether the current touch is in this area: mBounds. if yes, the touch event will be placed in the real area of the mDelegateView. int x = (int) event. getX (); int y = (int) event. getY (); // record the position of this touch point boolean sendToDelegate = false; // mark whether the touch is valid (it should be passed to mDelegateView) Boolean hit = true; // indicates whether the touch is in this region (mBounds) boolean handled = false; switch (event. getAction () {case MotionEvent. ACTION_DOWN: Rect bounds = mBounds; if (bounds. contains (x, y) {// ACTION_DOWN whether mDelegateTargeted = true in this region; // mark this touch event in this region (in mDelegateView) sendToDelegate = true ;} break; case MotionEvent. ACTION_UP: case MotionEvent. ACTION_MOVE: sendToDelegate = mDelegateTargeted; if (sendTo Delegate) {Rect slopBounds = mSlopBounds; if (! SlopBounds. contains (x, y) {hit = false ;}} break; case MotionEvent. ACTION_CANCEL: sendToDelegate = enabled; Enabled = false; break;} if (sendToDelegate) {// valid for this touch: The touch event is passed to mDelegateView final View delegateView = mDelegateView; // simulate this touch in the mDelegateView region: Calculate the touch point of the event to ensure that the touch point of the event is in the real region of mDelegateView. if (hit) {// Offset event coordinates to be inside the target view event. setLocation (delegateView. getWidth ()/2, delegateView. getHeight ()/2);} else {// Offset event coordinates to be outside the target view (in case it does // something like tracking pressed state) int slop = mSlop; event. setLocation (-(slop * 2),-(slop * 2);} handled = delegateView. dispatchTouchEvent (event); // transmits the calculated touch event to the dispatchTouchEvent of the mDelegateView so that it can touch the event accordingly .} return handled ;}

Step 3:Finally, let's see when mTouchDelegate is used in the view.
As mentioned above, a variable mTouchDelegate is defined in the view to save the TouchDelegate object of the current view. The purpose is to ensure that the view has the following functions: in fact, the touch events in their own region correspond to the Views in other places (other regions.
How is view implemented? You can check the source code. In the onTouchEvent method of the view, you will first determine whether you have available TouchDelegate objects. If so, the onTouchEvent method will first execute the mTouchDelegate
OnTouchEvent method. The following 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 () +", isLongClickable = "+ isLongClickable () +", this = "+ this);} if (viewFlags & ENABLED_MASK) = DISABLED) {// M: we need to res Et 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 & PFLAG_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 that is clickable still consumes the touch // events, it just doesn't respond to them. return (viewFlags & CLICKABLE) = CLICKABLE | (viewFlags & LONG_CLICKABLE) = LONG_CLICKABLE);} if (mTo UchDelegate! = Null) {// determine whether the TouchDelegate object if (mTouchDelegate. onTouchEvent (event) {execute onTouchEvent return true; // if this touch event is in the region set by TouchDelegate, this return. other tasks will not be executed }}.....


Finally, let's take a look at an instance. Below is a method for a parent view to exaggerate its own view touch area:

    public static void enlargeBtnTouchScope(View parent, Button btn, Rect rect, float width, float height){rect.top = btn.getTop();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 code above allows the button in parent_view to be better touched (clicked ).
 

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.