I'm trying to position an ImageView so that the bottom of the image is always pinned to the bottom of the view, no matter how small the height of the ImageView is. However, none of the scale types seem to fit what I am trying to do. CenterCrop is close, but I don't want the image to be centered. Similar to how CSS would handle absolute positioning.
The reason is, I need to animate the height of the ImageView, but make it seem as though it is "revealing" the upper portion of the image. I assume figuring out this method of cropping the image and animating the ImageView height is the easiest way to do this, but if someone knows of a better way I'd love being pointed in the right direction.
Any help appreciated.
I ended up subclassing ImageView and creating a way to enable a 'BottomCrop' type image scaling.
I assigned the image to a RectF of the correct size by calculating the scale and expected image height based on the view height.
Did you try Imageview's Scaletype FIT_END Thts the best available option to show the end of a image.
This solution works fine. A little improvement would make a CustomView customizable from .xml to topCrop or bottomCrop. Here is the full solution on gitHub : ScalableImageView
I used @Jpoliachik code and it worked good, I made a couple of tweaks because sometimes
getWidth
andgetHeight
were returning0
-getMeasuredWidth
andgetMeasuredHeight
solved the problem.Jpoliachik's answer was cool enough to make me wanna generalize it to support both top/bottom and left/right, by a variable amount. :) Now to top crop, just call
setCropOffset(0,0)
, bottom cropsetCropOffset(0,1)
, left crop is alsosetCropOffset(0,0)
, and right cropsetCropOffset(1,0)
. If you want to offset the viewport by some fraction of the image in one dimension, you can call e.g.setCropOffset(0, 0.25f)
to shift it down by 25% of the non-viewable space, while 0.5f would center it. Cheers!