Go Understanding of the Android View.onmeasure method

Source: Internet
Author: User

Transferred from: http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.htmlAndroid The understanding of the View.onmeasure method shows that the view appears on the screen to go through measure (calculation) and layout.
1. When to call onmeasureMethod?
Called when the control's parent element is about to place the control. The parent element asks the child control a question, "How much place do you want to use?" ", and then pass in two parameters--widthmeasurespec and Heightmeasurespec.
These two parameters indicate the space that the control can obtain and the metadata about the space description.
A better way is to pass the height and width of the view to the Setmeasureddimension method so that you can tell the parent control how much place you need to place the child controls.

The next code snippet shows how to override the onmeasureNote that the local empty method of the call is to calculate the height and width. They interpret widthheightspec and Heightmeasurespec values and calculate the appropriate height and width values.
Java code:
  1. @Override
  2. protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
  3. int measuredheight = Measureheight (Heightmeasurespec);
  4. int measuredwidth = Measurewidth (Widthmeasurespec);
  5. Setmeasureddimension (Measuredheight, measuredwidth);
  6. }
  7. private int measureheight (int measurespec) {
  8. Return measured widget height.
  9. }
  10. private int measurewidth (int measurespec) {
  11. Return measured widget width.
  12. }
  13. Boundary parameters--widthmeasurespec and Heightmeasurespec, the reason for efficiency is passed in as integers. Before they are used, the first thing to do is to use the static method of the Measurespec class GetMode and getsize to translate the solution, as shown in the following fragment:

    Java code:
    1. int specmode = Measurespec.getmode (Measurespec);
    2. int specsize = measurespec.getsize (Measurespec);

    According to the value of Specmode, (Measurespec 3 modes are unspecified, exactly and At_most respectively)
  14. If the at_most,specsize represents the largest available space;
    If the exactly,specsize represents a precise size;
    In the case of unspecified, there is no reference to the size of the control.
    2, then these patterns and we usually set the layout parameters fill_parent, wrap_content have what relationship?
    The code tests know that when we set width or height to fill_parent, the measure method in which the container calls the child view in the layout is exactly because the child view occupies the space of the remaining container, so its size is deterministic.
    When set to Wrap_content, the container passes in the At_most, indicating the maximum size of the child view, so that the child view will set its own size according to this limit. When the size of the child view is set to an exact value, the container passes in exactly, and Measurespec's unspecified mode is not currently found under what circumstances.
    View'sonmeasureThe default behavior of the method is to set the size to Mminwidth (usually 0) or the minimum size of the background drawable when the mode is unspecified, or to the size of the incoming at_most when the mode is exactly or Measurespec.
    One idea that needs to be corrected is that fill_parent should be a sub-view that takes up space for the rest of the container, without overwriting the other view spaces that were previously laid out, and of course the layout sub-view has no space to allocate, so the Fill_parent property is important for the layout order. It was thought that the space of all the containers is full, no wonder Google in the 2.2 version of the Fill_parent name changed to Match_parent.

    In both cases, you must deal with these limits absolutely. In some cases, it may return dimensions that exceed these limits, in which case you can have the parent element choose how to treat the out-of-view, crop, or scrolling techniques.
    The following framework code gives a typical implementation of the view measurement:

    Java code:
    1. @Override
    2. protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
    3. int measuredheight = Measureheight (Heightmeasurespec);
    4. int measuredwidth = Measurewidth (Widthmeasurespec);
    5. Setmeasureddimension (Measuredheight, measuredwidth);
    6. }
    7. private int measureheight (int measurespec) {
    8. int specmode = Measurespec.getmode (Measurespec);
    9. int specsize = measurespec.getsize (Measurespec);
    10. The Default size if no limits is specified.
    11. int result = 500;
    12. if (Specmode = = Measurespec.at_most) {
    13. Calculate the ideal size of your
    14. Control within this maximum size.
    15. If your control fills the available
    16. Space return the outer bound.
    17. result = Specsize;
    18. }
    19. else if (Specmode = = measurespec.exactly) {
    20. If your control can fit within these bounds return that value.
    21. result = Specsize;
    22. }
    23. return result;
    24. }
    25. private int measurewidth (int measurespec) {
    26. int specmode = Measurespec.getmode (Measurespec);
    27. int specsize = measurespec.getsize (Measurespec);
    28. The Default size if no limits is specified.
    29. int result = 500;
    30. if (Specmode = = Measurespec.at_most) {
    31. Calculate the ideal size of your control
    32. Within this maximum size.
    33. If your control fills the available space
    34. Return the outer bound.
    35. result = Specsize;
    36. }
    37. else if (Specmode = = measurespec.exactly) {
    38. If your control can fit within these bounds return that value.
    39. result = Specsize;
    40. }
    41. return result;
    42. }

Go Understanding of the Android View.onmeasure method

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.