在splashscreen之前的白色屏幕

时间:2013-02-25 13:01:45

标签: android performance

我的SplashScreenActivity存在问题,当我在手机上启动应用程序时,它显示白屏大约0.5秒。 MainActitivy扩展FragmentActivityAndroidManifest我将SplashScreenActivity声明为启动器,纵向模式为screenOrientation

代码:

public class SplashScreenActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.splashscreen);
    randomSplash();

    Thread splashscreen = new Thread() {

        public void run() {
            try {
                Thread.sleep(1000);
                Intent mainScreen = new Intent("com.rm.jkrm.MAINACTIVITY");
                startActivity(mainScreen);
            } catch (InterruptedException e) {

            } finally {
                finish();
            }
        }
    };
    splashscreen.start();
}

private void randomSplash(){
    Random random = new Random();
    int i = random.nextInt(4);

    LinearLayout ln = (LinearLayout) findViewById(R.id.splashscreen);

    switch(i){
    case 1: 
        ln.setBackgroundResource(R.drawable.splash1);
        break;
    case 2: 
        ln.setBackgroundResource(R.drawable.splash2);
        break;
    case 3: 
        ln.setBackgroundResource(R.drawable.splash3);
        break;
    default: 
        ln.setBackgroundResource(R.drawable.splash0);
        break;
    }
}
}

XML:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/splashscreen"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
</LinearLayout>

3 个答案:

答案 0 :(得分:1)

 Thread splashscreen = new Thread() {

        public void run() {
            try {
                Thread.sleep(1000);
                Intent mainScreen = new Intent("com.rm.jkrm.MAINACTIVITY");
                startActivity(mainScreen);
            } catch (InterruptedException e) {

            } finally {
                finish();
            }
        }
    };
    splashscreen.start();

这是你的问题UI线程睡觉不是一个很好的主意使用处理程序 而且我认为这也可能导致异常。

Handler h=new Handler();
        h.postDelayed(new Runnable() {

            public void run() {
                // TODO Auto-generated method stub
                startActivity(new Intent(Splash_Activity.this,Main_Activity.class));
                finish();
            }
        }, 2000);
    }

答案 1 :(得分:1)

您需要在AsyncTask中运行这两个操作:

setContentView(R.layout.splashscreen);
randomSplash();

setContentView放在doInBackground方法中,并在postExecute方法中运行randomSplash

答案 2 :(得分:1)

将AndroidManifest.xml文件中的SplashActivity主题更改为此。

android:theme="@android:style/Theme.Translucent.NoTitleBar"