加载MainActivity时的Android启动画面

3

我刚刚看到了这个问题:如何制作Android启动画面? 但是,我不想像最佳答案中所述那样添加固定延迟,而是希望在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)行,因为我之前注意到线程活动的时间很短。但是,如果我让它等待更长时间,那么只会有一个持续更久的黑屏。由于某些原因,启动屏幕活动无法显示ImageView。我该怎么办?谢谢。

2
启动画面很糟糕,不要使用它们! - Alex Lockwood
请查看我的最佳且简单的解决方案: https://medium.com/@vatani.ahmad/android-optimal-splash-screen-without-extra-activity-or-fragment-b60fea45a0cc - Ahmad Vatani
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>

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


这不是最好的方式。 - Zeeshan Shabbir
不是最好的方法。还有其他方法,如TimerTask、Handler、CountDownTimer。Handler是首选。 - Zar E Ahmer
那些也不是最好的方法。 - Zeeshan Shabbir
这个人展示了最好的方法。https://www.bignerdranch.com/blog/splash-screens-the-right-way/ - Zeeshan Shabbir
这是一种实际的技术。你可以检查地图或Youtube应用程序的启动画面,它们都使用了这种技术。 - Zeeshan Shabbir
显示剩余4条评论

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

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

希望能有所帮助! 编辑: 另一种制作启动画面的方法如下:
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.

会试一下!如果成功了,我一定会告诉你的。谢谢。 - Hugo M. Zuleta
我没有给你-1,我接受了答案。(: - Hugo M. Zuleta
@HugoM.Zuleta 哈哈,不是你啦 :).. 是其他人干的。:).. 我只是想知道原因,以便我能改进,如果我错了的话.. - Rat-a-tat-a-tat Ratatouille
原因是什么?因为它的闪屏不正确。 - Peter
启动画面的目的是在活动加载完成前显示标志。它不是用于固定时间显示的。在较慢的手机上,启动画面会出现更长时间,在更快的手机上,它只会闪烁一下。 - nizam.sp
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接