问题 Android布局和自动图像大小调整


我的Android应用程序目前有两个布局,用于其启动画面,纵向和横向。两者都使用相同的简单格式 - 与屏幕大小相同的图像,以简单的线性布局保存:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<ImageView
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:scaleType="fitCenter"
        android:src="@drawable/splash_p"
       />
</LinearLayout>

使用的图像是320w x 480h,Android会自动调整大小以适应屏幕,无论设备的屏幕尺寸如何。显然,例如,当为平板电脑调整大小时,图像不那么清晰。

我应该在这里指出,我的目标是尽可能减少最终应用程序的安装大小,并且我知道我可以为每个不同的屏幕大小包含相同图像的不同布局和大小。

我的启动画面由屏幕顶部三分之一处的应用程序名称组成,然后是屏幕底部三分之二处的图像。为了节省内存,我想将应用程序名称和图像裁剪成两个单独的图像,然后以纵向模式显示的设备以线性垂直布局显示它们。然后,我将使用线性水平布局的图像,然后使用应用程序名称进行横向模式。

对于肖像布局,我有这个:

     <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
        <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent"            android:layout_height="wrap_content" android:orientation="vertical">
        <View android:layout_height="45dp" android:layout_width="45dp" />   
        <ImageView android:layout_height="fill_parent" 
        android:src="@drawable/splashtext" 
        android:scaleType="fitCenter" 
        android:layout_gravity="center" 
        android:layout_width="fill_parent"></ImageView>

        <View
        android:layout_height="35dp"
        android:layout_width="35dp" />      

       <ImageView
       android:scaleType="fitCenter"
        android:src="@drawable/splashpic"
       android:layout_height="fill_parent" android:scaleType="fitCenter" android:layout_width="fill_parent" android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>

当我在eclipse中显示上述内容时,智能手机看起来没问题,但是在平板电脑上显示时图像没有放大,尽管我使用的是android:scaleType =“fitCenter”参数。我尝试在imageview layout_width和layout_height中使用dips而不是fill_parent,但这也不起作用。

我究竟做错了什么?还有另一种方法吗?

谢谢

我已根据@ KaHa6u的帮助编辑了这个修改过的XML的问题。所以现在我有:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" 
   android:orientation="vertical">
    <LinearLayout android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical">
<View
        android:layout_height="15dp"
        android:layout_width="15dp"
 /> 
<ImageView android:layout_height="wrap_content" 
        android:src="@drawable/splashtext" 
        android:adjustViewBounds="true"
        android:scaleType="fitXY" 
        android:layout_gravity="center" 
        android:layout_width="wrap_content" 
        android:layout_weight="1">
        </ImageView>


<View
        android:layout_height="15dp"
        android:layout_width="15dp"
 /> 
<ImageView
       android:scaleType="fitXY"
       android:src="@drawable/splashpic" 
       android:adjustViewBounds="false"
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:layout_weight="4" 
       android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>

它垂直放大,但不是水平放大,所以当屏幕尺寸增大时,我最终会看到高大的薄图像。


6229
2017-07-26 15:20


起源



答案:


@basinbasin

我刚刚遇到的情况与你解释的情况非常相似。我需要在布局的顶部放置一个ImageView,当手机进入横向时,必须只在HORIZONTALLY伸展并保持其高度。我成功了这个:

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/main_header"
    android:scaleType="fitXY"
/>

当然,还有活动的属性:

<activity android:configChanges="keyboardHidden|orientation">

希望这可以帮助。谢谢。


14
2017-08-24 11:13





fitCenter scaleType保持原始宽高比。你试过fitXY scaleType吗? Matrix.ScaleToFit文档


1
2017-07-26 15:31



谢谢你@ KaHa6uc。我已将其更改为fitXY并且还使用了adjustViewBounds。现在屏幕垂直放大,但不是水平放大。我编辑了我的问题以包含修改后的布局。任何人都有更多想法吗? - basinbasin


答案:


@basinbasin

我刚刚遇到的情况与你解释的情况非常相似。我需要在布局的顶部放置一个ImageView,当手机进入横向时,必须只在HORIZONTALLY伸展并保持其高度。我成功了这个:

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/main_header"
    android:scaleType="fitXY"
/>

当然,还有活动的属性:

<activity android:configChanges="keyboardHidden|orientation">

希望这可以帮助。谢谢。


14
2017-08-24 11:13





fitCenter scaleType保持原始宽高比。你试过fitXY scaleType吗? Matrix.ScaleToFit文档


1
2017-07-26 15:31



谢谢你@ KaHa6uc。我已将其更改为fitXY并且还使用了adjustViewBounds。现在屏幕垂直放大,但不是水平放大。我编辑了我的问题以包含修改后的布局。任何人都有更多想法吗? - basinbasin


您是否尝试将“fill_parent”作为您想要拉伸的ImageView的android:layout_height和android:layout_width值? 我想“wrap_content”设置不会让系统知道调整图像大小的界限。请尝试一下,让我知道结果。


0
2017-08-02 14:21



再次感谢@ KaHa6uc,使用fill_parent更好。我还尝试了相对布局和使用填充,以便我可以将文本图像放在图片上方的屏幕顶部。说实话,我现在已经放弃了。我无法在较大的屏幕上正确渲染文本图像,嘿,它只是一个闪屏。对于大多数用户点击解雇的东西来说,花费了太多的时间和精力。 - basinbasin