///
General, view measurement methods:
@Override public void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.main); final ImageView ImageView = (ImageView) Findviewbyid (R.id.imageview); 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 ()); }//------------------------------------------------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 (). removeglobalonlayoutlist Ener (this); Textview.append ( "\ n" +imageview.getheight () + "," +imageview.getwidth ()) ; } });
Recently, because you want to make a custom view, you need to measure the view width to find a good API:
View.MeasureSpec.makeMeasureSpec (Int,int)
The parameter 1 is a numeric value that is calculated based on the
Parameter 2 is a mode that refers to the method of measurement, mode is as follows:
It has three modes:
①, UNSPECIFIED (unspecified), the parent element imposes any constraint on the element, and the child element can get the desired size arbitrarily;
②, exactly (full), the parent element determines the exact size of the element, and the child element is confined to the given boundary and ignores its size;
③, At_most (at most), child elements up to a specified size value.
The specific use is as follows:
int w = View.MeasureSpec.makeMeasureSpec (screen_w,view.measurespec.at_most); int h = View.MeasureSpec.makeMeasureSpec (Screen_h/2,view.measurespec.at_most); View.measure (W, h); Logutil.showlog ("W:" + W + ", H:" + h);
- int w = view.measurespec.makemeasurespec ( 0 ,view.measurespec.unspecified);
- < span class= "keyword" style= "margin:0px; padding:0px; Border:none; Color:green; Background-color:inherit; Font-weight:bold ">int h = view.measurespec.makemeasurespec ( 0 ,view.measurespec.unspecified);
- Textview.measure (W, h);
///
Reference:
Http://blog.sina.com.cn/s/blog_4172d3d60101h15p.html
http://blog.csdn.net/listening_music/article/details/7210146
Http://www.tuicool.com/articles/jiuqEr
Wide-height measurement of view in Android