overridePendingTransition不适用于android 2.3.5

时间:2012-07-12 13:58:51

标签: android animation android-activity

我的这段代码不起作用...我已经检查了这个网站上的所有链接,并且还尝试了动画监听器,但仍然无法正常工作。

public class SplashScreenPage extends Activity implements Runnable{

Thread splash;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_screen_page_layout);

    splash = new Thread(this);
    splash.start();
}

@SuppressWarnings("static-access")
@Override
public void run() {
    try {
        splash.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    Intent intent = new Intent(SplashScreenPage.this,LoginPage.class);
     startActivity(intent);
     finish();
     SplashScreenPage.this.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
}

@Override
protected void onPause() {
    overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
    super.onPause();

}
}

3 个答案:

答案 0 :(得分:5)

问题在于设备的默认设置。转到设置>显示>动画>允许“所有动画”。这将允许overridePendingTransition正常工作。

答案 1 :(得分:2)

我看到的问题是你在主线程的另一个Thread中使用你的动画。该主线程也称为UI线程。所以,你需要回到那个UI线程。这应该工作(我使用overridePendingTransition(0,0)删除任何动画,你可以试验其他动画:

public class SplashActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_splash);

        presentLogo();
    }

    /** Called to present the Splash image for an amount of time */
    private void presentLogo() {
        final SplashActivity splashActivity = this;
            new Thread() {
                public void run() {
                    synchronized (splashActivity) {
                        try {
                            sleep(Constants.SPLASH_PRESENTATION_DURATION);
                        } catch (InterruptedException e) {
                        } finally {
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    finish();
                                    overridePendingTransition(0, 0);

                                    // After splash, go to the new activity
                                    Intent intent = new Intent();
                                    intent.setClass(splashActivity, LoginActivity.class);
                                    startActivity(intent);
                                }
                         });
                     }
                 }
             }
         }.start();
     }
 }

答案 2 :(得分:0)

试试这个:

Intent intent = new Intent(SplashScreenPage.this,LoginPage.class);
 startActivity(intent);
 finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);