The code is simple:
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/cat"/>
Notice the ImageView used fill_parent
for width and height.
The image cat
is a small image and it will be zoomed in to fit the ImageView, and keep the width/height ratio at the same time.
My question is how to get the displayed size of the image? I tried:
imageView.getDrawable().getIntrinsicHeight()
But which it the original height of the image cat
.
I tried:
imageView.getDrawable().getBounds()
But which returns Rect(0,0,0,0)
.
the following will work:
(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) :
Here is a helper function to get the bounds of image in an imageView.
use
I guess a lot of people are coming from this example https://developer.android.com/training/animation/zoom.html and don't want to use
android:scaleType="centerCrop"
(maybe because the ImageView is in a constraint layout and you want to see the small picture uncroped) don't you worry, I got your back!Just replace the entire block beginning with
with the following
works like a charme, you're welcome :)
Further explanation: as usual we check wheter the picture is taller than wide (expanded the height of the picture should match the height of expandedImageView) or vice versa. Then we check if the picture in the original (smaller) ImageView (thumbView) is matching the width or the heigth, so we can adjust for the space. This way we achieve a smooth scaling animation while not croping the picture in the thumbView, no matter it's dimension (as they may change from device to device when using constarints) or that of the picture.