![]() |
Android Animated Splash Screen Source code Tutorial |
In this Android Tutorial , you will learn How to add an animated splash screen to your Android app
You can add this Splash Screen either to Website2App Project or Start a New Android Studio Project.
build Animated splash screen Using android studio 2.2.3
you can download this version Here :
- Creat New android studio project with the following :
Application name : AnimatedSplashScreen
Package Name : com.nulledapps.AnimatedSplashScreen
MinSDK API 15 : Android 4.0.3(IceCreamSandich)
Click Next
- Add an Activity to Mobile Select (Add No Activity) and Click Next
- Click Finish without change anythings , and wait until android studio build project
-Right Click on your project package ,Select New -- Activity -- EmptyActivity Name the activity : MainActivity.java Click Finish
-Right Click on your project package , Select New -- Activity -- EmptyActivity Name the activity SplashActivity.java Check Launcher Activity and Click Finish
Now we will write the codes for the SplashActivity.java , so we need A Splash Time with an interval of 4 seconds , To end splash screen and show the MainActivity , we also need to creat an android animation for the splash screen we will use an android alpha animation and android translate animation those two files are written in xml
- Right Click on res Folder Select New -- Android resource directory -- Type the directory name : anim and Click OK
-Right Click on anim Folder Select New - Animation Resource file : set the name alpha.xml
- Open alpha.xml and remove all lines and copy/past the following codes : :
<?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="3000" />
- Right Click on anim Folder Select New - Animation Resource file : set the name translat.xml
- Open translate and remove all lines and copy/past the following codes :
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0%" android:toXDelta="0%" android:fromYDelta="200%" android:toYDelta="0%" android:duration="2000" android:zAdjustment="top" />
</set>
-Right Click on drawable Folder Select New - Drawable Resource file : set the nama radialback.xml
- Open radialback.xml and remove all lines and copy/past the following codes :
- Open radialback.xml and remove all lines and copy/past the following codes :
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#e1051b" android:endColor="#ed0427" android:gradientRadius="326" android:type="radial"/>
</shape>
-Right Click on drawable Folder Select New - Image asset , select form icon type Action bar and Tabs icons ,type the name splashlogo ,ASSET TYPE clip art, Theme HOLO_DARK , Click Next and Finish
- Expand layout folder and open activity_splash.xml
-Open activity_splash.xml , Remove all lines and copy/past the following codes :
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/radialback" android:layout_gravity="center" android:id="@+id/lin_lay" android:gravity="center" android:orientation="vertical" >- Open activity_main.xml Remove all lines and add the following xml:
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/logo" android:background="@drawable/splashlogo" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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" tools:context="com.nulledapp.animatedsplashscreen.MainActivity">
<TextView android:text="HELLO !!" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="133dp" android:layout_marginStart="133dp" android:layout_marginBottom="248dp" android:id="@+id/textView" android:textColor="@color/colorPrimary" android:textSize="24sp" android:textStyle="normal|bold" />
<TextView android:text="Splash Screen Android Tutorial" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="47dp" android:id="@+id/textView2" android:textStyle="bold" android:textSize="18sp" android:textColor="@color/colorPrimaryDark" android:layout_alignTop="@+id/textView" android:layout_centerHorizontal="true" />
</RelativeLayout>
- Open SplashActivity.java and Remove All lines the copy/past the following codes :
Method StartAnimation() : instantiate Android Animation And call the alpha animation using An animationUtils To LoadAnimation and assing the animation to A LinearLayout the start the animation , also the method animate an ImageView (Logo) when imageview loaded we clear animation using clearAnimation()package com.nulledapp.animatedsplashscreen;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class SplashActivity extends AppCompatActivity {
private static SplashTimer timer;
private final static int TIMER_INTERVAL = 4000; // 2 sec
private boolean activityStarted;
private boolean exitAds = false;
private boolean mWasGetContentIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
Intent intent = getIntent();
mWasGetContentIntent = intent.getAction().equals(
Intent.ACTION_GET_CONTENT);
setContentView(R.layout.activity_splash);
StartAnimations();
timer = new SplashTimer();
timer.sendEmptyMessageDelayed(1, TIMER_INTERVAL);
}
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
final class SplashTimer extends Handler {
@Override
public void handleMessage(Message msg) {
post(new Runnable() {
public void run() {
timer = null;
startHomePageActivity();
}
});
}
}
private void startHomePageActivity() {
if (activityStarted) {
return;
}
activityStarted = true;
SplashActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
});
}
private void StartAnimations() {
Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
anim.reset();
LinearLayout l=(LinearLayout) findViewById(R.id.lin_lay);
l.clearAnimation();
l.startAnimation(anim);
anim = AnimationUtils.loadAnimation(this, R.anim.translate);
anim.reset();
ImageView iv = (ImageView) findViewById(R.id.logo);
iv.clearAnimation();
iv.startAnimation(anim);
}
}
Method startHomePageActivity():
check if the activity started if yes a runable thread started for 4 seconds
and end the splash screen the open the Main activity
class SplashTimer Handle the Timer - Open AndroidManifest.xml , Remove All Lines and add the following:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.nulledapp.animatedsplashscreen">Now Clean And build Your project To see Result below : YouTube video Tutorial :
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity" />
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>Download android Splash Screen source code : HERE
No comments:
Post a Comment