【问题标题】:Android splash screen while loading MainActivity [duplicate]加载MainActivity时的Android启动画面[重复]
【发布时间】:2013-10-25 02:48:08
【问题描述】:

所以,我刚读到这个问题:How do I make a splash screen? 但是,我不想添加固定延迟(如在最佳答案中),而是希望在 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。 我该怎么办? 谢谢。

【问题讨论】:

标签: android multithreading wait splash-screen supportmapfragment


【解决方案1】:

简单的方法..

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。

【讨论】:

  • 这不是最好的方法。
  • 是的,这不是最好的方法。还有其他方法,例如 TimerTask、Handler、CountDownTimer。处理程序更可取。
  • 这些也不是最好的方法。
  • bignerdranch.com/blog/splash-screens-the-right-way 这家伙展示了最好的方法。
  • 我之前读过那篇文章,但它没有提供任何延迟,并且启动画面无法正常工作。
【解决方案2】:

主线程不能长时间阻塞。如果您想在 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.
    

    【讨论】:

    • 会试试的!如果是这样,我一定会告诉你的。谢谢。
    • 我没有给你-1,我接受了答案。 (:
    • @HugoM.Zuleta Lol 不是你 :) .. 别人做了。 :)..我只是想知道原因,以便如果我错了我可以改进..
    • 原因?因为它不正确的飞溅
    • 启动屏幕的目的是在加载活动之前显示徽标。它并不意味着固定时间显示。在速度较慢的手机上,闪屏会显示得更长,而在速度较快的手机上,它只会闪烁。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 2010-10-26
    • 2015-10-07
    • 2019-02-11
    • 2022-12-28
    • 1970-01-01
    相关资源
    最近更新 更多