ImageView 一维以适应自由空间,第二个评估以保持纵横比 [英] ImageView one dimension to fit free space and second evaluate to keep aspect ration

查看:14
本文介绍了ImageView 一维以适应自由空间,第二个评估以保持纵横比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要类似<img width="100%"/>的Android .我的意思是将宽度调整为所有可用空间(缩小或放大图像的宽度)并自动更改高度以保持纵横比.

类似于 <ImageView android:layout_width="match_parent" android:layout_height="auto">

Useandroid:scaleType="fitCenter" 用图像做我想要的,但只用图像,不会改变视图本身的高度.当我将展位尺寸设置为 match_parent 时,我会得到我想要的东西,但前提是屏幕上只有一张图片.但我需要图片下的一些文字.

这是创建 ImageView 子项的唯一方法吗?例如android:adjustViewBounds="true"对我没有任何作用.

解决方案

我发现问题出在哪里.ImageView 的默认实现无法放大图像.如果图像大于屏幕宽度 android:adjustViewBounds="true" 正常工作并调整高度以保持图像的纵横比.但如果图像小于屏幕宽度,则不会放大.

这是由这段代码引起的

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

在 ImageView 类中(第 688 行,API 10)

我制作了 ImageView 的特殊子元素,它允许放大图像以适应宽度.

这里是:https://gist.github.com/tprochazka/5486822>

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:scaleType="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

这篇关于ImageView 一维以适应自由空间,第二个评估以保持纵横比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆