启动画面不会转换到我的主菜单屏幕

时间:2012-08-30 16:01:57

标签: android android-activity transition splash-screen

我目前正在开发一个应用程序,它应该从启动画面转换到主菜单屏幕。我已经完成了编码,但似乎并没有这样做。任何人都可以看出下面的代码有什么问题吗?

启动画面活动:

public class MainActivity extends Activity {

public static final String GAME_PREFERENCES = "GamePrefs";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    // fade in animation
    TextView logo1 = (TextView)findViewById(R.id.TextViewTopTitle);
    Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    logo1.startAnimation(fade1);

    // custom animation
    Animation spining = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
    LayoutAnimationController controller = new LayoutAnimationController(spining);
    TableLayout table = (TableLayout)findViewById(R.id.TableLayout01);
    for(int i=0; i < table.getChildCount(); i++)
    {
        TableRow row = (TableRow) table.getChildAt(i);
        row.setLayoutAnimation(controller);
    }

    startAnimations();

    // saving game preferences
    SharedPreferences settings = getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
    SharedPreferences.Editor prefEditor = settings.edit();
    prefEditor.putString("UserName", "JaneDoe");
    prefEditor.putInt("UserAge", 22);
    prefEditor.commit();
}

private void startAnimations() {
    // Transition from Splash screen to Main Menu screen
    Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.fade_in2);
    fade2.setAnimationListener(new AnimationListener() {
        public void onAnimationEnd(Animation animation)
        {
            startActivity(new Intent(MainActivity.this,QuizMenuActivity.class));
            MainActivity.this.finish();
        }
        public void onAnimationStart(Animation animation)
        {
            //Nothing
        }
        public void onAnimationRepeat(Animation animation)
        {
            //Nothing
        }
    });
}

主菜单活动:

public class QuizMenuActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
    }
}

我的布局已经完成。关于这里有什么问题的任何建议?

1 个答案:

答案 0 :(得分:1)

也许从未触发过AnimationListener?如果您只想显示启动画面,请离开..如何尝试将以下内容添加到您的Splash活动的onCreate:

Thread timer = new Thread() {           
    public void run() {             
        try {                   
            sleep(5000); // Sets delay before splash appears        
        } catch (InterruptedException e) {
            e.printStackTrace();                
        } finally {
            Intent main = new Intent(Splash.this, MainMenu.class);
            startActivity(main);    
            finish();
        }       
   };   
   timer.start();

它会睡5秒然后进入MainMenu。

修改

我认为您的AnimationListener永远不会被调用,所以请尝试在此处添加fade2.startNow()

private void startAnimations() {
    // Transition from Splash screen to Main Menu screen
    Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.fade_in2);
    fade2.setAnimationListener(new AnimationListener() {
    public void onAnimationEnd(Animation animation)
    {
        startActivity(new Intent(MainActivity.this,QuizMenuActivity.class));
        MainActivity.this.finish();
    }
    public void onAnimationStart(Animation animation)
    {
        //Nothing
    }
    public void onAnimationRepeat(Animation animation)
    {
        //Nothing
    }
});
fade2.startNow();
}

有关如何使用Animation.startNow()Animation.start()

的详细信息

animation.start() or animation.startNow() does not start the animation immediately