編輯:關於 Android 程式設計
本節介紹 ViewFlipper,它是 Android 自帶的多頁面管理控件,還能自動播放。和 ViewPager 一次顯示一頁不同,ViewFlipper 是一層層堆疊的,常用於製作引導頁或圖片輪播。下面就用它做一個簡單的圖片輪播範例。官方 API:ViewFlipper。
直接在 XML 中把各個子頁寫進 ViewFlipper 標籤內。

透過 addView() 方法在程式碼中填充 View。

setInAnimation:設定 View 進入螢幕時使用的動畫
setOutAnimation:設定 View 退出螢幕時使用的動畫
showNext:顯示下一個 View
showPrevious:顯示上一個 View
setFlipInterval:設定 View 之間切換的時間間隔
startFlipping:以上面設定的間隔開始循環切換
stopFlipping:停止切換
每頁都是一個簡單的 ImageView。先準備進出動畫 right_in.xml 與 right_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
</set>
接著在 activity_main.xml 中使用 ViewFlipper,透過 include 靜態引入各頁,並設定 flipInterval 為 3000 毫秒:
<ViewFlipper xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/vflp_help"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:flipInterval="3000"
android:inAnimation="@anim/right_in"
android:outAnimation="@anim/right_out">
<include layout="@layout/page_help_one" />
<include layout="@layout/page_help_two" />
<include layout="@layout/page_help_three" />
<include layout="@layout/page_help_four" />
</ViewFlipper>
最後在 MainActivity 中呼叫 startFlipping() 即可開始自動輪播:
public class MainActivity extends AppCompatActivity {
private ViewFlipper vflp_help;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vflp_help = (ViewFlipper) findViewById(R.id.vflp_help);
vflp_help.startFlipping();
}
}

除了左右進出動畫外,我們再用 GestureDetector 來偵測滑動手勢,切換頁面。
public class MainActivity extends AppCompatActivity {
private Context mContext;
private ViewFlipper vflp_help;
private int[] resId = { R.mipmap.ic_help_view_1, R.mipmap.ic_help_view_2,
R.mipmap.ic_help_view_3, R.mipmap.ic_help_view_4 };
private final static int MIN_MOVE = 200; // 判斷手勢的最小距離
private GestureDetector mDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
mDetector = new GestureDetector(this, new MyGestureListener());
vflp_help = (ViewFlipper) findViewById(R.id.vflp_help);
// 動態加入子 View
for (int i = 0; i < resId.length; i++) {
vflp_help.addView(getImageView(resId[i]));
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return mDetector.onTouchEvent(event);
}
private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float v, float v1) {
if (e1.getX() - e2.getX() > MIN_MOVE) { // 左滑 → 下一頁
vflp_help.setInAnimation(mContext, R.anim.right_in);
vflp_help.setOutAnimation(mContext, R.anim.right_out);
vflp_help.showNext();
} else if (e2.getX() - e1.getX() > MIN_MOVE) { // 右滑 → 上一頁
vflp_help.setInAnimation(mContext, R.anim.left_in);
vflp_help.setOutAnimation(mContext, R.anim.left_out);
vflp_help.showPrevious();
}
return true;
}
}
private ImageView getImageView(int resId) {
ImageView img = new ImageView(this);
img.setBackgroundResource(resId);
return img;
}
}

要點提醒:這裡沒有直接用 onTouchEvent 判斷 ACTION_MOVE 來切頁,是因為真机上手指會持續顫抖,導致 ACTION_MOVE 不斷觸發、頁面瘋狂切換。改用 GestureDetector 的 onFling 來判斷「快速滑動」的手勢,切換就流暢自然多了。


Android 4.x 中「變相」攔截 HOME 鍵的一種思路
項目中遇到了需要屏蔽 HOME 鍵的需求。項目本身的要求是:按下 HOME 鍵後程序不做任何響應,就像按下返回鍵那樣,在 onBackPressed 方法裡直接 retu
移動一卡多號怎麼開通?資費、辦理管道與 App 用法
中國移動的一卡多號服務,可以在不換機、不換卡的情況下,為現有號碼增加 1 到 3 個副號。副號是標準格式的手機號碼,適合需要區分工作與生活、或想保護隱私的使用者。 裝
Android Spinner 用法解析
Spinner 基本概念 Spinner 是下拉式清單控制項,常用來讓使用者從一組選項中做選擇。 版面配置 <Spinner android:id=@+id/Spi
Android 6.0 指紋識別開發筆記:Enroll 流程與關鍵參數
Android M 時期指紋相關的資料還很少,讀了一段時間原生碼之後,把實際跑通的範例整理出來給需要的朋友參考。 以下內容基於 64 位高通 CPU、搭配 FPC1020