簡體   English   中英

在Android應用程序中使用啟動畫面的最佳方法?

[英]Best way to have a splash screen in an Android application?

我的應用程序需要一個啟動畫面。 試圖為我的啟動畫面創建一個具有圖像的活動; 並嘗試使用for循環和Timer類來引入時間延遲。 但它不會那樣工作。 我做錯了嗎? 如果有,那么正確的方法是什么?

上述解決方案很好,但如果用戶在啟動延遲結束之前按下后退鍵(並關閉應用程序),該怎么辦呢? 該應用程序可能仍會打開下一個活動,這不是真正用戶友好的。

這就是我使用自定義處理程序,並刪除onDestroy()中任何待處理消息的原因。

public class SplashActivity extends Activity
{
    private final static int MSG_CONTINUE = 1234;
    private final static long DELAY = 2000;

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

        mHandler.sendEmptyMessageDelayed(MSG_CONTINUE, DELAY);
    }

    @Override
    protected void onDestroy()
    {
        mHandler.removeMessages( MSG_CONTINUE );    
        super.onDestroy();
    }

    private void _continue()
    {
        startActivity(new Intent(this, SomeOtherActivity.class));
        finish();
    }

    private final Handler mHandler = new Handler()
    {
        public void handleMessage(android.os.Message msg)
        {
            switch(msg.what){
                case MSG_CONTINUE:
                    _continue();
                    break;
            }
        }
    };
}

試試這個

public class SplashActivity extends Activity {
    Handler handler;
    private long timeDelay = 2000; //2 seconds
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.SplashLayout);
        final Intent i = new Intent(this, Landing.class);
        handler = new Handler(); 
        handler.postDelayed(new Runnable() { 
             public void run() { 
                 startActivity(i); 
                 finish();
             } 
        }, timeDelay); 
    }      
}

你可以拖延嗎?

Thread delay = new Thread() {
    @Override
    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
        }

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                startNextActivity();
            }
        });
    }
};

試試這個,

protected int _splashTime = 15000; 

private Handler handler;
private Runnable runnable; 

private Context context;


@Override
public void onCreate(Bundle savedInstance)
{
    super.onCreate(savedInstance);
    setContentView(R.layout.splash);   

    final SplashScreen sPlashScreen = this; 

    handler = new Handler();

     runnable = new Runnable() {
         @Override
            public void run() { 
             try {
                handler.removeCallbacks(runnable);
                handler.postDelayed(runnable, _splashTime);
              }  
            finally {
                finish(); 
                //start a new activity

                //mtdCheckLicense();
                Intent main = new Intent();
                main.setClass(sPlashScreen, YourMainActivity.class);
                startActivity(main); 
                handler.removeCallbacks(runnable);

            }
        }
    }; 
    handler.postDelayed(runnable, 2000);
} 

它會飛濺一段時間並啟動主要活動。 在此代碼中,初始屏幕等待2秒,然后啟動主活動。

我為每個項目做的最簡單的方法是這樣的:

public class SplashActivity extends Activity {
    protected boolean active = true;
    protected int splashTime = 1000;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(active && (waited < splashTime)) {
                        sleep(100);
                        if(active) {
                            waited += 100;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    finish();
                    // Start your Activity here
               }
           }
       };
       splashTread.start();    
   }
 //...

使用此處描述的解決方案,您將浪費時間,因為它們會在繼續之前暫停初始化2-3秒。

我更喜歡在main_activity.xml之上添加一個Splash Screen Layout 我通過擴展Application來檢測應用程序的第一次啟動。 如果它是第一次啟動,我會在后台構建UI時顯示我的Splash-Screen ...(如果ProgressBar滯后,請使用后台線程!)

//Extend Application to save the value. You could also use getter/setter for this instead of Shared Preferences...
public class YourApplication extends Application {

    public static final String YOUR_APP_STARTUP = "APP_FIRST_START";

    @Override
    public void onCreate() {
        super.onCreate();

        //set SharedPreference value to true
        SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putBoolean(YOUR_APP_STARTUP, true);
        editor.apply();     
        ...    
     }

檢查您的MainActivity第一次啟動

public class YourMainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //hide actionbar and other menu which could overlay the splash screen
    getActionBar().hide();

    setContentView(R.layout.activity_main);

    Boolean firstStart = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(TVApplication.YOUR_APP_STARTUP, true);

    if (firstStart) {
        //First app start, show splash screen an hide it after 5000ms
        final RelativeLayout mSplashScreen = (RelativeLayout) findViewById(R.id.splash_screen);
        mSplashScreen.setVisibility(View.VISIBLE);
        mSplashScreen.setAlpha(1.0f);
        final FrameLayout mFrame = (FrameLayout) findViewById(R.id.frame_container);
        mFrame.setAlpha(0.0f);

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Animation fadeOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
                        R.anim.fade_out_animation);
                fadeOutAnimation.setDuration(500);
                fadeOutAnimation.setAnimationListener(new Animation.AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation animation) {
                        mFrame.setAlpha(1.0f);
                        getActionBar().show();
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        mSplashScreen.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                mSplashScreen.startAnimation(fadeOutAnimation);
            }
        }, 5000); //<-- time of Splash Screen shown

    } else {
        ((RelativeLayout) findViewById(R.id.splash_screen)).setVisibility(View.GONE);
        getActionBar().show();
    }

將SplashScreen插入main.xml的頂部。 我更喜歡RelativeLayout 在這個例子中,SplashScreen被放置在帶有Navitgation Drawer的布局上,我們真的很喜歡它,不是嗎?

//main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <!-- The main content view -->

        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <!-- The navigation drawer list -->

        <ListView
            android:id="@+id/slider_list"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_gravity="start"
            android:background="@color/tvtv_background"
            android:choiceMode="singleChoice"
            android:divider="@drawable/nav_bar_divider"
            android:dividerHeight="1dp"
            android:listSelector="@android:color/transparent" />
    </android.support.v4.widget.DrawerLayout>

    <RelativeLayout
        android:id="@+id/splash_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:background="@color/tvtv_white"
        android:visibility="visible" >

        <ImageView
            android:id="@+id/splash_screen_logo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:paddingLeft="50dp"
            android:paddingRight="50dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/splash_screen_text"
            style="@style/TVTextBlueContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/splash_screen_logo"
            android:layout_centerHorizontal="true"
            android:padding="10dp"
            android:text="Awesome splash shiat" />

        <ProgressBar
            android:id="@+id/splash_screen_loader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/splash_screen_text"
            android:layout_centerHorizontal="true"
            android:clickable="false"
            android:indeterminate="true" />
    </RelativeLayout>

</RelativeLayout>   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM