Java Android:如何防止图像在 ImageView 或 ImageButton 中被缩放?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2406172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to prevent image from being scaled in ImageView or ImageButton?
提问by user277827
How can I prevent my bitmap from being scaled automatically in an ImageView or ImageButton if the view or button is stretched using "fill_parent" or using "weight"?
如果使用“fill_parent”或“weight”拉伸视图或按钮,如何防止我的位图在 ImageView 或 ImageButton 中自动缩放?
This will be useful, for example, to create a 4-button toolbar at the top of the screen where the buttons are equally spaced, but the images inside the buttons keep getting streched even if I use scaleType="center", which should prevent scaling according to the doc, but it doesn't.
这将很有用,例如,在屏幕顶部创建一个 4 按钮工具栏,其中按钮等距,但即使我使用 scaleType="center",按钮内的图像也会不断拉伸,这应该可以防止根据文档进行缩放,但事实并非如此。
Any insight is appreciated!
任何见解表示赞赏!
Thanks,
谢谢,
采纳答案by Matt Swanson
I have found that when using android:background automatically scales the image you are using there to the size of the View.
我发现使用 android:background 时会自动将您在那里使用的图像缩放到视图的大小。
I tried using the android:src to put the image in the view. The image did not scale, but I could not get the image to center relative to the size of the View.
我尝试使用 android:src 将图像放入视图中。图像没有缩放,但我无法使图像相对于视图的大小居中。
So I tried making the whole Button it's own relative layout and this is what I used:
所以我尝试让整个 Button 成为它自己的相对布局,这就是我使用的:
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ImageButton android:id="@+id/ImageButton01"
android:layout_height="fill_parent"
android:layout_width="fill_parent"></ImageButton>
<ImageView android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/cardback1"
android:layout_centerInParent="true"></ImageView>
</RelativeLayout>
The ImageButton is in the background and will always be the same size as the layout. The ImageView however will always stay centered in the RelativeLayout and will not scale. This way the RelativeLayout itself can grow and move and the Image on top of the button will always stay the same size, but the button will grow. The image will however shrink if the Layout becomes smaller than the image itself.
ImageButton 位于背景中,并且始终与布局大小相同。然而 ImageView 将始终保持在 RelativeLayout 的中心并且不会缩放。这样,RelativeLayout 本身可以增长和移动,按钮顶部的图像将始终保持相同的大小,但按钮会增长。但是,如果布局变得小于图像本身,图像将缩小。
I think that's what you were looking for. There may be a better way to do this, but this is all I can come up with right now.
我想这就是你要找的。可能有更好的方法来做到这一点,但这就是我现在所能想到的。
回答by Romain Guy
If the image is scaled, make sure you are not running your app in compatibility mode (for instance if you target Android 1.5/1.6 without supporting multiple densities and you run the app on Android 2.0.)
如果图像被缩放,请确保您没有在兼容模式下运行您的应用程序(例如,如果您的目标是 Android 1.5/1.6 而不支持多密度,并且您在 Android 2.0 上运行应用程序。)
回答by Raymond Chenon
Simply modify your drawable, apply a larger transparent background. Say my drawable in hdpi is 41*48 px icon.png . I created a new image 60*60 px with a transparent background , copy-paste the previous icon.png and save under the same name. This way you don't need to touch your layouts .
只需修改您的可绘制对象,应用更大的透明背景。假设我在 hdpi 中的 drawable 是 41*48 px icon.png 。我创建了一个带有透明背景的新图像 60*60 px ,复制粘贴之前的 icon.png 并以相同的名称保存。这样你就不需要触摸你的布局。
Then you can check the button zone larger if you apply a non transparentandroid:background:
然后,如果您应用非透明android:background ,则可以检查更大的按钮区域:
<ImageButton android:id="@+id/widget_open"
android:src="@drawable/icon_widget_arrow_up"
android:background="#ff777777"
android:layout_height="wrap_content"
android:layout_width="wrap_content"></ImageButton>
Apply the previous and new drawable to android:src, you should clearly see ImageButton area.
将以前的和新的 drawable 应用到android:src,您应该清楚地看到 ImageButton 区域。
By the way, I am running the app in compatibility mode.
顺便说一下,我正在以兼容模式运行该应用程序。