編輯:關於 Android 程式設計
閃屏是幾乎每個 App 的標配。本文把常見的三種閃屏實作方式一次整理出來,供大家參考。
一、專案目錄結構

二、activity_main.xml 程式碼
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.zgs.SplashScreenByXml.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="主頁面" /> </RelativeLayout>
三、activity_splashscreen.xml 程式碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/splash" android:orientation="vertical" android:id="@+id/ll_splashActivity"> <TextView android:id="@+id/tv_countDown" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:layout_marginEnd="20dp" android:layout_marginTop="20dp" android:textColor="@android:color/white" android:textSize="20sp" /> </LinearLayout>
四、SplashScreenActiviy.java 程式碼
package com.zgs.SplashScreenByXml; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.TranslateAnimation; import android.widget.LinearLayout; import android.widget.TextView; import com.zgs.CommonlySplashScreen.R; public class SplashScreenActiviy extends Activity { private TextView tv_countDown; private LinearLayout ll_splashActivity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 通過下面兩行代碼也可實現全屏無標題欄顯示activity // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_splashscreen); tv_countDown = (TextView) findViewById(R.id.tv_countDown); ll_splashActivity = (LinearLayout) findViewById(R.id.ll_splashActivity); /******************************************************************************** * * 普通閃屏實現方式 * * ******************************************************************************/ /*new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(getApplicationContext(), MainActivity.class); startActivity(intent); finish(); } }, 1000*4);*/ /******************************************************************************** * * 倒計時閃屏實現方式 * * ******************************************************************************/ /*MyCountDownTimer mc = new MyCountDownTimer(4000, 1000); mc.start(); new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(getApplicationContext(), MainActivity.class); startActivity(intent); finish(); } }, 1000*4);*/ /******************************************************************************** * * 倒計時+動畫閃屏實現方式 * * ******************************************************************************/ MyCountDownTimer mc = new MyCountDownTimer(4000, 1000); mc.start(); new Handler().postDelayed(new Runnable() { @Override public void run() { //左移動畫 TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, -1, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0); ta.setDuration(2000); //設置動畫執行的時間 ta.setFillAfter(true);//當動畫結束後 動畫停留在結束位置,然後等啟動主界面後將其銷毀 ll_splashActivity.startAnimation(ta); ta.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation arg0) { } @Override public void onAnimationRepeat(Animation arg0) { } @Override public void onAnimationEnd(Animation arg0) { Intent intent = new Intent(getApplicationContext(), MainActivity.class); startActivity(intent); finish(); } }); } }, 1000*4); } class MyCountDownTimer extends CountDownTimer { //millisInFuture:倒計時的總數,單位毫秒 //例如 millisInFuture=1000;表示1秒 //countDownInterval:表示間隔多少毫秒,調用一次onTick方法() //例如: countDownInterval =1000;表示每1000毫秒調用一次onTick() public MyCountDownTimer(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } public void onFinish() { tv_countDown.setText("開始跳轉……"); } public void onTick(long millisUntilFinished) { tv_countDown.setText("倒計時(" + millisUntilFinished / 1000 + ")"); } } }
五、MainActivity.java 程式碼
package com.zgs.SplashScreenByXml; import android.app.Activity; import android.os.Bundle; import com.zgs.CommonlySplashScreen.R; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //為了讓閃屏結束後更自然的過度到主界面,去除主界面的啟動動畫,將下面函數的第一個參數設為0即可 overridePendingTransition(0, 0); } }
六、操作演示

以上就是本文介紹的三種閃屏實作方式(普通閃屏、倒計時閃屏、倒計時+動畫閃屏),希望對大家有所幫助。若有疑問歡迎留言,我會及時回覆,也感謝大家對本站的支持!
Android 自定義載入動畫:打造專屬的旋轉進度樣式
系統內建的載入轉圈樣子實在乏善可陳,想換一張自己的圖片其實不難。先來看最終效果: 第一步:寫一個可重複使用的載入版面 這個載入畫面專案裡到處都會用到,所以值得抽成獨
Android SQLite 實作:省 / 市 / 區三級連動的動態 Spinner
三級連動是中文選址表單的經典需求:選了省份要更新城市清單、選了城市要更新區縣清單。用 SQLite 存這份靜態資料,比打包成 JSON 或寫死在程式碼裡都更合適——查詢用
Android TabHost 標籤頁的四種實作方式
Tab 標籤是 Android 早期常見的 UI 元件,核心類別是 TabHost。本文用四個由淺入深的例子把它的用法完整走一遍,最後再談談為什麼現在已經不建議使用它。
為什麼Android開發者都愛用Genymotion?速度與真機體驗兼得的模擬器
如果你還在用 Android 原生的 AVD 調試程式,那種「點一下、等三秒」的煎熬大概不必多說。Genymotion 在開發者圈子裡口碑極佳,理由其實非常直白:跑得快、