Pages

Chapter 11.1: Animated Splash Screen in Android

Animated Splash Screen In Android

In this chapter we will learn to implement Animated splash screen with a simple example in Android Studio.
In the Older Splash Screen which we have learned in previous chapter we will just Integrate the Animation. We will set the animation to our company logo to fade and rotate in the splash screen.

Example:
  • In the previous project just include the new directory as anim, right click on res -> New -> Directory and name the directory as anim.
  • In anim folder create a new Animation resource xml file, right click on anim -> New -> Animation resource file and name it as fade.xml.
File: fade.xml:
<!--fade animation-->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">

    <alpha
        android:duration="2000"
        android:fromAlpha="0"
        android:toAlpha="1">

    </alpha>

    <alpha
        android:duration="2000"
        android:fromAlpha="1"
        android:startOffset="2000"
        android:toAlpha="0">

    </alpha>

    <!-- rotate animation-->
    <set xmlns:android="http://schemas.android.com/apk/res/android">

        <rotate xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="5000"
            android:fromDegrees="0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="360">

        </rotate>

        <rotate xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="5000"
            android:fromDegrees="360"
            android:pivotX="50%"
            android:pivotY="50%"
            android:startOffset="5000"
            android:toDegrees="0">

        </rotate>

    </set>

</set>
  • Set the above animation to the logo in java file to start animation.
File: activity_splash_screen.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:padding="20dp"
    tools:context="com.example.hss_24.splashscreenexample.SplashScreen">

    <ImageView
        android:id="@+id/image"
        android:src="@drawable/simply_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>
File: SplashScreen.java:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class SplashScreen extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        //    hide action bar from the activity
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        final ImageView image = (ImageView) findViewById(R.id.image);

        Thread timer = new Thread() {
            public void run() {
                try {
                    // set animation
                    Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade);
                    image.startAnimation(animation);
                    // setting timer
                    sleep(6000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    // launch activity
                    Intent i = new Intent(SplashScreen.this, MainActivity.class);
                    startActivity(i);
                    finish();
                }
            }
        };
        timer.start();
    }
}

File: activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center"
    tools:context="com.example.hss_24.splashscreenexample.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorAccent"
        android:textSize="20sp"
        android:text="Welcome to simplifiedandroid.com" />

</RelativeLayout>
File: MainActivity.java:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
}

  • Run the App and enjoy the Splash screen.
Output:

Download project here
Thank you!!!
Please like and share...

Unknown

Author: Nouman Baig

Editor: Mohammad Jabir

No comments:

Post a Comment