Android 如何缩小图像(ImageView或ImageButton)而不丢失其碎片?

Android 如何缩小图像(ImageView或ImageButton)而不丢失其碎片?,android,xml,image,imageview,imagebutton,Android,Xml,Image,Imageview,Imagebutton,我告诉你我有以下代码 <ImageButton android:id="@+id/imageButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" and

我告诉你我有以下代码

                    <ImageButton
                        android:id="@+id/imageButton1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/happy" />

但是它太大了,我需要缩小它,但是当我这样做的时候,会发生以下情况

                    <ImageButton
                        android:id="@+id/imageButton1"
                        android:layout_width="44dp"
                        android:layout_height="41dp"
                        android:src="@drawable/happy" />



您可以看到图像的某些部分丢失了,如何缩小它而不发生这种情况?

您需要调整原始图像的大小,或者可以使用ImageView(具有scaleType参数)并使其可单击,例如

<ImageView
     android:id="@+id/smiley_iv"
     android:layout_width="44dp"
     android:layout_height="41dp"
     android:src="@drawable/happy"
     android:scaleType="centerInside" 
     android:clickable="true" />


在这种情况下,您可能需要添加自己的背景选择器(用于按下状态等),显然您需要通过xml或编程方式添加onClick回调。

使用布局中的wieght+1解决这个问题,这个问题经常被很多人问到,也是一个令人困惑的问题,不过这是一个可以解决的问题。@Damian谢谢你的回复,帮了我很大的忙,但当我按照你说的做时,图像会显示为白色背景。实际上@pskink是正确的,ImageButton从ImageView扩展而来,所以你只需将安卓:scaleType=“centerInside”添加到你的ImageButton中,它就会保持图像的比例。你应该接受下面的答案。