Bitmap gravity set to center not filling the screen even though the image is big enough

11,475

Solution 1

ImageView's scale type centerCrop was what I wanted. Unfortunately I couldn't specify this property for bitmaps. I changed my splash screen layout to FrameLayout and added an ImageView and TextView overlapping each other. This way I was able to achieve what I wanted.

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

Solution 2

You can use android:gravity="fill" to cover vertical & horizontal direction

Solution 3

For splash image try gravity="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>

Solution 4

Try adding this to the bitmap:

android:gravity="fill_vertical"

This should fix it.

P.S. Sorry for the first answer, I edited it now.

Share:
11,475
MikkoP
Author by

MikkoP

I'm a student living in Finland, interested in mathematical subjects, programming and computers.

Updated on June 13, 2022

Comments

  • MikkoP
    MikkoP almost 2 years

    I have a RelativeLayout in my Android project. This has it's background set to a Bitmap:

    <?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 is a PNG image sized 2560x1440 pixels. I haven't set the background of the layout directly to the image, because the default scale mode (or gravity) is fill which stretches the image to fit the screen. With center it should take correct size image from the center and show it unscaled. In case of a vertical 1080x1920 screen, it should take that big piece and center it in the layout.

    However, I have a problem. The image is bigger than any screen out in the market today. Still, with my Nexus 7, which has a 1920x1080 screen, it has borders around the image. The layout is set to full screen. The image is shrinked vertically.

    enter image description here

    How do I fix this?