Android View Drawing Knowledge Quiz

Source: Internet
Author: User

 The drawing process of the 1.View is divided into several steps, from where to start? What is the view when the process is over?

A: Starting from Viewroot's performtraversals, three processes are passed through Measure,layout,draw. After the draw process is finished, you can see the view on the screen.

  is the measurement width of 2.view different from the actual width and height?

A: Basically 99 of the case can be considered no difference. There are two kinds of situations, there are differences. The first is that sometimes the view is measured multiple times for some reason, and the width and height of the first measurement is not necessarily equal to the final actual width, but in this case

The width and height of the last measurement are consistent with the actual width. In addition, the actual width is determined in the layout process, we can in the layout process to write the actual width and height of writing hard code, so that the width of the measurement and the actual width is not the same, although it is meaningless and not good.

  3. What happens if the Onmeasure method does not handle wrap_content in a custom view?

A: If you do not handle wrap_content, even if you set it to Wrap_content in XML. The effect is the same as match_parent. Look at the analysis of question 4. We can see that view's layout is wrap, and that mode is at_most (regardless of what Father View is Specmode).

This mode is equal to Specsize (getdefaultsize function Analysis), and here the specsize is obviously the size of parentsize. That is, the remaining size of the parent container. Isn't that the same effect as we set directly into Match_parent?

The solution is to do special processing in onmeasure for wrap, such as specifying a default width, and setting the default width height when found to be wrap_content.

  4.ViewGroup do you have a onmeasure method?

A: No, this method is given to the subclass to implement itself. Different ViewGroup sub-categories certainly layout are not the same, that Onmeasure simply give them all to their own realization.

  5. Why are measurement widths not available in the activity life cycle? Is there any way to solve this problem?

A: Because the process of measure has nothing to do with the life cycle of the activity. You cannot be sure which life cycle is completed after the measure process of the view must be finished. There are several ways to get the measurement width of a view.

// This method of rewriting activity  Public void onwindowfocuschanged (boolean  hasfocus) {  super. onwindowfocuschanged (  Hasfocus);   if  (hasfocus) {  int width = tv.getmeasuredwidth ();   int height = tv.getmeasuredheight ();  LOG.V ("Burning", "width==" + width);  LOG.V ("Burning", "height==" + height); }}  

or override this method

@Override protected void  OnStart () {  super. OnStart (); Tv.post (new  Runnable () {@Override  publicvoid  run () {  int
     width = tv.getmeasuredwidth ();   int height = tv.getmeasuredheight ();  }  }); }

  What is the difference between 6.layout and OnLayout methods?

A: layout determines the location of the view itself and OnLayout is the location where all child elements are determined. In layout, the position of the four vertices of the view is set by the Serframe method. These 4 positions are fixed to determine the location of your view.

The onlayout is then called to determine the position of the child element. Neither view nor ViewGroup's OnLayout method is written. All left to ourselves. Layout of child elements

  How many steps does the 7.draw method have?

A: There are altogether 4 steps to draw the background ——— draw your own --– draw Chrildren-- draw decorations .

  What is the use of the 8.setWillNotDraw method?

Answer: This method is in view.

  

/** * If This view doesn ' t does any drawing in its own, set this flag to * allow further optimizations.  By default, the this flag isn't set on * View, but could am set on some view subclasses such as ViewGroup.  * * Typically, if you override {@link@param  Willnotdraw Whether or no this View draw On its own  */publicvoid Setwillnotdraw (boolean?)  will_not_draw:0, Draw_mask); }  

Used to set the flag bit this means that if your custom view does not need draw, you can set this method to true. This way the system knows that you do not need draw for this view to optimize execution speed. ViewGroup is usually set to True by default, because ViewGroup is mostly responsible for layout

Not responsible for draw. The view flag bit is usually closed by default.

Android View Drawing Knowledge Quiz

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.