creating background drawable using layer-list, icon getting stretched

himanshu1496 picture himanshu1496 · Mar 21, 2016 · Viewed 7.1k times · Source

Hi I am trying to create a background drawable for my splash screen which I'll be setting in theme itself. But the bitmap drawable used to keep in the center is getting stretched and I am not able to figure how to keep it normal. Below is my drawable code: splash_screen_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <gradient
            android:angle="360"
            android:centerColor="@color/colorAccentXDark"
            android:endColor="@color/Black"
            android:gradientRadius="500dp"
            android:startColor="@color/colorAccentXDark"
            android:type="radial"
            android:useLevel="false" />
    </shape>
</item>
<item
    android:bottom="50dp"
    android:top="50dp">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:innerRadius="500dp"
        android:innerRadiusRatio="1"
        android:shape="oval">
        <gradient
            android:angle="360"
            android:centerColor="@color/colorAccentXDark"
            android:endColor="@color/colorAccentXDark"
            android:gradientRadius="400dp"
            android:startColor="@color/colorAccent"
            android:type="radial"
            android:useLevel="false" />
    </shape>
</item>
<item android:gravity="center">
    <bitmap android:src="@drawable/ty_logo" />
</item>
</layer-list>

Here is code where I am setting this drawable as background of an activity:

 <style name="TYTheme" parent="SearchActivityTheme.NoActionBar">
    <item name="colorPrimaryDark">@color/colorAccentXDark</item>
    <item name="android:alertDialogTheme">@style/AlertDialogTheme</item>
    <item name="android:windowBackground">@drawable/splash_screen_bg</item>
</style>

So here the bitmap drawable ty_logo is an png is getting stretched in my phone. Since there is no scaleType option with bitmapDrawable I don't know how to handle it.

Answer

nt-complete picture nt-complete · May 12, 2016

Add gravity to your bitmap set to center.

<item android:gravity="center">
    <bitmap android:src="@drawable/ty_logo" 
            android:gravity="center" />
</item>