在不同分辨率下缩放图像的XML

6
我已经创建了4个分辨率文件夹,并按照下面图片中所示,将适当的图像大小放入每个文件夹中:

enter image description here

我还为各种屏幕尺寸创建了8个不同的布局文件夹,如下图所示:

enter image description here

正如您在下面的图片中看到的那样,我需要根据每个XML文件中的要求重新调整图像大小,以便它们填充屏幕:

enter image description here

我使用这段代码来缩小图像,效果很好:
android:adjustViewBounds="true"  
    android:maxWidth="150dp" 
    android:scaleType="fitCenter"

但是我不知道如何对图像进行扩展。 我一直在尝试使用以下代码:

android:adjustViewBounds="true"  
    android:minWidth="150dp" 
    android:scaleType="fitCenter"

有关如何放大图片的任何想法吗?谢谢!

XML:

    <ImageButton
    android:id="@+id/btnPlay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/imageView1"
    android:layout_marginTop="15dp"
    android:background="@null"
    android:src="@drawable/btn_play"
    android:adjustViewBounds="true"  
    android:scaleX="1.5"
    android:scaleY="1.5"
    android:scaleType="fitXY"  />
3个回答

10

你尝试过吗?:

android:scaleX="1.5" // 1 is 100% size
android:scaleY="1.5"

1
这段代码可以调整实际图像的大小,但是imageView不会随之缩放,因此图片会重叠。有什么办法可以让imageView与图像一起缩放吗? - Shane
是的,我在imageView上有android:layout_width="wrap_content"和android:layout_height="wrap_content"。 - Shane
1
将所有的图像视图都包裹在一个布局中,将布局的宽度和高度设置为match_parent,并将重力设置为center或center_horizontal。 - Cԃաԃ
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/34121/discussion-between-c-and-shane - Cԃաԃ

1
请使用android:scaleType="fitXY"代替android:scaleType="fitCenter" 编辑
请使用带有layout_weight和weightSumLinearLayout 根据您的UI调整WeighSumlayout_weight
 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="8" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3" >

        <!-- your Match m Image -->
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:background="#fff" >

        <!-- your play image -->
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <!-- your Multiplayer image -->
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <!-- your High score Image -->
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <!-- your how to play Image -->
    </LinearLayout>
</LinearLayout>

测试过了,但图像仍无法缩放。 - Shane
@Shane 使用 android:layout_width="match_parent" 和 android:layout_height="match_parent"。 - Tarsem Singh
"match_parent" 会用 imageView 填满整个屏幕。我已经在我的问题中更新了 XML。 - Shane
@Shane 然后使用带有 layout_weight 和 weightSum 的线性布局。 - Tarsem Singh
这似乎需要改变很多代码才能扩大不同尺寸屏幕上的图像。我在考虑只为所有分辨率制作xhdpi图像,并将它们缩小用于其他布局。 - Shane
我的整个应用程序都是使用RelativeLayout设计的。如果要将所有布局更改为Linear,那将是一件非常痛苦的事情。我很惊讶,因为我已经完全按照Android的指示来支持多屏幕,但解决问题的方法并不容易。 - Shane

0

你应该使用新的尺寸限定符。这是最好的方法。你现在所做的并没有错,但是由于有如此多不同品牌的设备,它已经过时了。请参考this


尺寸限定符是在Android 3.2中引入的。我的应用程序需要支持至少API 8。 - Shane

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接