編輯:關於 Android 開發
閃屏頁的實現其實很簡單,核心就是「延遲一會兒再跳轉」。這裡記錄兩種常見寫法:一種是純延遲,另一種帶可見的倒計時。
postDelayed() 接受兩個參數:一個 Runnable,裡面放延遲後要執行的操作;一個延遲時間,單位是毫秒。順手給 logo 加一個放大動畫,觀感會好不少。
package com.msn.zn.splashdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class SplashActivity extends Activity {
private ImageView iv_logo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 取消標題
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// 取消狀態欄
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
iv_logo = (ImageView) findViewById(R.id.logo);
// 加載 xml 文件中定義的動畫
Animation anim = AnimationUtils.loadAnimation(this, R.anim.splash);
iv_logo.startAnimation(anim);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}, 3000);
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.msn.zn.splashdemo.SplashActivity">
<ImageView
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@mipmap/splasht2"/>
</RelativeLayout>
動畫定義:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
android:duration="1500"
android:fillAfter="true">
<scale
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.1"
android:toYScale="1.1"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.8"/>
</set>
兩個屬性的作用:duration="1500" 把動畫時長設為 1.5 秒;fillAfter="true" 讓動畫結束後保持在最終狀態,不會跳回起始位置。這兩項在代碼裡也能設置:animation.setDuration(1500)、animation.setFillAfter(true)。
如果需要在界面上顯示「3 秒後跳轉」這類倒數,用系統封裝好的 CountDownTimer 最省事。它本質上是把後台線程的創建和 Handler 隊列封裝成了一個好用的類。


package com.msn.zn.splashdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.widget.TextView;
public class SplashTimeActivity extends Activity {
private TextView tv_time;
private MyCountDownTimer mc;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_time);
tv_time = (TextView) findViewById(R.id.tv_time);
mc = new MyCountDownTimer(5000, 1000);
mc.start(); // 啟動倒計時
handler.postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashTimeActivity.this, MainActivity.class));
}
}, 5000);
}
/**
* 定義一個倒計時的內部類
*/
class MyCountDownTimer extends CountDownTimer {
/**
* @param millisInFuture 從調用 start() 到倒計時完成、onFinish() 被調用的毫秒數
* @param countDownInterval 接收 onTick(long) 回調的間隔時間
*/
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
tv_time.setText("倒計時 " + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
tv_time.setText("正在跳轉");
}
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<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:background="@mipmap/splasht2"
tools:context="com.msn.zn.splashdemo.SplashTimeActivity">
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="20sp"
android:text="閃一下" />
</RelativeLayout>
純展示、不需要跟用戶交互的閃屏頁,做法一就够了。若還要提供「跳過」按鈕,倒計時就更合適——把跳轉邏輯收進 onFinish(),跳過按鈕則調用 mc.cancel() 後手動跳轉,兩條路徑不會打架。
另外提醒一個實務細節:Activity 銷毀時記得在 onDestroy() 裡調用 handler.removeCallbacksAndMessages(null) 並取消倒計時,否則用戶在 3 秒內按返回鍵退出,延遲任務仍會執行,可能把不該啟動的 Activity 拉起來。
Android 學習筆記(十三):ToggleButton 開關控件的使用
Android 學習筆記(十三):ToggleButton 開關控件的使用 ToggleButton 顧名思義是一個「開關」控件,具有兩種不同狀態(開與關)。它有三個主要
《Android源碼設計模式解析與實戰》讀書筆記(十九):組合模式
讀書筆記(十九):組合模式 第十九章、組合模式 組合模式又稱「部分—整體」模式,是結構型設計模式中的一員,擅長處理具有層次結構的對象集合。 1. 定義 組合模式將對象組合
為 RecyclerView 加上頭部與底部視圖:XCRecyclerView 開源實作解析
讓 RecyclerView 也能輕鬆加入 Header 與 Footer 熟悉 ListView 的朋友都知道,它自帶了 addHeaderView 與 addFoot
深入理解 RCU 實現機制:以 2.6.21 核心為例的原始碼剖析
深入理解 RCU 實現 ——基於 2.6.21 核心的 RCU 實現(lvyilong316) RCU(Read-Copy Update),顧名思義就是讀—拷貝—修改,名