Android Beginner Tutorial _ Get the width and height of Android controls

Source: Internet
Author: User

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
  1. Public class Myimageview extends ImageView {
  2. Public Myimageview (context context, AttributeSet attrs) {
  3. Super (context, attrs);
  4. }
  5. Public Myimageview (context context) {
  6. super (context);
  7. }
  8. @Override
  9. protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
  10. super.onmeasure (Widthmeasurespec, Heightmeasurespec);
  11. System.out.println ("Onmeasure I was called" +system.currenttimemillis ());
  12. }
  13. @Override
  14. protected void OnDraw (canvas canvas) {
  15. Super.ondraw (canvas);
  16. System.out.println ("OnDraw I was called" +system.currenttimemillis ());
  17. }
  18. }

Layout file:

[Java]View Plaincopy
    1. <com.test.myimageview
    2. android:id="@+id/imageview"
    3. Android:layout_width="Wrap_content"
    4. android:layout_height="Wrap_content"
    5. android:src="@drawable/test"/>


OnCreate () of the activity to be tested:

[Java]View Plaincopy
    1. @Override
    2. Public void OnCreate (Bundle savedinstancestate) {
    3. super.oncreate (savedinstancestate);
    4. Setcontentview (R.layout.main);
    5. System.out.println ("Execution completed ..."  +system.currenttimemillis ());
    6. }

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
  1. ------------------------------------------------Method One
  2. int w = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified);
  3. int h = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified);
  4. Imageview.measure (W, h);
  5. int height =imageview.getmeasuredheight ();
  6. int width =imageview.getmeasuredwidth ();
  7. Textview.append ("\ n" +height+"," +width);
  8. -----------------------------------------------Method Two
  9. Viewtreeobserver vto = Imageview.getviewtreeobserver ();
  10. Vto.addonpredrawlistener (new Viewtreeobserver.onpredrawlistener () {
  11. Public Boolean Onpredraw () {
  12. int height = imageview.getmeasuredheight ();
  13. int width = imageview.getmeasuredwidth ();
  14. Textview.append ("\ n" +height+"," +width);
  15. return true;
  16. }
  17. });
  18. -----------------------------------------------Method Three
  19. Viewtreeobserver Vto2 = Imageview.getviewtreeobserver ();
  20. Vto2.addongloballayoutlistener (new Ongloballayoutlistener () {
  21. @Override
  22. public void Ongloballayout () {
  23. Imageview.getviewtreeobserver (). Removeglobalonlayoutlistener (this);
  24. Textview.append ("\ n" +imageview.getheight () +"," +imageview.getwidth ());
  25. }
  26. });


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
  1. @Override
  2. public void OnCreate (Bundle savedinstancestate) {
  3. super.oncreate (savedinstancestate);
  4. Setcontentview (R.layout.main);
  5. final ImageView ImageView = (ImageView) Findviewbyid (R.id.imageview);
  6. //------------------------------------------------method one
  7. int w = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified);
  8. int h = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified);
  9. Imageview.measure (W, h);
  10. int height =imageview.getmeasuredheight ();
  11. int width =imageview.getmeasuredwidth ();
  12. Textview.append ("\ n" +height+"," +width);
  13. System.out.println ("Execution completed ..."  +system.currenttimemillis ());
  14. }


Then look at the following several ways to output the results:

Change the test activity to read as follows:

[Java]View Plaincopy
  1. @Override
  2. Public void OnCreate (Bundle savedinstancestate) {
  3. super.oncreate (savedinstancestate);
  4. Setcontentview (R.layout.main);
  5. final ImageView ImageView = (ImageView) Findviewbyid (R.id.imageview);
  6. -----------------------------------------------Method Two
  7. Viewtreeobserver vto = Imageview.getviewtreeobserver ();
  8. Vto.addonpredrawlistener (new Viewtreeobserver.onpredrawlistener () {
  9. Public Boolean Onpredraw () {
  10. int height = imageview.getmeasuredheight ();
  11. int width = imageview.getmeasuredwidth ();
  12. Textview.append ("\ n" +height+"," +width);
  13. return true;
  14. }
  15. });
  16. }


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
    1. <scrollview
    2. Android:layout_width="Wrap_content"
    3. android:layout_height="Wrap_content" >
    4. <textview
    5. android:id="@+id/text"
    6. Android:layout_width="Wrap_content"
    7. android:layout_height="Wrap_content"/>
    8. </ScrollView>


To test the method first:

[Java]View Plaincopy
  1. @Override
  2. Public void OnCreate (Bundle savedinstancestate) {
  3. super.oncreate (savedinstancestate);
  4. Setcontentview (R.layout.main);
  5. final ImageView ImageView = (ImageView) Findviewbyid (R.id.imageview);
  6. -----------------------------------------------Method Two
  7. Viewtreeobserver vto = Imageview.getviewtreeobserver ();
  8. Vto.addonpredrawlistener (new Viewtreeobserver.onpredrawlistener () {
  9. Public Boolean Onpredraw () {
  10. int height = imageview.getmeasuredheight ();
  11. int width = imageview.getmeasuredwidth ();
  12. Textview.append ("\ n" +height+"," +width);
  13. return true;
  14. }
  15. });
  16. }


The results are as follows:

Let's go back to test method three.

[Java]View Plaincopy
  1. @Override
  2. Public void OnCreate (Bundle savedinstancestate) {
  3. super.oncreate (savedinstancestate);
  4. Setcontentview (R.layout.main);
  5. final ImageView ImageView = (ImageView) Findviewbyid (R.id.imageview);
  6. //-----------------------------------------------method three
  7. Viewtreeobserver Vto2 = Imageview.getviewtreeobserver ();
  8. Vto2.addongloballayoutlistener (new Ongloballayoutlistener () {
  9. @Override
  10. public void Ongloballayout () {
  11. Imageview.getviewtreeobserver (). Removeglobalonlayoutlistener (this);
  12. Textview.append ("\ n" +imageview.getheight () +"," +imageview.getwidth ());
  13. }
  14. });
  15. }


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

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.