We all know that the height of the control in OnCreate () is 0, which is why? Let's take a look at the example:
First we write ourselves a control, which is very simple:
[Java]View Plaincopy
- Public class Myimageview extends ImageView {
- Public Myimageview (context context, AttributeSet attrs) {
- Super (context, attrs);
- }
- Public Myimageview (context context) {
- super (context);
- }
- @Override
- protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
- super.onmeasure (Widthmeasurespec, Heightmeasurespec);
- System.out.println ("Onmeasure I was called" +system.currenttimemillis ());
- }
- @Override
- protected void OnDraw (canvas canvas) {
- Super.ondraw (canvas);
- System.out.println ("OnDraw I was called" +system.currenttimemillis ());
- }
- }
Layout file:
[Java]View Plaincopy
- <com.test.myimageview
- android:id="@+id/imageview"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:src="@drawable/test"/>
OnCreate () of the activity to be tested:
[Java]View Plaincopy
- @Override
- Public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- System.out.println ("Execution completed ..." +system.currenttimemillis ());
- }
Now let's look at the results:
The description of the OnCreate method is done, and the control we define is measured (measure), so we get the height or width of the control in the OnCreate method through View.getheight () is definitely 0, because it has not been measured, That means he doesn't know how tall he is, and when you go to get its size, it's definitely not going to work.
Now we have to solve this problem, on the Internet to find the following methods:
[Java]View Plaincopy
- ------------------------------------------------Method One
- int w = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified);
- int h = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified);
- Imageview.measure (W, h);
- int height =imageview.getmeasuredheight ();
- int width =imageview.getmeasuredwidth ();
- Textview.append ("\ n" +height+"," +width);
- -----------------------------------------------Method Two
- Viewtreeobserver vto = Imageview.getviewtreeobserver ();
- Vto.addonpredrawlistener (new Viewtreeobserver.onpredrawlistener () {
- Public Boolean Onpredraw () {
- int height = imageview.getmeasuredheight ();
- int width = imageview.getmeasuredwidth ();
- Textview.append ("\ n" +height+"," +width);
- return true;
- }
- });
- -----------------------------------------------Method Three
- Viewtreeobserver Vto2 = Imageview.getviewtreeobserver ();
- Vto2.addongloballayoutlistener (new Ongloballayoutlistener () {
- @Override
- public void Ongloballayout () {
- Imageview.getviewtreeobserver (). Removeglobalonlayoutlistener (this);
- Textview.append ("\ n" +imageview.getheight () +"," +imageview.getwidth ());
- }
- });
Here are three ways to find out what has been forgotten now.
Now we're going to talk about which method to use when we need it.
Now change the activity of the test to the following:
[Java]View Plaincopy
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- final ImageView ImageView = (ImageView) Findviewbyid (R.id.imageview);
- //------------------------------------------------method one
- int w = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified);
- int h = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified);
- Imageview.measure (W, h);
- int height =imageview.getmeasuredheight ();
- int width =imageview.getmeasuredwidth ();
- Textview.append ("\ n" +height+"," +width);
- System.out.println ("Execution completed ..." +system.currenttimemillis ());
- }
Then look at the following several ways to output the results:
Change the test activity to read as follows:
[Java]View Plaincopy
- @Override
- Public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- final ImageView ImageView = (ImageView) Findviewbyid (R.id.imageview);
- -----------------------------------------------Method Two
- Viewtreeobserver vto = Imageview.getviewtreeobserver ();
- Vto.addonpredrawlistener (new Viewtreeobserver.onpredrawlistener () {
- Public Boolean Onpredraw () {
- int height = imageview.getmeasuredheight ();
- int width = imageview.getmeasuredwidth ();
- Textview.append ("\ n" +height+"," +width);
- return true;
- }
- });
- }
The results are as follows:
Method Three is no longer tested with the same method two!!!
So what is the difference between a method and a method three in execution?
We add a textview to the layout file to record the width height of the control.
[Java]View Plaincopy
- <scrollview
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content" >
- <textview
- android:id="@+id/text"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"/>
- </ScrollView>
To test the method first:
[Java]View Plaincopy
- @Override
- Public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- final ImageView ImageView = (ImageView) Findviewbyid (R.id.imageview);
- -----------------------------------------------Method Two
- Viewtreeobserver vto = Imageview.getviewtreeobserver ();
- Vto.addonpredrawlistener (new Viewtreeobserver.onpredrawlistener () {
- Public Boolean Onpredraw () {
- int height = imageview.getmeasuredheight ();
- int width = imageview.getmeasuredwidth ();
- Textview.append ("\ n" +height+"," +width);
- return true;
- }
- });
- }
The results are as follows:
Let's go back to test method three.
[Java]View Plaincopy
- @Override
- Public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- final ImageView ImageView = (ImageView) Findviewbyid (R.id.imageview);
- //-----------------------------------------------method three
- Viewtreeobserver Vto2 = Imageview.getviewtreeobserver ();
- Vto2.addongloballayoutlistener (new Ongloballayoutlistener () {
- @Override
- public void Ongloballayout () {
- Imageview.getviewtreeobserver (). Removeglobalonlayoutlistener (this);
- Textview.append ("\ n" +imageview.getheight () +"," +imageview.getwidth ());
- }
- });
- }
The output results are as follows:
I think the difference between the two methods and the three methods is needless to say.
Summary: Then need to get the width of the control of the method?
Method One: More than the other two methods more than one calculation, that is, multiple calls to the Onmeasure () method, although the method looks simple, but if the target control calculation time is relatively large (such as ListView, etc.), is not recommended to use.
Method Two, its callback method will be called many times, and sliding textview when the time is called, so it is not recommended.
Method three, more appropriate.
Of course, the actual application needs to be based on the actual situation.
Android Beginner Tutorial _ Get the width and height of Android controls