Skip to content

Instantly share code, notes, and snippets.

@ityancs
Last active December 21, 2015 03:16
Show Gist options
  • Save ityancs/c4513ff54e89c1064b19 to your computer and use it in GitHub Desktop.
Save ityancs/c4513ff54e89c1064b19 to your computer and use it in GitHub Desktop.
public class measureSize{
public void getSize1(){
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();
}
public void getSize2(){
ViewTreeObserver vto = imageView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
vto.removeOnPreDrawListener(this);
int height = imageView.getMeasuredHeight();
int width = imageView.getMeasuredWidth();
return true;
}
});
}
public void getSize3(){
ViewTreeObserver vto = imageView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
imageView.getHeight();
imageView.getWidth();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment