1. Introduction
Earlier I posted an article about dialog box animation the only one on the web that pops up dialog box styles. The amount of users reading, sharing, reading after the amount of attention and other data is better. With the previous emphasis on Android source code analysis of the article is different, it is a more practical article. On the downside, the code has very few comments, and some readers have reported a poor reading experience. If this problem has troubled you, I would like to take this opportunity to sincerely apologize to you for not investing more energy in the article due to haste. I will invest more time in the article polishing in the future, and strive to bring readers a better reading experience.
If you think my article is good, please follow the wechat official account of bytecode. More articles are waiting for you.
2. About the TouchDelegate
This article will cover not only the simple use of TouchDelegate, but also how it works, its limitations, and solutions.
At work, this can happen
Designer: “ah, you this button click is too insensitive, can you click the area a little bigger?”
Developer: “My button is as big as your design draft. Why don’t you cut the picture bigger?”
If you modify the size of the button, you can expand the click area of the button. Can you expand the click area of the button without modifying the size of the button? Sure, you can do that with a TouchDelegate.
2.1 Core methods
- TouchDelegate(Rect bounds, View delegateView)
View delegateView: The control that needs to enlarge the click range.
Rect Bounds: The area where the delegateView responds to the event, generally larger than its original range.
- View#setTouchDelegate(TouchDelegate delegate)
TouchDelegate is a member variable of View.java. The View object is typically the parent control of the delegateView (not just the parent, can be the parent control)
2.2 scenario
Scenario 1: The parent control area is large enough to expand the click range of a single Button
Enlarge Button click area by 500 pixels as shown below
The following code
Set this up so that when clicked in the green area, the Button also responds to the click event.
Scenario 2: The parent control area is small, expand the click range of a single Button
The pink ViewGroup area is just a little bigger than the Button. Expand the Button click area by 500 pixels.
If the call ViewGroup. SetTouchDelegate () have no effect, you need to call Root. SetTouchDelegate ().
If a ViewGroup wants to expand the click area of its descendant View, it must ensure that the ViewGroup has enough space; otherwise, it needs to find a parent control with enough space to expand the click area of its descendant View
Scenario 3: Multiple buttons want to expand the click area
As shown below, expand the click area of Button1 and Button2 by 500 pixels
You might say, isn’t that easy? I wrote the following code
Unfortunately, only button2’s click area has been expanded by 500 pixels, but Button1’s has not. This is the default TouchDelegate limitation of the system, which can only expand the click area for a descendant View. I’ll show you the solution later
Scene 4: The Button that expands the click area is filled with other views
The pseudocode is as follows, assuming that the left, top, and other views are exactly 500 pixels wide and high
When the user clicks on the left, top and other views, Button1 cannot get the click event even if the click of Button1 has been enlarged by 500px. Because it’s the TouchDelegate’s turn to distribute events only if all of the children of the ViewGroup don’t handle them. Please refer to the event distribution mechanism for detailsIn-depth traversal of the Android event distribution mechanism
3. Principle analysis
3.1 Core methods
- View.onTouchEvent(MotionEvent event)
- If the TouchDelegate is set, its onTouchEvent is called, and if true is returned, the call ends, otherwise it goes down
- Handle OnClickListener, OnLongClickListener and other events
- TouchDelegate.onTouchEvent(MotionEvent event)
- The Down event determines whether the finger falls within the event expansion range
- Non-down event, determines if the finger is out of slopBounds, which extends the bounds area by a certain amount, and if so, sends a negative event coordinate to the delegateView
- If an event is distributed, set the event coordinate to the center of the delegateView
- If the phone is out of the maximum range, set the event coordinates to negative distribution
4. The DEMO code
Demo has been uploaded to github.com/lizijin/zij…
Operation screenshot is as follows
5. Push the boundaries
Scenario 3: Multiple buttons want to expand the click area at the same time. We said that the TouchDelegate can’t expand the click area for multiple views at the same time.
Suppose you have the following requirements and want to expand the add and subtract click area at the same time. How do you do that?
The net effect is as follows. As shown, clicking around add or subtract also triggers an event.
Core code, rewrite TouchDelegate.
The full code has been uploaded to github.com/lizijin/zij…
6. Summary
- Blind spot 1: Set the TouchDelegate, which must be on the ancestor of the View that needs to expand the click area.
- Blind spot 2: If the ancestor View space is smaller than the area required by the child View, the click area cannot be properly expanded.
- Blind spot 3: Enlarging the click area may not work if there are other View consumption events around the View.
- Blind spot 4: The system default, can not expand the click area for multiple views.
Finally: code words are not easy. Welcome to the wechat official account of Bytecode. More articles are waiting for you.
read
- One of the four steps for event distribution: In-depth traversal Explains the Android event distribution mechanism
- Event distribution tetralogy 2: Nested sliding event analysis
- Event distribution: CoordinatorLayout event analysis
- Event distribution four tetralogy: understand BottomSheetBehavior