Android:如何防止图像在 ImageView 或 ImageButton 中缩放?

Android: How to prevent image from being scaled in ImageView or ImageButton?(Android:如何防止图像在 ImageView 或 ImageButton 中缩放?)

本文介绍了Android:如何防止图像在 ImageView 或 ImageButton 中缩放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用fill_parent"或weight"拉伸视图或按钮,如何防止我的位图在 ImageView 或 ImageButton 中自动缩放?

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"?

这将很有用,例如,在屏幕顶部创建一个 4 按钮工具栏,其中按钮等间距,但即使我使用 scaleType="center",按钮内的图像也会不断拉伸,这应该可以防止根据文档进行缩放,但事实并非如此.

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.

感谢任何见解!

谢谢,

推荐答案

我发现在使用 android:background 时会自动将你正在使用的图像缩放到 View 的大小.

I have found that when using android:background automatically scales the image you are using there to the size of the View.

我尝试使用 android:src 将图像放入视图中.图像没有缩放,但我无法让图像相对于视图的大小居中.

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.

所以我尝试将整个 Button 设为它自己的相对布局,这就是我使用的:

So I tried making the whole Button it's own relative layout and this is what I used:

<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>

ImageButton 位于背景中,并且始终与布局大小相同.然而,ImageView 将始终保持在 RelativeLayout 的中心,并且不会缩放.这样,RelativeLayout 本身可以增长和移动,并且按钮顶部的 Image 将始终保持相同的大小,但按钮会增长.但是,如果 Layout 变得比图像本身小,图像会缩小.

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.

我想这就是你要找的.可能有更好的方法来做到这一点,但这是我现在能想到的.

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.

这篇关于Android:如何防止图像在 ImageView 或 ImageButton 中缩放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android:如何防止图像在 ImageView 或 ImageButton 中缩放?

基础教程推荐