项目作者: MarichTech

项目描述 :
Android Splash Screen
高级语言: Java
项目地址: git://github.com/MarichTech/SplashScreen.git
创建时间: 2020-08-21T11:20:26Z
项目社区:https://github.com/MarichTech/SplashScreen

开源协议:MIT License

下载


How to create a Splash screen

Well, actually, it’s easy. The ingredients are:
  • An Activity for the Splash Screen (without the layout file)
  • Your Manifest: to declare you Splash Screen as the Launcher
  • One drawable(logo) file to customize the splash screen a little

    The splash view has to be ready immediately, even before you can inflate a layout file in your splash activity.

Api’s Used

For making our splash screen more attractive and more real, I used two animation api’s to ehnhance the look. The api’s are com.daimajia.androidanimations:library and com.airbnb.android:lottie. So, lets get started on adding the dependencies.

The recipe:

Step 1 (Adding dependencies)

Android Animations Library

Gradle

  1. dependencies {
  2. implementation 'com.daimajia.androidanimations:library:2.3@aar'
  3. }

Maven

  1. <dependency>
  2. <groupId>com.android.support</groupId>
  3. <artifactId>support-compat</artifactId>
  4. <version>25.1.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.daimajia.androidanimation</groupId>
  8. <artifactId>library</artifactId>
  9. <version>2.3</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.daimajia.easing</groupId>
  13. <artifactId>library</artifactId>
  14. <version>2.0</version>
  15. </dependency>

Android Lottie

Gradle

Gradle is the only supported build configuration, so just add the dependency to your project build.gradle file:

  1. dependencies {
  2. implementation 'com.airbnb.android:lottie:$lottieVersion'
  3. }

The latest Lottie version is:
lottieVersion

Lottie 2.8.0 and above only supports projects that have been migrated to androidx. For more information, read Google’s migration guide.

Step 2

Create your Splash Screen Activity
  1. public class SplashActivity extends Activity {
  2. //This is the time it will take for the splash screen to be displayed
  3. private static int SPLASH_TIME_OUT = 3000;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_splash);
  8. //This is where we change our app name font to blacklist font
  9. Typeface typeface = ResourcesCompat.getFont(this, R.font.blacklist);
  10. TextView appname= findViewById(R.id.appname);
  11. appname.setTypeface(typeface);
  12. // We use the Yoyo to make our app logo to bounce app and down.
  13. //There is a lot of Attension Techniques styles
  14. // example Flash, Pulse, RubberBand, Shake, Swing, Wobble, Bounce, Tada, StandUp, Wave.
  15. // Your can change the techniques to fit your liking.
  16. YoYo.with(Techniques.Bounce)
  17. .duration(7000)
  18. .playOn(findViewById(R.id.logo));
  19. //This is where we make our app name fade in as it moves up
  20. // There are other Fade Techniques too
  21. //example FadeIn, FadeInUp, FadeInDown, FadeInLeft, FadeInRight
  22. //FadeOut, FadeOutDown, FadeOutLeft, FadeOutRight, FadeOutUp
  23. YoYo.with(Techniques.FadeInUp)
  24. .duration(5000)
  25. .playOn(findViewById(R.id.appname));
  26. new Handler().postDelayed(new Runnable() {
  27. /*
  28. * Showing splash screen with a timer. This will be useful when you
  29. * want to show case your app logo / company
  30. */
  31. @Override
  32. public void run() {
  33. // This method will be executed once the timer is over
  34. // Start your app main activity
  35. startActivity(new Intent(SplashActivity.this,WelcomeActivity.class));
  36. finish();
  37. }
  38. }, SPLASH_TIME_OUT);
  39. }
  40. }

activity_splash.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="@color/colorPrimary"
  8. tools:context=".SplashActivity">
  9. <RelativeLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent">
  12. <ImageView
  13. android:id="@+id/logo"
  14. android:layout_width="100dp"
  15. android:layout_height="100dp"
  16. android:layout_centerHorizontal="true"
  17. android:layout_centerVertical="true"
  18. android:src="@drawable/paw"></ImageView>
  19. <TextView
  20. android:layout_marginTop="10dp"
  21. android:textColor="#fff"
  22. android:textSize="35sp"
  23. android:id="@+id/appname"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_below="@id/logo"
  27. android:text="@string/app_name"
  28. android:layout_centerHorizontal="true"></TextView>
  29. <com.airbnb.lottie.LottieAnimationView
  30. android:id="@+id/animation_view"
  31. android:layout_width="80dp"
  32. android:layout_height="80dp"
  33. app:lottie_fileName="splash.json"
  34. app:lottie_loop="true"
  35. app:lottie_autoPlay="true"
  36. android:layout_gravity="center"
  37. android:layout_alignParentBottom="true"
  38. android:layout_centerHorizontal="true"
  39. android:layout_marginBottom="20dp"></com.airbnb.lottie.LottieAnimationView>
  40. </RelativeLayout>
  41. </androidx.constraintlayout.widget.ConstraintLayout>

and your Manifest should look something like this

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. package="com.marichtech.splashscreen">
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:roundIcon="@mipmap/ic_launcher_round"
  10. android:supportsRtl="true"
  11. android:theme="@style/AppTheme"
  12. tools:ignore="GoogleAppIndexingWarning">
  13. <activity android:name=".SplashActivity"
  14. android:theme = "@style/NoActionBar">
  15. <intent-filter>
  16. <action android:name="android.intent.action.MAIN" ></action>
  17. <category android:name="android.intent.category.LAUNCHER" ></category>
  18. </intent-filter>
  19. </activity>
  20. <activity android:name=".MainActivity"></activity>
  21. </application>
  22. </manifest>

That is it. You can clone the project using Android Studio and have fun.
Dont forget to like and follow me for more projects.