Wide-height measurement of view in Android

Source: Internet
Author: User

///

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);

  1. int  w = view.measurespec.makemeasurespec ( 0 ,view.measurespec.unspecified);   
  2. < 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);   
  3. 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

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.