首页 文章

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

提问于
浏览
9

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

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

任何见解都表示赞赏!

谢谢,

3 回答

  • 10

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

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

    所以我尝试将整个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>
    

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

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

  • 2

    只需修改您的drawable,应用更大的透明背景 . 说我在hdpi中的drawable是41 * 48 px icon.png我用透明背景创建了一个60 * 60像素的新图像,复制粘贴上一个icon.png并以相同名称保存 . 这样您就不需要触摸布局了 .

    然后,如果您应用非透明 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>
    

    将上一个和新的drawable应用于 android:src ,您应该清楚地看到ImageButton区域 .

    顺便说一句,我在兼容模式下运行应用程序 .

  • 3

    如果缩放图像,请确保您没有在兼容模式下运行应用程序(例如,如果您在不支持多个密度的情况下定位Android 1.5 / 1.6并且在Android 2.0上运行该应用程序 . )

相关问题