Sunday, February 23, 2014

How to solve the problem of view Oh, width and height


android View the getWidth (), getHeight always 0 oh ask how to solve?

View cv = context.getWindow () getDecorView ();.
bmp = Bitmap.createBitmap (cv.getWidth (), cv.getHeight (), Bitmap.Config.ARGB_8888);

Found cv.getWidth (), cv.getHeight () values ​​are 0, causes an exception, ask how to solve Oh<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
With getMeasuredHeight () and getMeasuredWidth ()
 View cv = context.getWindow () getDecorView ();. 
bmp = Bitmap.createBitmap (cv.getMeasuredWidth (), cv.getMeasuredHeight (), Bitmap.Config.ARGB_8888);

Reply:
cited a floor ncepu307 reply:
with getMeasuredHeight () and getMeasuredWidth ()
 View cv = context.getWindow () getDecorView ();. 
bmp = Bitmap.createBitmap (cv.getMeasuredWidth (), cv.getMeasuredHeight (), Bitmap.Config.ARGB_8888);


Reply:
 
/ / ------------------------------------------------ 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);




/ / ----------------------------------------------- Methods 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;
}
});
/ / ----------------------------------------------- Methods three
ViewTreeObserver vto2 = imageView.getViewTreeObserver ();
vto2.addOnGlobalLayoutListener (new OnGlobalLayoutListener () {
@ Override
public void onGlobalLayout () {
imageView.getViewTreeObserver () removeGlobalOnLayoutListener (this);.
textView.append ("\ n \ n" + imageView.getHeight () + "," + imageView.getWidth ());
}
});

Reply:
Not only shows your view
Reply:
Are you going to get a wide-screen high?

No comments:

Post a Comment