Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 開發:用 ViewFlipper 打造觸控滑動動畫

編輯:Android 開發實例

背景說明:

在正式踏入 Android 專案開發之前,有三件事得先搞定:第一,下載 Android SDK;第二,準備好程式編輯器(我個人推薦 Eclipse);第三,安裝 Android 的 Eclipse 外掛。環境就緒後,就可以開始這篇教學了。

初次開啟 Android SDK 時,你需要先建立一個虛擬裝置。建議以 Android 1.6(API Level 4)這類較通用的版本為目標,接著再依需求加入硬體功能,例如 SD 卡支援、加速計、相機支援等等。若需要相關協助,可參考 Android 官方關於虛擬裝置安裝的說明。

建立 Eclipse 專案:

1、選擇 File > New > Project。

2、選擇 Android > Android Project,點擊 Next。

3、選擇專案目錄:

A、輸入專案名稱,也就是這個專案所在資料夾的名稱;

B、選擇 Create new project in workspace,並指定 workspace 的位置;

C、依 Target 選擇 Android 版本,作為專案建立時的目標平台,也就是你想開發的應用所對應的 Android 版本。

說明:除非你確定要使用最新 SDK 中新增的 API,否則建議盡量挑選較低的平台版本,例如 Android 1.6。

D、依屬性欄位填寫必要資訊:

・輸入應用程式名稱;

・輸入 package 名稱,日後所有原始碼都會放在這個 package 中;

・勾選 Create Activity,並為主要 Activity 命名;

・輸入 Min SDK Version,這是一個整數,代表能正確運行應用所需的最低 API 級別,它會自動設定 Android Manifest 中的 minSdkVersion 屬性。若不確定該填哪個級別,可直接沿用 Target 分頁中所選 Build Target 列出的 API Level。

4、點擊 Finish。

完成後,ADT 會為你建立以下資料夾與檔案:

src/:存放各 Activity 的 Java 檔。

:存放編譯應用時產生的 android.jar。

gen/:存放 ADT 自動產生的 Java 檔,包含 R.java 以及由 AIDL 檔建立的介面。

assets/:預設為空,可用來存放原始資源檔。

res/:應用程式的資源檔,如 drawable、layout、string 等。

AndroidManifest.xml:專案的 Android 描述檔。

default.properties:記錄專案設定(如 build target)等資訊,屬於專案的一部分,需納入版本控制,切勿手動編輯(要改設定請右鍵點擊專案資料夾並選擇「屬性」)。

透過觸控事件滑動切換多個布局

類似新聞、天氣那樣滑動切換畫面的效果,是很多應用的經典互動。接下來我們就來動手實作,只需一點 Java 程式碼與 XML 布局技巧即可達成。

第一步,在布局檔中加入 ViewFlipper,並放入兩個 LinearLayout 與各自的 TextView:

<ViewFlipper
android:layout_margin="6dip"
android:id="@+id/layoutswitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_height="wrap_content"
android:id="@+id/firstpanel"
android:paddingTop="10dip"
android:text="my first panel"
android:layout_width="fill_parent"
android:layout_weight="1"
android:text >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_height="wrap_content"
android:id="@+id/secondpanel"
android:paddingTop="10dip"
android:text="my second panel"
android:layout_width="fill_parent"
android:layout_weight="1"
android:text >
</TextView>
</LinearLayout>
</ViewFlipper>

如你所見,我加入了一個 ViewFlipper、兩個 LinearLayout,並在每個內部布局各放一個 TextView。先把切換布局的骨架搭好是關鍵的第一步,之後你可以自由客製兩個內部布局的風格,也能加上工具列來切換。

第二步,撰寫 Java 程式碼(透過觸控切換布局)。從 src/ 開啟主類別,在類別開頭宣告兩個變數:

private ViewFlipper vf;
private float oldTouchValue;

第一個變數用來控制 ViewFlipper,第二個則用於記錄觸控事件。

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
vf = (ViewFlipper) findViewById(R.id.switchlayout);
}

第三步,處理 touch 事件。觸控螢幕是手機最重要的特色之一,就像 iPhone 或其他裝置那樣,使用者習慣用手指滑動來切換畫面。

在 Android 中掛上觸控事件有兩種做法:一是實作 TouchListener,二是覆寫 onTouchEvent。本文採用第二種。

@Override
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction())
{
case MotionEvent.ACTION_DOWN:
{
oldTouchValue = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP:
{
if(this.searchOk==false) return false;
float currentX = touchevent.getX();
if (oldTouchValue < currentX)
{
vf.setInAnimation(AnimationHelper.inFromLeftAnimation());
vf.setOutAnimation(AnimationHelper.outToRightAnimation());
vf.showNext();
}
if (oldTouchValue > currentX)
{
vf.setInAnimation(AnimationHelper.inFromRightAnimation());
vf.setOutAnimation(AnimationHelper.outToLeftAnimation());
vf.showPrevious();
}
break;
}
}
return false;
}

從程式碼可以看出,當使用者第一次觸碰螢幕時,裝置會把手指的 X 座標存入 oldTouchValue;等到使用者放開手指時,再比對最新位置,判斷要切到下一頁還是上一頁(比較 oldTouchValue 與 currentX)。切換布局的邏輯相同,但依方向不同會套用不同動畫。以下是 AnimationHelper 的範例程式碼:

//for the previous movement
public static 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(350);
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
}
public static 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(350);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
// for the next movement
public static 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(350);
inFromLeft.setInterpolator(new AccelerateInterpolator());
return inFromLeft;
}
public static 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(350);
outtoRight.setInterpolator(new AccelerateInterpolator());
return outtoRight;
}

你可以把這些方法放進一個靜態類別,或直接在類別中宣告。

結論:

到這裡,你已經有了基礎類別與由右至左(showPrevious)、由左至右(showNext)的觸控滑動事件,搭配四段動畫與一個簡單布局就能跑起來。當布局超過兩個、內容更複雜時,建議再進一步測試與調整。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved