开发者

ImageView one dimension to fit free space and second evaluate to keep aspect ration

开发者 https://www.devze.com 2023-03-09 23:02 出处:网络
I need something like<img width=\"100%\" /> for Android <ImageView>. I mean resize width to all available space (shrink or enlarge width of image) and automatically change height to keep a

I need something like<img width="100%" /> for Android <ImageView>. I mean resize width to all available space (shrink or enlarge width of image) and automatically change height to keep aspect ratio.

Something like <ImageView android:layout_width="match_parent" android:layout_height="auto">

Useandroid:sc开发者_Python百科aleType="fitCenter"do with image what I want, but only with image, doesn't change height of View itself. When I set booth dimensions to match_parent I will get what I want, but only if I have only one image on the screen. But I need some texts under image.

It's only way create child of ImageView? For exampleandroid:adjustViewBounds="true"does't do anything for me.


I found where is problem. Default implementation of ImageView can't enlarge image. If image is bigger than screen width android:adjustViewBounds="true" works correctly and adjust height to keep aspect ratio of image. But if image is smaller that screen width it don't upscale it.

This is cause by this code

if (newHeight <= heightSize) {
    heightSize = newHeight;
} 

in ImageView class (on line 688, API 10)

I made special child of ImageView which allow enlarge image to fit width.

Here is: https://gist.github.com/tprochazka/5486822

0

精彩评论

暂无评论...
验证码 换一张
取 消