6

I used a image as a picture for my slash screen.

<style name="AppTheme.Launcher">
        <item name="android:windowBackground">@drawable/splash_screen</item>
</style>

However, in some different devices, it has been stretched and distorted. Is there a way to make the photo centercrop like in ImageView?

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Hai Hack
  • 948
  • 13
  • 24
  • this link may help you: https://stackoverflow.com/questions/39082029/make-drawable-do-centercrop-in-layer-list – android Apr 24 '19 at 04:19
  • try this link https://stackoverflow.com/questions/39212977/android-center-splash-screen-image – unzila Apr 24 '19 at 06:39

3 Answers3

0

Rather than directly setting an image , try setting it from a xml drawable this might you a better control over the image .

You can follow the steps shown here:-

Splash the right way

Kaveri
  • 1,060
  • 2
  • 12
  • 21
0

If your drawable is a bitmap image then you have to change the android:gravity attribute to "center" on the bitmap element of the drawable's resource.

The default value is "fill" which "Grow the horizontal and vertical size of the object if needed so it completely fills its container.". More xml bitmap info.

For example drawable/background_splash.xml :

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="@color/colorAccent"></color>
    </item>
    <item>
        <bitmap
            android:src="@drawable/logo"
            android:gravity="center"
            android:antialias="true"
        />
    </item>
</layer-list>
jhavatar
  • 3,236
  • 1
  • 18
  • 11
  • 7
    set gravity to center only place the drawable in the middle, it doesn't do center crop as ImageView. It should both scale up and crop the drawable. – Hai Hack Apr 28 '19 at 02:54
  • True, I missed the "crop" part. While centering, my solution will not guarantee that both dimensions of the image are equal to or larger than the corresponding dimension of the splash screen. To achieve this (scaling) using my solution you would have to carefully choose the size of the splash image for each of the its screen density buckets (drawable-hdpi, drawable-xhdpi, ...). – jhavatar May 01 '19 at 03:52
-2

Try this solution

First of all get the splash screen image resized in different sizes and put them in drawable folders like drawable-mpdi,drawable-hpdi,drawable-xhpdi,drawable-xxhpdi,drawable-xxxpdi

directory like this

360x640 - mdpi 
540x960 - hpdi
720x1280 - xhpdi
1080x1920 - xxhpdi
1440x2560 - xxxhpdi

Then in xml of splash activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splash_screen">


</RelativeLayout>

Or simply define the drawable in style for the particular activity

<style name="AppTheme.Launcher">
        <item name="android:windowBackground">@drawable/splash_screen</item>
</style>
Quick learner
  • 10,632
  • 4
  • 45
  • 55