android啟動第一個界面時即閃屏的核心代碼(兩種方式)


閃屏,就是SplashScreen,也能夠說是啟動畫面,就是啟動的時候,閃(展示)一下,持續數秒后。自己主動關閉。


   第一種方式:

android的實現很easy,使用Handler對象的postDelayed方法就能夠實現。在這種方法里傳遞一個Runnable對象和一個延遲的時間。該方法實現了一個延遲運行的效果,延遲的時間由第2個參數指定。單位是毫秒。

第一個參數是Runnable對象,里面包括了延遲后須要運行的操作。

詳細的實現步驟為:

1.實現一個閃屏窗口。設置背景圖片等。



2.實現主窗口,當閃屏結束后會啟動該窗口。

2.在閃屏窗口里的onCreate方法重載里。處理一個延遲運行頁面跳轉的操作。方法如上面的代碼所看到的。

在這里跳轉到程序的主窗口。


這里僅僅給出核心代碼。ui非常easy就不給了

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;

public class StarActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_star);

		// 閃屏的核心代碼
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				Intent intent = new Intent(StarActivity.this,
						MainActivity.class); // 從啟動動畫ui跳轉到主ui
				startActivity(intent);
				overridePendingTransition(R.anim.in_from_right,
						R.anim.out_to_left);
				StarActivity.this.finish(); // 結束啟動動畫界面

			}
		}, 4000); // 啟動動畫持續3秒鍾
	}

}


另外一種方式:

通過AlphaAnimation 動畫。窗體的動畫效果。淡入淡出,有些游戲的歡迎動畫。logo的淡入淡出效果就使用AlphaAnimation。 

【基本的語法】public AlphaAnimation (float fromAlpha, float toAlpha)

fromAlpha:開始時刻的透明度,取值范圍0~1。
toAlpha:結束時刻的透明度,取值范圍0~1。

public class SplashActivity extends Activity
{
        private TextView tv_version;
        private LinearLayout ll;
        private ProgressDialog progressDialog;
        
        private UpdateInfo info;
        private String version;
        
        private static final String TAG = "Security";
        
        private Handler handler = new Handler()
        {
                public void handleMessage(Message msg) 
                {
                        if(isNeedUpdate(version))
                        {
                                showUpdateDialog();
                        }
                };
        };
        
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                requestWindowFeature(Window.FEATURE_NO_TITLE);
                setContentView(R.layout.splash);
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
                
                tv_version = (TextView) findViewById(R.id.tv_splash_version);
                version = getVersion();
                tv_version.setText("版本  " + version);
                
                ll = (LinearLayout) findViewById(R.id.ll_splash_main);
                AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
                alphaAnimation.setDuration(2000);
                ll.startAnimation(alphaAnimation);
                
                progressDialog = new ProgressDialog(this);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setMessage("正在下載...");
                
                new Thread()
                {
                        public void run() 
                        {
                                try
                                {
                                        sleep(3000);
                                        handler.sendEmptyMessage(0);
                                }
                                catch (InterruptedException e)
                                {
                                        e.printStackTrace();
                                }
                        };
                }.start();
                
        }
        


事實上在啟動界面會包含非常多操作的,比方,獲取應用的版本,以及提醒是否更新,等等操作。這里也僅僅給出簡單的核心代碼。一般也是非常easy理解的。


上述代碼僅僅是簡單的給出了一些操作,詳細的實現沒有給出,比方版本,以及更新操作,下載操作等。

假設沒有上述的一些操作,僅僅要注意例如以下代碼就可以。


 ll = (LinearLayout) findViewById(R.id.ll_splash_main);
     AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
     alphaAnimation.setDuration(2000);
     ll.startAnimation(alphaAnimation);




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM