Transfer from http://my.oschina.net/xiahuawuyu/blog/167949
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:
?
| 123456789101112131415161718192021 |
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 我被调用了"+System.currentTimeMillis()); } @Overrideprotected void onDraw(Canvas canvas) { super.onDraw(canvas); System.out.println("onDraw 我被调用了"+System.currentTimeMillis()); } } |
Layout
?
| 12345 |
<com.test.MyImageViewandroid:id="@+id/imageview"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/test"/> |
OnCreate
?
| 123456 |
@OverridepublicvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); System.out.println("执行完毕.."+System.currentTimeMillis()); } |
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:
?
| 123456789101112131415161718192021222324252627282930 |
//------------------------------------------------方法一 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); //-----------------------------------------------方法二 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; } }); //-----------------------------------------------方法三 ViewTreeObserver vto2 = imageView.getViewTreeObserver(); vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Overridepublic void onGlobalLayout() { imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this); textView.append("\n\n"+imageView.getHeight()+","+imageView.getWidth()); } }); |
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:
?
| 123456789101112131415 |
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("执行完毕.."+System.currentTimeMillis()); } |
Then look at the following several ways to output the results:
Change the test activity to read as follows:
?
| 123456789101112131415 |
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final ImageView imageView = (ImageView) findViewById(R.id.imageview); -----------------------------------------------方法二 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.
?
| 123456789 |
<ScrollViewandroid:layout_width="wrap_content"android:layout_height="wrap_content" > <TextViewandroid:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content" /> </ScrollView> |
First, test method two:
?
| 123456789101112131415 |
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final ImageView imageView = (ImageView) findViewById(R.id.imageview); -----------------------------------------------方法二 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; } }); } |
Three more test methods.
?
| 1234567891011121314 |
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final ImageView imageView = (ImageView) findViewById(R.id.imageview); //-----------------------------------------------方法三 ViewTreeObserver vto2 = imageView.getViewTreeObserver(); vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Overridepublic void onGlobalLayout() { imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this); textView.append("\n\n"+imageView.getHeight()+","+imageView.getWidth()); } }); } |
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, not used, such as the ListView.
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 gets a height width of 0 when the processing