ImageView adjustViewBounds not working(ImageView 调整视图边界不起作用)
问题描述
我有一个带有 android:layout_width=100dp
、android:layout_height=wrap_content
和 android:adjustViewBounds=true
I have an ImageView with android:layout_width=100dp
, android:layout_height=wrap_content
and android:adjustViewBounds=true
它的来源是 50 x 50 像素的图片.但是没有保留纵横比 - ImageView 的高度是 50 像素,而不是 100 像素(即 adjustViewBounds
不起作用).如果我有一张 200x200 像素的图片,它可以工作 - 宽度和高度都是 100 像素.这段代码生成了一个 100px 宽和 50px 高的图片,但 src 图像是方形的:
It's source is a 50 x 50 px picture. But the aspect ratio is not preserved - height of the ImageView is 50px, not 100px (i.e. adjustViewBounds
is not working). If I have a 200x200px picture it works - width and height are 100px. This code results in a 100px wide and 50px tall picture but the src image is square:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/photo"
android:src="QGRyYXdhYmxlL2ljb24="
android:layout_width="100dp"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true" />
</LinearLayout>
推荐答案
问题是adjustViewBounds
不会增加ImageView
> 超出可绘制对象的自然尺寸.它只会缩小视图以保持纵横比;如果您提供 500x500 的图像而不是 50x50 的图像,这应该可以.
The issue is that adjustViewBounds
will not increase the size of the ImageView
beyond the natural dimensions of the drawable. It will only shrink the view to maintain aspect ratio; if you provide a 500x500 image instead of a 50x50 image, this should work.
如果您对实现此行为的位置感兴趣,请参阅 ImageView.java的onMeasure实现.
If you're interested in the spot where this behavior is implemented, see ImageView.java's onMeasure implementation.
一种解决方法是实现一个自定义 ImageView
来改变 onMeasure
中的这种行为.
One workaround is to implement a custom ImageView
that changes this behavior in onMeasure
.
这篇关于ImageView 调整视图边界不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ImageView 调整视图边界不起作用


- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01