5

I am building an Android Application that has a :

Splash Screen , which displays the logo of the Application

What is working :

The screen is being displayed perfectly well and is going to FirstActivity as desired if I set a ImageView in a layout for the SplashScreenActivity .

What isn't :

However this isn't the correct approach as it produces a delay when the App starts as layout is being inflated . I have used the recommended approach as follows :

splash.xml

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

styles.xml

<style name="Splash" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/splash</item>
</style>

AndroidManifest.xml

<application
    android:theme="@style/Splash">
  • When i tried to set the bitmap src from mipmap folder it produced a tiny image .

  • As I also had the SVG file of the logo I tried using Vector Asset to produce a drawable but bitmap src needs an image and the App crashed .

  • Then I tried to generate a PNG from SVG using ImageMagick , InkScape and other tools with their recommended options for high quality images .

But it still isn't as sharp as using a ImageView with a Vector Drawable as its source and finally I can't think of any other way now .

So , how can I achieve the same quality of the image like all other Apps have ? Also is there any way I can make bitmap use the SVG itself ?

hajas
  • 113
  • 1
  • 6
  • try generating PNGs using GIMP with different resolutions --> the ones I'm working with are : 830x830 (xxhdpi) 553x553 (xhdpi) 415x415 (hdpi) – user 007 Jul 02 '18 at 11:15

2 Answers2

3

@hrk sing is right, dont even bother trying to display a bitmap correctly. you will always get low resolution images. the best you can do is really create a vectordrawable even online on a page such as vectr. what I did is silmply scale the vectordrawable directly in the xml and it finally worked perfectly for my splash screen

<vector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:aapt="http://schemas.android.com/aapt"
android:viewportWidth="640"
android:viewportHeight="640"
android:width="240dp"
android:height="240dp">
<path
    android:pathData="M320.96 55.9L477.14 345L161.67 345L320.96 55.9Z"
    android:strokeColor="#292929"
    android:strokeWidth="24" />
</vector>

in the code above I am rescaling a drawable I drew on a 640x640 canvas to be 240x240. then i just put in in my splash screen drawable like so and it works great:

<?xml version="1.0" encoding="utf-8"?>

<!-- The background color, preferably the same as your normal theme -->
<item>
    <shape>
        <size android:height="120dp" android:width="120dp"/>
        <solid android:color="@android:color/white"/>
    </shape>
</item>

<!-- Your product logo - 144dp color version of your app icon -->
<item
    android:drawable="@drawable/logo_vect"
    android:gravity="center">

</item>

my code is actually only drawing the triangle in the following picture but here you see what you can achieve with this. Resolution is finally great as opposed to the pixelated edges I was getting when using bitmap.

my apps splash screen

quealegriamasalegre
  • 2,887
  • 1
  • 13
  • 35
  • 1
    happy to have helped. you might find that you get a distorted image on API 21 to 23. check a more complete version of this answr to deat with that issue https://stackoverflow.com/a/60124091/10637400 – quealegriamasalegre Jul 05 '20 at 23:22
-1

You Should use vector instead of png or jpg

to use vector in android use

app:srcCompat="@drawable/logo"

and you need to add vectorDrawables.useSupportLibrary = true to your build.gradle file

 // Gradle Plugin 2.0+  
   android {  
   defaultConfig {  
   vectorDrawables.useSupportLibrary = true  
  }  
}  

for more information and usage REFER TO: https://www.androidhive.info/2017/02/android-working-svg-vector-drawables/

hajas
  • 113
  • 1
  • 6
hrk singh
  • 143
  • 12
  • Bitmap requires android:src attribute . You mentioned android:srcCompat which should be app:srcCompat . And setting srcCompat has no effect because bitmap requires and uses src . – hajas Jul 02 '18 at 12:35
  • Instead of a bitmap, you can use Imageview and it supports app:srcCompat – hrk singh Jul 02 '18 at 12:50
  • 1
    ImageView requires a layout . Setting layout is not the recommended way for a Splash screen . It's clearly mentioned in the question . – hajas Jul 02 '18 at 12:55
  • Sorry I didn't see that and maybe right now I think you can create your own logo using photoshop or gimp of preferable size – hrk singh Jul 02 '18 at 13:29