Android Custom View-------why rewrite onmeasure () and how to rewrite

Source: Internet
Author: User

These two days in the knowledge of Android custom components, just started to look at a lot of information, still feel that the Onmeasure () method of understanding is not thorough, and then roughly know how onmeasure used, and very curious why need to implement Onmeasure () this method.


This article mainly records two issues, 1) when customizing the component, what circumstances need to implement the Onmeasure () method, 2) How to implement the Onmeasure () method


Why do I need to implement the Onmeasure () method


We look at the following example, we define a view ourselves, very simple, just inherit the view class.

public class MyView extends View {public MyView (context context, AttributeSet attrs, int defstyleattr) {Super (context, ATT RS, defstyleattr);//TODO auto-generated Constructor Stub}public MyView (context context, AttributeSet Attrs) {super ( context, attrs);//Todo auto-generated constructor stub}public MyView (context context) {super (context);//TODO auto-generated constructor stub} @Overrideprotected void Onmeasure (int widthmeasurespec, int heightmeasurespec) {//TODO Auto-generated method Stubsuper.onmeasure (Widthmeasurespec, Heightmeasurespec);}}

Then define the following layout in the layout file:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    xmlns:myview=" Http://schemas.android.com/apk/res/com.notes.notes_onmeasure "    android:layout_width= "match_parent"    android:layout_height= "match_parent"    android:orientation= " Vertical "    android:padding=" @dimen/activity_vertical_margin "    tools:context=" com.notes.notes_onmeasure. Easyactivity ">            <com.notes.notes_onmeasure. MyView        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"        android: background= "#78787878"/>    <textview        android:layout_width= "wrap_content"        android:layout_ height= "Wrap_content"        android:text= "Can you see me?" "/></linearlayout>


The results are shown as


When the width and height of the myview is set to Match_parent, MyView fills the entire interface without question, and when set to a certain value, the size of the MyView is also displayed as the determined value. But in the example above, we set the width and height of the myview to Wrap_content, which still fills the entire interface. The problem is here, so we need to override the Onmeasure () method to control its size. Other components that inherit view, such as Button,textview, have been rewritten by the Onmeasuer () method.


How to rewrite the Onmeasure () method

Android, the size of each view, not only depends on itself, but also affected by the parent view, you can refer to my other article on the Android interface drawing process----How Android draws views. and Onmeasure () is called when the parent view calculates the size of the child view, where the details are not described in detail here, and we only need this for the moment.

    protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {       //,,,    }

The Onmeasure () method has two parameters, which are the constraints that the parent view sends over to the child view. Let's just rewrite the Onmeasure () method in the MyView above, and use an example to understand how the Onmeasure () method is implemented.


@Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//TODO auto-generated method Stubsuper.onmeasure (Widthmeasurespec, heightmeasurespec); int width = measuredimension (n, widthmeasurespec); int Height = measuredimension (heightmeasurespec); setmeasureddimension (width, height);} public int measuredimension (int defaultsize, int measurespec) {int Result;int specmode = Measurespec.getmode (measureSpec int specsize = measurespec.getsize (Measurespec); if (Specmode = = measurespec.exactly) {result = Specsize;} Else{result = defaultsize;   Unspecifiedif (Specmode = = measurespec.at_most) {result = Math.min (result, specsize);}} return result;}

When overriding the Onmeasure () method, you must finally call the Setmeasuredimension () method, or you will get an error. And how do we finally calculate the width and height of the setmeasuredimension ()?


First we parse Widthmeasurespec to get two data, one is the parent view wants the size of the child view, one is about the size of the pattern, the two data are obtained by the following two lines of code


      int specmode = Measurespec.getmode (measurespec);      int specsize = measurespec.getsize (Measurespec);

The Measurespec.getmode () method returns three kinds of results:

    • UNSPECIFIED: There are no requirements for the size of the parent node's child nodes.
    • Exactly: The parent node requires that the size of its child nodes be specified as an exact value. Its child nodes and other descendants need to be adapted to that size.
    • At most: The parent node requires that its child nodes not be larger than a certain maximum, and that the size of its child nodes and other descendants must be less than this value


Android Custom View-------why rewrite onmeasure () and how to rewrite

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.