3

所以,我刚读到这个问题:如何制作启动画面? 但是,我不想添加固定延迟(如在最佳答案中),而是希望在 MainActivity(带有 MapFragment)加载时保持启动画面。

    public class SplashScreen extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_splash);

            Thread t = new Thread(new Runnable() {          
                @Override
                public void run() {
                    Intent i = new Intent(SplashScreen.this, MainActivity.class);
                    startActivity(i);
                    synchronized (this) {
                          try {
                            wait(3000);
                            System.out.println("Thread waited for 3 seconds");
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }       
                }
            });
            try {
                t.start();
                t.join();
                finish();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
}

我添加了 wait(3000) 行,因为我之前注意到该线程没有存活很长时间。但是,如果我让它等待更长时间,就会出现持续更长时间的黑屏。由于某种原因,SplashScreen 活动不会显示 ImageView。我应该怎么办?谢谢。

4

3 回答 3

4

很简单的做法。。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.example.tabs.R;

public class Splash extends Activity implements Runnable
{

    Thread mThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.splash);

        mThread = new Thread(this);

        mThread.start();
    }

    @Override
    public void run() 
    {
        try
        {
            Thread.sleep(2000);
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        finally
        {
            startActivity(new Intent(Splash.this, MainActivity.class));

            finish();
        }
    }

}

如果要显示图像,则 splash.xml

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


</LinearLayout>

请注意,如果您想在 splash 中进行一些 UI 操作。那么您必须创建一个处理程序并在其中更新 UI。

于 2014-04-22T05:39:55.760 回答
1

主线程不能长时间阻塞。Handler如果您想在 3 秒内开始另一个事件,您应该使用触发另一个事件。您可以使用sendMessageDelayed。另外,startActivity应该在主线程中调用。

于 2013-10-25T06:41:14.543 回答
-3

制作这样的启动画面:

while(counter < 1300) {
    try {
            Thread.sleep(1000);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        counter+=100;
    }

    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
    startActivity(intent);

希望能帮助到你!

编辑: Anotehr 引起轰动的方式是这样的:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    public void run() {
         Intent intent = new Intent(getApplicationContext(), YourNextActivity.class);
         startActivity(intent);
    }
},3000); -> the splash will show for 3000 ms, you can reduce this.
于 2013-10-25T06:50:04.527 回答