How to get the width and height of an android.widget.ImageView?
My answer on this question might help you:
int finalHeight, finalWidth;
final ImageView iv = (ImageView)findViewById(R.id.scaled_image);
final TextView tv = (TextView)findViewById(R.id.size_label);
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
iv.getViewTreeObserver().removeOnPreDrawListener(this);
finalHeight = iv.getMeasuredHeight();
finalWidth = iv.getMeasuredWidth();
tv.setText("Height: " + finalHeight + " Width: " + finalWidth);
return true;
}
});
You can then add your image scaling work from within the onPreDraw() method.
How to get the width and height of an Image View in android?
Where you calling getWidth()
and getHeight()
on ImageView? If you calling from onCreate()
in activity, it won't work. You need to wait for activity window to attached and then call getWidth() and getHeight()
on ImageView. You can try calling getWidth() and getHeight()
from onWindowFocusChanged()
method of your activity.
@Override
public void onWindowFocusChanged(boolean hasFocus){
int width=imageView.getWidth();
int height=imageView.getHeight();
}
Get the displayed size of an image inside an ImageView
the following will work:
ih=imageView.getMeasuredHeight();//height of imageView
iw=imageView.getMeasuredWidth();//width of imageView
iH=imageView.getDrawable().getIntrinsicHeight();//original height of underlying image
iW=imageView.getDrawable().getIntrinsicWidth();//original width of underlying image
if (ih/iH<=iw/iW) iw=iW*ih/iH;//rescaled width of image within ImageView
else ih= iH*iw/iW;//rescaled height of image within ImageView
(iw x ih) now represents the actual rescaled (width x height) for the image within the view (in other words the displayed size of the image)
EDIT: I think a nicer way to write the above answer (and one that works with ints) :
final int actualHeight, actualWidth;
final int imageViewHeight = imageView.getHeight(), imageViewWidth = imageView.getWidth();
final int bitmapHeight = ..., bitmapWidth = ...;
if (imageViewHeight * bitmapWidth <= imageViewWidth * bitmapHeight) {
actualWidth = bitmapWidth * imageViewHeight / bitmapHeight;
actualHeight = imageViewHeight;
} else {
actualHeight = bitmapHeight * imageViewWidth / bitmapWidth;
actualWidth = imageViewWidth;
}
return new Point(actualWidth,actualHeight);
How to get the height and width of an ImageView/Bitmap in Android
You can get height and width of ImageView by using getWidth() and getHeight() through while this will not give you the exact width and height of the image, for getting the Image width height first you need to get the drawable as background then convert drawable to BitmapDrawable to get the image as Bitmap from that you can get the width and height like here
Bitmap b = ((BitmapDrawable)imageView.getBackground()).getBitmap();
int w = b.getWidth();
int h = b.getHeight();
or do like here
imageView.setDrawingCacheEnabled(true);
Bitmap b = imageView.getDrawingCache();
int w = b.getWidth();
int h = b.getHeight();
the above code will give you current imageview sized bitmap like screen shot of device
for only ImageView size
imageView.getWidth();
imageView.getHeight();
If you have drawable image and you want that size you can get like this way
Drawable d = getResources().getDrawable(R.drawable.yourimage);
int h = d.getIntrinsicHeight();
int w = d.getIntrinsicWidth();
How to get the height and width of ImageView?
A common mistake made by new Android developers is to use the width and height of a view inside its constructor. When a view’s constructor is called, Android doesn't know yet how big the view will be, so the sizes are set to zero. The real sizes are calculated during the layout stage, which occurs after construction but before anything is drawn. You can use the onSizeChanged()
method to be notified of the values when they are known, or you can use the getWidth()
and getHeight()
methods later, such as in the onDraw()
method.
Get the Height and the Width of drawn area of an ImageView
I think that what is drawn outside your bitmap
is lost, and you can check this by rotating your ImageView
, you will see that what was outside your bitmap
is black even if you drew ther.
Related Topics
How to Use an Arrayadapter in Android of Custom Objects
How to Implement Drag and Drop in Android 2.2
Write to /Res/Drawable/ on the Fly
How to Detect If User First Time in Firebase
Validate Username and Email Crashed and Cannot Insert to Firebase Database
Recyclerview Changing Items During Scroll
How to Display a List of Images in a Listview in Android
Can't Include C++ Headers Like Vector in Android Ndk
How to Add New Column to Android SQLite Database
Android + Pair Devices via Bluetooth Programmatically
How to Keep the Spaces at the End And/Or at the Beginning of a String
Difference Between Surfaceview and View
Versioncode VS Versionname in Android Manifest
Android Emulator: How to Monitor Network Traffic
Difference Between Android-Support-V7-Appcompat and Android-Support-V4
Low-Latency Audio Playback on Android