Android 位图重心设置为“中心”,即使图像足够大也不会填充屏幕

Android 位图重心设置为“中心”,即使图像足够大也不会填充屏幕,android,image,android-layout,bitmap,android-drawable,Android,Image,Android Layout,Bitmap,Android Drawable,我在我的Android项目中有一个相对人。将其背景设置为位图: <?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center" android:src="@drawable/splash2" /> splash2是一个大小为2560x1440像素的PNG图

我在我的Android项目中有一个相对人。将其背景设置为位图:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center"
    android:src="@drawable/splash2" />

splash2
是一个大小为2560x1440像素的PNG图像。我没有将布局的背景直接设置为图像,因为默认的缩放模式(或重力)是
fill
,它拉伸图像以适应屏幕。使用
center
时,应从中心获取正确大小的图像,并显示未缩放的图像。如果是垂直1080x1920屏幕,则应将该大屏幕置于布局的中心

然而,我有一个问题。这个图像比当今市场上任何一个屏幕都要大。尽管如此,我的Nexus7有一个1920x1080的屏幕,图像周围有边框。布局设置为全屏。图像垂直收缩


如何修复此问题?

尝试将其添加到位图:

android:gravity="fill_vertical"
这应该可以解决它


另外,对于第一个答案我很抱歉,我现在编辑了它。

ImageView的缩放类型
centerCrop
正是我想要的。很遗憾,我无法为位图指定此属性。我将启动屏幕布局更改为FrameLayout,并添加了相互重叠的ImageView和TextView。这样我就能够实现我想要的

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SplashScreen"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/splash2"
        android:scaleType="centerCrop" />

     <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/roadSignName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="#FFF"
            android:gravity="center_vertical|center_horizontal"
            android:padding="10dp"
            android:text="My program"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#000000"
            android:textSize="40sp" />
    </RelativeLayout>
</FrameLayout>


您可以使用android:gravity=“fill”覆盖垂直和水平方向

对于飞溅图像,请尝试gravity=“center | fill”



将splash2.png置于res/drawable中-nodpi@pskink这没用。我以前在不同的文件夹中复制了相同的图像。不过,感谢您指出
可绘制的nodpi
。这将节省一些空间。所以现在您可以。把它放在一个文件夹中(nodpi)它有重力=中心(因此没有缩放),但仍然y9u说png没有覆盖整个屏幕?@pskink行为与问题中指出的相同。有点奇怪,你试过hierarchyviewer吗?位图没有
scaleType
属性。你是对的,这会垂直填充屏幕,但这会使形象有所延伸。它没有裁剪出正确大小的工件。那么为什么不使用经典的ImageView呢?我发现它更容易使用。错误,
center | fill
与独立的
fill
=图像被拉伸,这是可怕的
fill
导致图像stretching@mikep这是对的。我也有同样的问题。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/black" />

    <item>
        <bitmap
            android:gravity="center|fill"
            android:src="@drawable/fondo" />
    </item>
</layer-list>