ImageView的adjustViewBounds不工作 [英] ImageView adjustViewBounds not working
问题描述
我有一个 Android的一个ImageView的:layout_width = 100dp
,安卓layout_height = WRAP_CONTENT
和安卓:adjustViewBounds = TRUE
这是源是一个50×50像素的图片。但纵横比是不是preserved - ImageView的高度为50像素,而不是100像素(即<$ C $ C> adjustViewBounds 不工作)。如果我有一个200x200px图片它的工作原理 - 宽度和高度100像素。这code导致100像素宽,50像素高的图片,但在src图像是方形的:
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT;的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT&GT;
&LT; ImageView的
机器人:ID =@ + ID /摄
机器人:SRC =@可绘制/图标
机器人:layout_width =100dp
机器人:layout_height =WRAP_CONTENT
机器人:scaleType =fitXY
机器人:adjustViewBounds =真/&GT;
&LT; / LinearLayout中&GT;
问题是, adjustViewBounds
的不会增加的大小的的ImageView
超出绘制的自然尺寸。它只会收缩,以保持高宽比;如果你提供了一个50×50的图像的500×500的图像,而不是,这应该工作。
如果你有兴趣在这个行为实施现场,看到<一href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/ImageView.java">ImageView.java's onMeasure实施。
一个解决办法是实现自定义的ImageView
改变这个行为 onMeasure
。
I have an ImageView with android:layout_width=100dp
, android:layout_height=wrap_content
and android:adjustViewBounds=true
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="@drawable/icon"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true" />
</LinearLayout>
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.
If you're interested in the spot where this behavior is implemented, see ImageView.java's onMeasure implementation.
One workaround is to implement a custom ImageView
that changes this behavior in onMeasure
.
这篇关于ImageView的adjustViewBounds不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!