【问题标题】:Bitmap gravity set to center not filling the screen even though the image is big enough即使图像足够大,位图重力设置为中心也不填满屏幕
【发布时间】:2014-03-19 12:13:44
【问题描述】:

我的 Android 项目中有一个 RelativeLayout。这将其背景设置为位图:

<?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 屏幕的情况下,它应该占据那个大块并在布局中居中。

但是,我有一个问题。该图像比当今市场上的任何屏幕都大。不过,对于我的 Nexus 7,它有一个 1920x1080 的屏幕,它的图像周围有边框。布局设置为全屏。图像垂直缩小。

我该如何解决这个问题?

【问题讨论】:

  • 将 splash2.png 放在 res/drawable-nodpi 中
  • @pskink 这没有帮助。我之前在不同的文件夹中复制了相同的图像。感谢您指出drawable-nodpi。这将节省一些空间。
  • 所以现在你。将它放在一个文件夹中(nodpi),它有重力=中心(因此没有缩放)并且仍然 y9u 说 png 不会覆盖整个屏幕?
  • @pskink 行为与问题中指出的相同。
  • 有点奇怪,你试过hierarchyviewer吗?

标签: android image android-layout bitmap android-drawable


【解决方案1】:

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

【讨论】:

  • fill 导致图像拉伸
【解决方案2】:

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>

【讨论】:

    【解决方案3】:

    对于初始图像,请尝试重力 =“center|fill”

    <?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>
    

    【讨论】:

    • 错了,center|fill 和独立的fill 的效果一样 = 图片被拉伸了,太糟糕了
    【解决方案4】:

    尝试将其添加到位图中:

    android:gravity="fill_vertical"
    

    这应该可以解决它。

    附:对不起第一个答案,我现在编辑它。

    【讨论】:

    • 位图没有scaleType 属性。
    • 你是对的,这会垂直填充屏幕,但会拉伸图像。它不会裁剪正确大小的部分。
    • 那你为什么不用经典的 ImageView 呢?我发现它更容易使用。
    猜你喜欢
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多