Android custom control series tutorial ----- transfer of touch events, android ----- touch

Source: Internet
Author: User

Android custom control series tutorial ----- transfer of touch events, android ----- touch
Cutting-edge: I haven't written a blog for a long time. I have very little time to write something for work reasons. Recently I want to write a blog on the UI series, because I found that there are very few such blogs, I want to explain how to customize the android control series from my own point of view. Custom Controls: In my understanding, custom controls need to understand the touch event transfer, distribution, and interception mechanisms, the use of Scroller classes, and the andorid view, familiar with ViewGroup, because most of our controls are inherited from ViewGroup, and we also need to learn layout measurement. First of all, we need to know that the Touch event will take place in the android system in a few places. This is a common problem, but I still want to write this question, for the sake of warmth, we first need to know that the VIew class, which cannot be a container class, only has these two functions:

@Overridepublic boolean dispatchTouchEvent(MotionEvent event) {// TODO Auto-generated method stubreturn super.dispatchTouchEvent(event);}@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubreturn super.onTouchEvent(event);}
The classes that can be used as containers, such as ViewGroup and the classes that inherit it, have these functions:
@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {// TODO Auto-generated method stubreturn super.onInterceptTouchEvent(ev);}@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubreturn super.onTouchEvent(event);}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {// TODO Auto-generated method stubreturn super.dispatchTouchEvent(ev);}
Now we will rewrite these functions to go through the process separately, so that we can see more clearly how the android touch event is transmitted. Let's rewrite these two methods. Override A TextView
public class MyTextView extends TextView{private final String TAG = MyTextView.class.getSimpleName();public MyTextView(Context context) {super(context);}@Overridepublic boolean dispatchTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_DOWN");break;case MotionEvent.ACTION_POINTER_DOWN:Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_POINTER_DOWN");break;case MotionEvent.ACTION_POINTER_UP:Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_POINTER_UP");break;case MotionEvent.ACTION_MOVE:Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_MOVE");break;case MotionEvent.ACTION_UP:Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_UP");break;}return super.dispatchTouchEvent(event);}@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:Log.d(TAG, TAG + "onTouchEvent+ACTION_DOWN");break;case MotionEvent.ACTION_POINTER_DOWN:Log.d(TAG, TAG + "onTouchEvent+ACTION_POINTER_DOWN");break;case MotionEvent.ACTION_POINTER_UP:Log.d(TAG, TAG + "onTouchEvent+ACTION_POINTER_UP");break;case MotionEvent.ACTION_MOVE:Log.d(TAG, TAG + "onTouchEvent+ACTION_MOVE");break;case MotionEvent.ACTION_UP:Log.d(TAG, TAG + "onTouchEvent+ACTION_UP");break;}return super.onTouchEvent(event);}}
Then we will rewrite a LinearLayout.
public class MyLinearLayout extends LinearLayout {private final String TAG = MyLinearLayout.class.getSimpleName();public MyLinearLayout(Context context) {super(context);}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {switch (ev.getAction()) {case MotionEvent.ACTION_DOWN:Log.d(TAG, TAG + "onInterceptTouchEvent+ACTION_DOWN");break;case MotionEvent.ACTION_POINTER_DOWN:Log.d(TAG, TAG + "onInterceptTouchEvent+ACTION_POINTER_DOWN");break;case MotionEvent.ACTION_POINTER_UP:Log.d(TAG, TAG + "onInterceptTouchEvent+ACTION_POINTER_UP");break;case MotionEvent.ACTION_MOVE:Log.d(TAG, TAG + "onInterceptTouchEvent+ACTION_MOVE");break;case MotionEvent.ACTION_UP:Log.d(TAG, TAG + "onInterceptTouchEvent+ACTION_UP");break;}return super.onInterceptTouchEvent(ev);}@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:Log.d(TAG, TAG + "onTouchEvent+ACTION_DOWN");break;case MotionEvent.ACTION_POINTER_DOWN:Log.d(TAG, TAG + "onTouchEvent+ACTION_POINTER_DOWN");break;case MotionEvent.ACTION_POINTER_UP:Log.d(TAG, TAG + "onTouchEvent+ACTION_POINTER_UP");break;case MotionEvent.ACTION_MOVE:Log.d(TAG, TAG + "onTouchEvent+ACTION_MOVE");break;case MotionEvent.ACTION_UP:Log.d(TAG, TAG + "onTouchEvent+ACTION_UP");break;}return super.onTouchEvent(event);}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {switch (ev.getAction()) {case MotionEvent.ACTION_DOWN:Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_DOWN");break;case MotionEvent.ACTION_POINTER_DOWN:Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_POINTER_DOWN");break;case MotionEvent.ACTION_POINTER_UP:Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_POINTER_UP");break;case MotionEvent.ACTION_MOVE:Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_MOVE");break;case MotionEvent.ACTION_UP:Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_UP");break;}return super.dispatchTouchEvent(ev);}}


public class TestActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);MyLinearLayout layout = new MyLinearLayout(this);MyTextView myTextView = new MyTextView(this);myTextView.setText("touch event");layout.addView(myTextView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));setContentView(layout);}}
Let's take a look at the log. Then we can change TextView to the Button. Then we can look at the log above the log analysis. We can see that the touch event first goes to the father's dispatchTouchEvent method, the onInterceptTouchEvent is followed by the dispatchTouchEvent and TouchEent of MyTextView. So we know one thing: All touch events must be first passed to the father and then to the child, and then the dispatchTouchEvent method is first executed in the order of analyzing the function execution, after onInterceptTouchEvent is executed, the onTouchEvent event is finally executed. At the same time, we should also note that the return values of these functions are boolean variables. Then, return true for the return value of the dispatchTouchEvent method of MyLinearLayout, and run it.

You will see that all events are always in MyLinearLayout and are not transmitted to the children. Similarly, we will return false. Try again.

At this time, we can only see the down event. What does this mean? When the dispatchTouchEvent returns true, it will continue to pass the event to its own control dispatchTouchEvent for processing. Like MylinearLayout, it can still be executed, DOWN and UP events, when false is returned, even the control itself cannot receive the event. Similarly, we restore dispatchTouchEvent, and then return true in onInterceptTouchEvent;

We can see that the event is only distributed to its own control and enters its own OntouchEvent. Then we are changing the onInterceptTouchEvent return value to false to see the result.

We can see that our events do not receive any impact, and all operations are executed as usual. We also restore the onInterceptTouchEvent in MylinearLayout. Now we have noticed that we want to modify the onTouchEvent return value in MyTextView, modify it to true.

We can see that there is no change. We are trying to change it to false.

As you can see, only the DOWN event is received in MytextView and no other events are received. Conclusion: The androidtouch event transmits the parent control of the sequence to the Child control by default, the Calling sequence of the function is roughly the dispatchTouchEvent ----> message distribution ---> onInterceptTouchEvent ---> event interrupt --> onTouchEvent --> event processing. I will briefly summarize the previous experiment and conclusion about the returned value: When the returned dispatchTouchEvent is true, the event will not be passed below, always follow the dispatchTouchEvent method. When the returned value is false, only one Down event of the dispatchTouchEvent method will be taken: when onInterceptTouchEvent returns true, it intercepts the event and allows him to use his onTouchEvent method. When he returns false, it does not intercept normally. When the return value of onTouchEvent is true, the event is distributed to its own touchEvent. If it is false, only the first down event is distributed, there are also combinations of various return values. I will not give an example one by one. If you want to write a custom UI and Control touch event distribution, you must learn it well. The rest will be learned by yourself.

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.