Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Android splash screen using svg instead of png?

I make RN app. For Android version I have made /drawable/splash_screen.xml with following content

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

  <item
    android:drawable="@color/gray"/>

  <item>
    <bitmap
      android:gravity="center"
      android:src="@mipmap/ic_launcher"/>
  </item>

</layer-list>

and links to this file in res/values/styles.xml

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

also in AndoirdManifest.xml

<application
  android:name=".MainApplication"
  android:allowBackup="true"
  android:label="@string/app_name"
  android:icon="@mipmap/ic_launcher"
  android:theme="@style/AppTheme">
  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@style/SplashTheme"
    android:screenOrientation="landscape"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
  <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>

This works fine, but I'd like to make SVG file instead PNG (mipmap/ic_launcher). The SVG file might be like this https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable.html (at least without animation). How to achieve this?

like image 804
CodeBy Avatar asked Mar 04 '17 11:03

CodeBy


People also ask

Can we use SVG in Android Studio?

Android Studio includes a tool called Vector Asset Studio that helps you add material icons and import Scalable Vector Graphic (SVG) and Adobe Photoshop Document (PSD) files into your project as vector drawable resources.

How do you animate a splash screen on Android?

Go to app > java > first package name > right-click > New > Activity > Empty Activity and create another activity and named it as SplashScreen. Edit the activity_splash_screen. xml file and add image, text in the splash screen as per the requirement.


2 Answers

The default SVG importer in the Android Studio doesn't work too well, so I recommend to use a SVG to VectorDrawable converter like svg2android.

Now we need to create a drawable file using the code generated by the svg2android.

res/drawable --> new --> drawable resource file --> paste the vector code there

And on the splash_screen.xml we need to add an item using our vector as drawable with a gravity set to center.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- background with solid color -->
    <item android:drawable="@android:color/white"/>

    <!-- logo -->
    <item
        android:drawable="@drawable/your_logo"
        android:gravity="center"/>

</layer-list>

-- Edited (Dec 2019) --

As @petyr pointed in the comments, the svg2android is now deprecated, but the as @Krisztián suggested, the Respresso Image Converter can do virtually everything that svg2android can. Just add a SVG image into it, leave it in auto and it will produce an Android compatible SVG.

like image 57
Nícolas Schirmer Avatar answered Sep 21 '22 08:09

Nícolas Schirmer


First conversion to xml and second naming of managed elements in svg. (VectorDrawable) android_svg.xml:

   <vector android:height="400dp" android:viewportHeight="206.132".....>
   ...............
    <path android:name = "arrow"
        android:fillColor="#00000000"
        android:pathData="M 47.30268,56.147897 37.688314,45.379807"
        android:strokeAlpha="1" android:strokeColor="#151414"
        android:strokeLineCap="butt" android:strokeLineJoin="miter" android:strokeWidth="1"/>
   </vector>

Third use of AnimatedVectorDrawable to animate VectorDrawable:

 <animated-vector
        xmlns:android="http://schemas.android.com/apk/res/android"
        ............................>
    <target android:name="arrow">
        <aapt:attr name="android:animation">
            <set android:ordering="sequentially">
                <objectAnimator
                    android:duration="1000"
                    android:propertyName="pathData"
                    android:valueFrom="M 47.30268,56.147897 37.688314,45.379807"
                    android:valueTo=  "M 47.30268,56.147897 37.688314,45.379807"
                    android:valueType="pathType"
                    tools:targetApi="lollipop" />
                <objectAnimator
                    android:duration="1000"
                    android:propertyName="pathData"
                    android:valueFrom="M 47.30268,56.147897 37.688314,45.379807"
                    android:valueTo=  "M 47.30268,56.147897 57.686195,45.187519"
                    android:valueType="pathType"
                    tools:targetApi="lollipop" />
            </set>

        </aapt:attr>
    </target>
</animated-vector>

Here is https://www.ap-impulse.com/animaciya-svg-dlya-web-i-android-splash-screen-shag-105/ for a description of how to use the above. Here's the project itself https://github.com/nik137/Diagnostics

like image 45
Alexsandr Shpuryka Avatar answered Sep 21 '22 08:09

Alexsandr Shpuryka