RSS

Search Engine

Friday, November 5, 2010

Splash Screen Activity In Android

Hello Friends,

I have to create splash screen activity. This code is basic code for splash screen.

This are simple step for splash screen. I hope you enjoyed this code.

If you create splashscreen activity then must be devices target name android 1.6 or 1.8 or 2.1 or more then. otherwise this project cany be run in your devices.

1 ==> First Step

Create one android project. Name This android project is android. And go to res/layout/main.xml file.

In main.xml file code is given below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#000000">
<com.splash.SplasGraphics
android:id="@+id/graphics"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>

Now this com.splash.SplasGraphics name is your own file name. This are very important. if you give your project name then change it.

2 ==> Second Step

Now go to src/com.splash/splash.java

In Java File Splash.java Open it and given code it below.

package com.splash;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.MotionEvent;

import android.view.Window;

import android.view.WindowManager;

import android.view.animation.AccelerateInterpolator;

import android.view.animation.Animation;

import android.view.animation.AnimationSet;

import android.view.animation.BounceInterpolator;

import android.view.animation.ScaleAnimation;

import android.view.animation.TranslateAnimation;

public class Splash extends Activity {

protected boolean _active = true;

protected int _splashTime = 2000;

AnimationSet rootSet;

SplasGraphics graphics;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

graphics = (SplasGraphics) findViewById(R.id.graphics);

rootSet = getAnimation();

Thread splashTread = new Thread()

{

@Override

public void run()

{

graphics.startAnimation(rootSet);

try

{

int waited = 0;

while (_active && (waited <>

{

sleep(100);

if (_active)

{

waited += 100;

}

}

} catch (InterruptedException e)

{} finally

{

_active = false;

finish();

}

}

};

splashTread.start();

}

private AnimationSet getAnimation()

{

rootSet = new AnimationSet(true);

rootSet.setInterpolator(new BounceInterpolator());

TranslateAnimation trans1 = new TranslateAnimation(0, 0, -400, 0);

trans1.setStartOffset(0);

trans1.setDuration(800);

trans1.setFillAfter(true);

rootSet.addAnimation(trans1);

ScaleAnimation scale = new ScaleAnimation(0, 1, 0, 1, ScaleAnimation.RELATIVE_TO_SELF, 0.5f,

ScaleAnimation.RELATIVE_TO_SELF, 0.5f);

scale.setDuration(800);

scale.setFillAfter(true);

AnimationSet childSet = new AnimationSet(true);

childSet.addAnimation(scale);

childSet.setInterpolator(new BounceInterpolator());

rootSet.addAnimation(childSet);

Animation outtoRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f,

Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,

Animation.RELATIVE_TO_PARENT, 0.0f);

outtoRight.setStartOffset(4000);

outtoRight.setDuration(400);

outtoRight.setInterpolator(new AccelerateInterpolator());

rootSet.addAnimation(outtoRight);

return rootSet;

}

@Override

public boolean onTouchEvent(MotionEvent event)

{

if (event.getAction() == MotionEvent.ACTION_DOWN)

{

_active = false;

}

return true;

}

}


Now

3 ==> Third Step

Now Create Anothe Java File. This java file name is SplashGraphics.java

This Java File Code Given Below.

package com.splash;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class SplasGraphics extends View
{
Bitmap p;
Display display;
int textSize = 10;

public SplasGraphics(Context context)
{
super(context);
display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
init();
}

public SplasGraphics(Context context, AttributeSet attrs)
{
super(context, attrs);
display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
init();
}

private void init()
{
this.setFocusableInTouchMode(true);
this.setFocusable(true);
p = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
}

@Override
protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(p, 0, 0, null);
}
}

Now run it.

So this is the splashscreen activity in android devices.

So enjoyed my code.

Here is my Source Code.



7 comments:

himanshu said...

good tutorials keep it up......

Anonymous said...

Nice example!
I found similar example but using Message and Handler classes. http://www.mubasheralam.com/tutorials/android/how-create-splash-activity-android

Anonymous said...

nice tutorial, thx

The website design (white text and black bg), however is hard on the eye

Anonymous said...

STEP BY STEP ANDROID TUTORIALS

VISIT:-www.androidituts.blogspot.com

Anonymous said...

All new android tutorials

VISIT ANDROIDITUTS

http://www.androidituts.com

Anonymous said...

Thanks for this helpful tutorial! I compiled a list of some top resources on building an android splash screen; I included your post. Check it out, feel free to share. Hope other developers find it useful too. http://www.verious.com/board/Giancarlo-Leonio/building-an-android-splash-screen/

Masfikur Rahman said...

You need to have time to take care of the kids active. Please visit our website and play exciting flash games.
Thanks you for sharing!View for more info wellness products .

Post a Comment