0

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

闪屏活动:

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);
    }
}

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

4

1 回答 1

1

也许 AnimationListener 永远不会被触发?如果您只想显示 Splash 屏幕然后离开.. 尝试将以下内容添加到您的 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

于 2012-08-30T16:08:23.127 回答