編輯:進階開發
我們先前介紹過「在 Android 開發中使用 Gallery 實作多級聯動」與「在 Android 中讓 Service 動態更新 UI」。這次要示範的是首頁左右拖動的效果。
實作起來並不複雜:用 ViewFlipper 把要來回切換的 View 裝在一起,再讓它與 GestureDetector 聯手判斷該顯示哪一頁,最後加上一點位移動畫即可。本例的規則是手指向左快滑切到下一頁、向右快滑切到上一頁,每頁放一張圖片。
Android ViewFlipper 效果
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<ViewFlipper android:id="@+id/flipper"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<include android:id="@+id/firstlayout" layout="@layout/first" />
<include android:id="@+id/secondlayout" layout="@layout/second" />
<include android:id="@+id/thirdlayout" layout="@layout/third" />
<include android:id="@+id/fourthlayout" layout="@layout/fourth" />
</ViewFlipper>
</LinearLayout>
ViewFlipper 裡放了四個 layout,同一時間只會顯示其中一個,滑動時在這些 layout 之間移動。
每個 layout 都很簡單,就一張圖片:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/v1" />
</LinearLayout>
Activity 需要實作兩個介面:OnGestureListener 與 OnTouchListener。完整程式碼如下,註解已標明各段用途。
package com.ideasandroid.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ViewFlipper;
public class ViewFlipperDemo extends Activity implements OnGestureListener, OnTouchListener {
private ViewFlipper mFlipper;
GestureDetector mGestureDetector;
private int mCurrentLayoutState;
private static final int FLING_MIN_DISTANCE = 100;
private static final int FLING_MIN_VELOCITY = 200;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mFlipper = (ViewFlipper) findViewById(R.id.flipper);
// 註冊一個負責手勢識別的物件
mGestureDetector = new GestureDetector(this);
// 替 mFlipper 掛上 touch 監聽器
mFlipper.setOnTouchListener(this);
mCurrentLayoutState = 0;
// 允許長按 ViewFlipper,這樣才識別得到拖動等手勢
mFlipper.setLongClickable(true);
}
/**
* 此方法在本例中未使用,可用來直接跳到指定頁面
*/
public void switchLayoutStateTo(int switchTo) {
while (mCurrentLayoutState != switchTo) {
if (mCurrentLayoutState > switchTo) {
mCurrentLayoutState--;
mFlipper.setInAnimation(inFromLeftAnimation());
mFlipper.setOutAnimation(outToRightAnimation());
mFlipper.showPrevious();
} else {
mCurrentLayoutState++;
mFlipper.setInAnimation(inFromRightAnimation());
mFlipper.setOutAnimation(outToLeftAnimation());
mFlipper.showNext();
}
}
}
/** 由右側進入的動畫 */
protected Animation inFromRightAnimation() {
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(500);
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
}
/** 向左側退出的動畫 */
protected Animation outToLeftAnimation() {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration(500);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
/** 由左側進入的動畫 */
protected Animation inFromLeftAnimation() {
Animation inFromLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromLeft.setDuration(500);
inFromLeft.setInterpolator(new AccelerateInterpolator());
return inFromLeft;
}
/** 向右側退出的動畫 */
protected Animation outToRightAnimation() {
Animation outtoRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoRight.setDuration(500);
outtoRight.setInterpolator(new AccelerateInterpolator());
return outtoRight;
}
public boolean onDown(MotionEvent e) {
return false;
}
/*
* 使用者按下螢幕、快速移動後放開,即觸發這個事件
* e1:第一個 ACTION_DOWN 事件
* e2:最後一個 ACTION_MOVE 事件
* velocityX:X 軸移動速度,像素/秒
* velocityY:Y 軸移動速度,像素/秒
* 觸發條件:X 軸位移大於 FLING_MIN_DISTANCE,且速度大於 FLING_MIN_VELOCITY
*/
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {
// 向左滑動
mFlipper.setInAnimation(inFromRightAnimation()); // 進入時的動畫
mFlipper.setOutAnimation(outToLeftAnimation()); // 退出時的動畫
mFlipper.showNext();
} else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {
// 向右滑動
mFlipper.setInAnimation(inFromLeftAnimation());
mFlipper.setOutAnimation(outToRightAnimation());
mFlipper.showPrevious();
}
return false;
}
public void onLongPress(MotionEvent e) { }
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
public void onShowPress(MotionEvent e) { }
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
public boolean onTouch(View v, MotionEvent event) {
// 一定要把觸屏事件交給手勢識別物件處理,自己算會很麻煩
return mGestureDetector.onTouchEvent(event);
}
}
幾個實作細節值得留意:mFlipper.setLongClickable(true) 這行不能省,否則 GestureDetector 收不到完整的移動軌跡;FLING_MIN_DISTANCE 與 FLING_MIN_VELOCITY 這兩個門檻要依實際手感調整,門檻太低會誤觸,太高則要滑得很用力;而動畫之所以用 TranslateAnimation 搭配 AccelerateInterpolator,是為了讓切頁有「被甩出去」的速度感,比線性位移自然得多。
Android Widget 設計指南:尺寸、框架與陰影規範
Widget 是 Android 1.5 版引入的特性之一,讓使用者不必打開應用,就能在主螢幕上直接看到程式的重要資訊。標準 Android 系統已內建幾個範例,例如模擬
Android 建立 sdcard 模擬記憶卡完整圖解
Android 的應用方式相當靈活,在模擬器中做適當設定就能滿足許多特定的功能需求,例如需要外部儲存空間的相機、錄音或檔案讀寫測試。本文說明如何建立並啟用 sdcard
值得投入嗎?Android 的 Google 束縛與 Gmail 紅利
Android 大概是最能代表 Google 開發速度的產品。在與微軟、諾基亞等巨頭的拉鋸中,它展現出相當的競爭韌性。 如果你只是單純想換機嚐鮮,而新機恰好搭載 Andr
Eclipse 搭配 ADT 開發 Android 的常見問題與解法
近來用 Eclipse 搭配 ADT 開發 Android,感覺 bug 還真不少,問題甚至比 Symbian 更多,反倒讓人體會到微軟在開發者體驗上的用心。Eclips