15

I have a application which shows list of items. i need to display a fadeout image before start of main activity. i tried something like that in onCreate method of main activity i started one animation activity like

Intent animationIntent=new Intent(this,AnimationActivity.class);
startActivityForResult(AnimationIntent);

in onCreate method of AnimationActivity.class

super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView introanim = (ImageView) findViewById(R.id.imgView);
    AnimationSet StoryAnimation = (AnimationSet)AnimationUtils.
loadAnimation(this, R.anim.alphanim);
StoryAnimation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
                AnimationSubActivity.this.finish();

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            AnimationSubActivity.this.finish();
        }
    });

    introanim.clearAnimation();
    introanim.startAnimation(StoryAnimation);

and my main.xml is

<?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"
>
<ImageView  
android:id="@+id/imgView"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:src="@drawable/icon"
/>
</LinearLayout>

problem with this approach is that it display an amimation but imageview come to screen for a while and main activity comes with slide transition effect

but i want i image fade out and my activity should fade in

any help will be appreciated. Thanks in advance

Sunil Pandey
  • 7,042
  • 7
  • 35
  • 48

2 Answers2

30

Basically, what you're looking for is a Splash screen which shows your image and then fades out. A main activity's screen then fades in. So what you could do is create an activity for the Splash screen and then another one for the main activity you might want to call. This would be your Splash Screen activity.

public class SplashScreen extends Activity {
private static final int SPLASH_DISPLAY_TIME = 4000; // splash screen delay time

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    new Handler().postDelayed(new Runnable() {
        public void run() {

            Intent intent = new Intent();
            intent.setClass(Splash.this, NextActivity.class);

            Splash.this.startActivity(intent);
            Splash.this.finish();

            // transition from splash to main menu
            overridePendingTransition(R.animate.activityfadein,
                    R.animate.splashfadeout);

        }
    }, SPLASH_DISPLAY_TIME);
}

NextActivity is any activity that you want to fade in and take its place. For the animations you would need to create the two xml files in a folder called animate in your resources. Here is the splashfadeout.xml file

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zAdjustment="top" android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="4000" />

This would be the activityfadein.xml file

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" />

These files basically make your activities fade in and out

Manish Burman
  • 3,069
  • 2
  • 30
  • 35
  • 1
    why do you use a Handler to start an activity instead of just starting an Activity in OnCreate? – kaid Jun 06 '12 at 09:11
  • 1
    Thanks for this amazing tip. Just a note for others that the folder should be named "anim" instead of "animate". Cheers – Ethan Jul 01 '12 at 02:31
  • Isn't this code starting the other activity in other thread(other than main thread)? I am confused. Please clarify. – shriguru nayak May 02 '13 at 07:49
  • invalid resource directory name. – Lou Morda Nov 04 '14 at 00:31
  • Android already has `android.R.anim.fade_in` and `android.R.anim.fade_out`. They use a different interpolator and are _much_ shorter than these (500 ms vs. 2000 ms). These are way too long. – Sam Oct 15 '21 at 21:50
4

A "Splash Screen" like the one @Manish Burman suggests is not a real one. It's really just a "full screen comercial", which no user wants to spend his time looking at. It has no purpose except being there, meaning it doesn't mask long loading times or something in that region, like a real splash screen should.

If you are to show a splash screen, it should be because you have an initial activity that takes quite some time to load - then a splash screen can be justified, to show the user that stuff is loading or to convince him that the app has not "died".

Check out this guide on how to create a real splash screen.

kaspermoerch
  • 16,127
  • 4
  • 44
  • 67