1.What are the main coordinate parameters of View? What are the key points to pay attention to? Answer: Left, Right, top, Bottom. Note that these 4 values are actually the relative coordinates of the view and its parent control. They are not the absolute values from the upper left corner of the screen. Please note this. In addition, X and Y are actually coordinate values relative to the parent control. The default values of TranslationX and TranslationY are both 0, which are the offsets relative to the upper left corner of the parent control. Conversion relationship: x=left+tranX,y=top+tranY. Many people don’t understand why this is the case. In fact, if the View moves, such as translation, you should pay attention that the values of top and left will not change. No matter how you drag the view, the values of x, y, tranX, and tranY will change as you drag and move. Just understand this. 2. When is it better to use onTouchEvent or GestureDetector? Answer: Use the former when there is only a sliding requirement, and use the latter when there is a double-click or other behavior. 3.What problem does Scroller solve? Answer: The scrollTo and scrollBy of view have very poor sliding effects, which are completed instantly. However, the scroller can cooperate with the computeScroll of view to complete the gradual sliding effect. The experience is better. 4.What should I pay attention to when using ScrollTo and ScrollBy? A: The former is absolute sliding, and the latter is relative sliding. What slides is the content of the view rather than the view itself. This is very important. For example, when a textview calls these two methods, what slides is the content of the displayed words. Generally speaking, we use scrollBy more often. When passing values, just remember a few rules: right-left x is positive, otherwise x is negative; up-down y is negative, otherwise y is positive. You can take a look at the source code of these two:
You can see there are two variables mScrollX and mScrollY. The values of these two units are pixels. The former represents the distance between the left edge of the view and the left edge of the view content. The latter represents the distance between the top edge of the view and the top edge of the view content. 5. What are the consequences of using animation to implement view sliding? A: Actually, view animation is to move the surface UI of the view, that is, the visual effect presented to the user. The animation itself cannot move the actual position of the view. Except for attribute animation. After the animation is finished, the view will eventually return to its original position. Of course, you can set the fillafter property to make the view appearance stay at the changed position after the animation is finished. So this will bring a very serious consequence. For example, your button is on the left side of the screen. Now you use an animation and set the fillafter property to move it to the right side. You will find that clicking the right button does not trigger a click event, but clicking the left button can trigger it. The reason is that the right button is only the appearance of the view, and the real button is still on the left and has not moved. If you must do this, you can put a new button in advance at the position after the right button moves. When the animation is finished, enable the right button and let the left button go. This can avoid the above problems. 6. How many ways are there to make a view slide? What should you pay attention to? What scenarios are they applicable to? A: There are three types: a: scrollto, scrollby. This is the simplest, but you can only slide the content of the view but not the view itself. b: Animation. Animation can slide the view content, but please note that non-attribute animation will affect the interaction as mentioned in question 5, so you should pay more attention when using it. However, most complex sliding effects are completed by attribute animation, which is a killer level. c: Change layout parameters. This method is easy to understand. It is nothing more than dynamically modifying view parameters such as margin through Java code. However, it is rarely used. I personally don't use this method very often. 7.What does Scroller do? How does it work? A: Scroller is used to make the view have a sliding gradient effect. The usage is as follows:
In fact, many people should be able to search for the above code. Here we mainly talk about its principle.
8.How many methods are there for view sliding gradient effect? A: Three. The first one is the scroller, which is also the most used one. There is an explanation in question 7. Another one is animation. I will not talk much about animation because it is not in the scope of this article. The last one, which we often use, is to use a handler to update the state of the view at a certain time interval. The code is very simple. You can experience it yourself. 9. How to express the event delivery mechanism of view using pseudo code? answer:
10. What is the priority of view's onTouchEvent, OnClickListerner and OnTouchListener's onTouch method? A: onTouchListener has the highest priority. If the onTouch method returns false, onTouchEvent will be called. If it returns true, it will not be called. As for onClick, it has the highest priority. 11. What is the order in which click events are delivered? Answer: Activity-Window-View. Pass from top to bottom. Of course, if the view you are touching returns false onTouchEvent, it means that it does not want to handle it. Then throw it up again. If none of them handle it, In the end, we let the Activity handle it by itself. For example, the PM sends a task to the leader. The leader does not do it himself but gives it to architect A, and A does not do it either but gives it to programmer B. If B does it, then the task is over. If b finds that he can't do it, he will ask a to do it. If a can't do it either, he will keep making requests upwards, and in the end it may still be done by pm.
12.How many steps does the event consist of? Answer: It starts with a down event and ends with an up event, and there may be an indefinite number of move events in between. 13.How does ViewGroup distribute click events? answer:
14. What happens if a view does not consume the down event when processing an event? Answer: If a view's onTouchEvent returns false when a down event occurs, then the event sequence to which the down event belongs will not handle its subsequent move and up operations, and all operations will be handled by its parent view. 15.What happens if the view does not consume the move or up events? Answer: Then the event sequence to which this event belongs will disappear, and the parent view will not handle it. Ultimately, it will be handled by the activity. 16.Does ViewGroup intercept events by default? Answer: By default, no events are intercepted and onInterceptTouchEvent returns false. 17. Once an event is passed to a view, will the view's onTouchEvent be called? Answer: Yes, because the view itself does not have an onInterceptTouchEvent method, so as long as the event comes to the view, it will definitely go through the onTouchEvent method. By default, they are all consumed and return true. Unless the view is not clickable, which means both clickable and longgclikable are false. Button's clickable is true but textview is false. 18. Does enable affect the onTouchEvent return value of the view? A: It has no effect. As long as one of clickable and longClickable is true, onTouchEvent will return true. 19.Can requestDisallowInterceptTouchEvent interfere with the event distribution of the parent element in the child element? If so, can it interfere with all of them? A: Of course you can, but the down event cannot interfere. 20.Will dispatchTouchEvent be called every time? A: Yes, onInterceptTouchEvent will not. 21. What is the idea for solving the sliding conflict problem? A. To solve the sliding conflict, the most important thing is to have a core idea. In an event sequence, which view do you want to respond to your sliding? For example, when sliding from top to bottom, which view handles this event, or from left to right? After you figure it out with business requirements, the rest is actually easy to do. The core methods are two external interceptions, namely the parent interception, and the other is the internal interception, namely the child view interception method. Learning these two methods basically solves all sliding conflicts. They are all variants of these two, and the core code ideas are the same. External interception method: The idea is to rewrite the onInterceptTouchEvent of the parent container. Child elements generally do not need to be managed. It is easy to understand because this is exactly the same as the logic of Android's own event processing mechanism.
Internal interception method: The internal interception method is a little more complicated. When an event arrives, the parent container does not care and lets the child element decide whether to handle it. If it is consumed, it will be killed. If it is not consumed, it will be transferred to the parent container for processing. Sub-element code:
The father container code also needs to be modified, in fact, to ensure that the father does not intercept down:
|
<<: Uncover the technical features of mini program development through programming mode
>>: If you want to implement a refresh control yourself, you only need to master this knowledge
For operations , user volume and revenue are the ...
China's Internet industry has been developing...
How many mini-programs can be created on WeChat? ...
Whenever a foreign head of state or government vi...
Experts from Yiyang Central Hospital remind peopl...
Today we want to share some of the discoveries we...
background As business needs evolve, the complexi...
Intel is not a GPU company, but because its deskt...
Google recently released an online course on Andr...
Socializing has a psychological and emotional imp...
Recently, the domestic prices of Tesla's enti...
With the substantial reduction in terminal subsid...
Nuts are nutritious and delicious. The reason for...
The cartoon "SpongeBob SquarePants" tel...
if The ancestor of chicken is dinosaur We brought...