Transferred from: http://blog.csdn.net/johnny901114/article/details/7839512
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:
public class Myimageview extends ImageView {public Myimageview (context context, AttributeSet Attrs) {Super (context, attrs );} Public Myimageview (Context context) {super (context);} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, HEIGHTMEASURESPEC); System.out.println ("Onmeasure I was called" +system.currenttimemillis ());} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); System.out.println ("OnDraw I was called" +system.currenttimemillis ());}}
Layout file:
<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:
@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:
Note that when the OnCreate method is executed, the control we define is measured (measure), so we get the height or width of the control through View.getheight () in the OnCreate method, which is definitely 0. Because it has not been measured itself, that is to say he does not know how high he is, and you at this time to get its size, it must not be.
Now we have to solve this problem, on the Internet to find the following methods:
------------------------------------------------method One int w = View.MeasureSpec.makeMeasureSpec (0,view.measu respec.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:
@Override public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.main); Final ImageView ImageView = (ImageView) Findviewbyid (R.id.imageview); ------------------------------------------------method an 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:
@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.
<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:
@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.
@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.