Fading Background then Go to Next Activity

2019-07-25 21:58发布

问题:

I just wanted to imitate famous games like Angry Birds wherein when you start the game, there are couple of screens, flashing then fading out, then go to another screen, fades out then the main menu comes out. How do i do that? Currently my code is this for the fading in and out. After implementing the code below, surprisingly, it did not animate. Any idea guys?

package com.kfc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.*;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Intro extends Activity {
    LinearLayout screen;
    Handler handler = new Handler();
    int i;
    Intent intent;
    TextView tv;
    Animation mAnim;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.introxml);

        screen = (LinearLayout) findViewById(R.id.myintro);


        Animation fade = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
        fade.setAnimationListener(new AnimationListener() {
            @Override
             public void onAnimationRepeat(Animation animation) {
              // TODO Auto-generated method stub

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

             }
            @Override
             public void onAnimationEnd(Animation animation) {
                startActivity(new Intent(Intro.this, NewKFCActivity.class));
                Intro.this.finish();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
             }

        });
        screen.startAnimation(fade);

    }
}

回答1:

This can be done with activity animations.

Just after invoking startActivity, call

overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

The first argument means that the activity you are about to start is going to fade in, the second argument specifies a fade-out animation for the activity that is currently in the foreground.