Custom view, is a very important knowledge in Android, but also interviewers like to investigate the interview questions;

A lot of people before the interview, may not have worked in Internet companies or work in the past, but is shorter, old don’t know the Internet technology company interview about custom view would ask what issues, plus might not fully ready myself, was to interview a few round the interviewer a few questions received, finally ended in a dismal.

What questions do interviewers usually ask about custom views? Below is a summary of the frequently met questions in Android View

DecorView (DecorView) DecorView (DecorView); (millet)

The DecorView is the top View of the Window interface. It has only one child, LinearLayout. Represents the entire Window interface, including notification bar, title bar, and content display bar. There are two FrameLayout children in the LinearLayout.

DecorView: The DecorView is a top-level View, which is essentially a FrameLayout. It contains two parts, the title bar and the container bar, which are both FrameLayout. The content bar id, which is the part of the activity that sets the setContentView, adds the layout to the FrameLayout with the ID content.

Access to the content:

ViewGroup Content =findViewById (Android.id.content) Get the set View: getChildAt(0).

2. What is the event distribution mechanism of View? (Fast Dog taxi)

The event distribution mechanism of View can be shown in the following figure:

As shown in the figure above, the figure is divided into three layers: Activity, ViewGroup and View from top to bottom.

  • Events start with the white arrow in the upper left corner and are distributed by the Activity’s dispatchTouchEvent

  • The top word of the arrow represents the method return value, (return true, return false, return super.xxxxx(), where super means to call the superclass implementation.

  • There is a “true—-> Consume” in the dispatchTouchEvent and onTouchEvent boxes, which means that if the method returns true, the event will be consumed and will not be sent anywhere else.

  • At present, all graph events are for ACTION_DOWN, and ACTION_MOVE and ACTION_UP are analyzed at last.

  • The Activity in the previous diagram had an incorrect dispatchTouchEvent(the diagram has been fixed). Only return super.DispatchTouchEvent (EV) will go down and return true or false to consume the event (the transmission is terminated).

ViewGroup Event distribution

When a click event occurs, it is passed in the following order:

Activity -> Window -> View

The event is always passed to the Activity, which in turn is passed to the Window, which in turn is passed to the top-level View, which receives the event and distributes it according to the event dispatch mechanism.

If a View’s onTouchEvent returns FALSE, the onTouchEvent of its parent container will be called, and so on. If none of them handle the event, the Activity will handle it.

The ViewGroup event distribution process looks something like this: If the top-level ViewGroup intercepting event (onInterceptTouchEvent) returns true, the event will be given to the ViewGroup. If the ViewGroup’s onTouchListener is set, then onTouch will be called. Otherwise onTouchEvent will be called.

If both are set, onTouch will block the onTouchEvent. If onClickerListener is set in onTouchEvent, onClick will be called. If the top-level ViewGroup does not intercept, then the event will be passed to its click event subview and the subview’s dispatchTouchEvent will be called

View event distribution

dispatchTouchEvent -> onTouch(setOnTouchListener) -> onTouchEvent -> onClick

The difference between onTouch and onTouchEvent is that both of them are called within dispatchTouchEvent, onTouch takes precedence over onTouchEvent, and if onTouch returns true, then onTouchEvent is not executed, And onClick are not executed.

3. Please explain the drawing process of View (Ali)

In an XML layout file, our layout_width and layout_height arguments can be wrap_content or match_parent instead of the specific size. These two Settings do not specify the actual size, but the View we draw onto the screen must have a specific width and height, and because of this, we have to handle and set the size ourselves. Of course, the View class gives the default handling, but if the View class’s default handling doesn’t meet our requirements, we have to override the onMeasure function.

The onMeasure function is an int that contains the measurement mode and size. An int takes 32 bits, but Google uses the first two bits of the int to distinguish between different layout patterns, and the last 30 bits to store the size of the data.

The onMeasure function is used as follows:

MeasureSpec has three measurement modes:

Match_parent – > EXACTLY. How do you understand that?

So match_parent is just going to take advantage of all the remaining space that the parent View gives us, and the remaining space of the parent View is defined, which is the size that’s stored in the integer of this measurement mode.

Wrap_content – > AT_MOST. How do you understand that?

If we want to set the size to wrap the contents of our view, then the size is the size that the parent view gives us as a reference, as long as it does not exceed this size, the specific size can be set according to our needs.

Fixed size (e.g. 100dp) — >EXACTLY. The user specified the size, we do not need to interfere, of course, is the size of the specified.

4. Please describe the drawing process of ViewGroup. (tencent)

Customizing a ViewGroup isn’t that easy, because it has to manage not only its own views but also its subviews. We all know that a ViewGroup is a View container that holds a Child View and is responsible for putting the Child View in the specified location.

First, we need to know the size of each subview. Only by knowing the size of the subview will we know how big the current ViewGroup should be set to accommodate them.

The size of the ViewGroup depends on the size of the child View and what function our ViewGroup is trying to implement

Now that you have the size of the ViewGroup and the child View, the next thing to do is to place it. How do you place it? For example, you want the child views to be placed next to each other in vertical order, or on top of each other in sequential order. That’s up to you.

Already know how to put is not good ah, decided how to put is equivalent to the existing space “split” into large and small space, each space corresponds to a child View, we next is to put the child View, put them in their place.

Next to share

⑤ what is a MotionEvent? How many events are involved? Under what conditions? (idle fish)

⑥, how to resolve the View event conflict? What’s an example in development? (IQiyi)

How does Scroller realize the elastic sliding of View? (a second-tier Internet company in Shenzhen)

What is the difference between SurfaceView and View? (netease)

Interview Knowledge Reference

Github

The last

If you have to study together, you can follow my public account — ❤️ [Program Ape Program] ❤️. I will regularly do interviews/technology sharing every week. Come and study with me!