Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

PopupWindow 實戰心得:從佈局到進出動畫的完整流程

編輯:關於 Android 程式設計

PopupWindow 是 Android 開發中非常實用的一個浮動視圖元件,適合用來製作彈出菜單、提示框或臨時資訊面板。本文整理了自己在使用 PopupWindow 時的完整實作思路,從 XML 佈局、代碼初始化,到進出場動畫的設定,一步步帶你走過整個流程。

一、先準備 PopupWindow 的佈局檔

第一步,我們需要為 PopupWindow 撰寫一個對應的 XML 佈局檔案,用來定義彈出視圖的內容與樣式。以下是一個包含標題、副標題與內容文字的範例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/margin_max"
        android:layout_marginRight="@dimen/margin_max"
        android:background="@color/white"
        android:orientation="vertical">
        <!-- 主內容 -->
        <TextView
            android:id="@+id/diet_pop_tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/margin_max"
            android:text="@string/activity_aerobic"
            android:textColor="@color/string_bgwhite_main"
            android:textSize="@dimen/text_15" />
        <!-- 主內容副標 -->
        <TextView
            android:id="@+id/diet_pop_tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/recipe_choose_exam"
            android:layout_marginLeft="@dimen/margin_max"
            android:textColor="@color/string_bgwhite_sub"
            android:textSize="@dimen/text_15" />
        <!-- 第二標題 -->
        <TextView
            android:id="@+id/diet_pop_tv3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/diet_pop_title"
            android:paddingLeft="@dimen/margin_max"
            android:layout_marginLeft="@dimen/margin_max"
            android:text="@string/activity_aerobic_content"
            android:textColor="@color/white"
            android:textSize="@dimen/text_15" />
        <!-- 第二內容 -->
        <TextView
            android:id="@+id/diet_pop_tv4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/margin_max"
            android:text="@string/activity_aerobic_suggest"
            android:textColor="@color/string_bgwhite_main"
            android:textSize="@dimen/text_15" />
        <ImageView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitStart"
            android:background="@drawable/popupshadow" />
    </LinearLayout>
</LinearLayout>

二、在代碼中載入並初始化 PopupWindow

佈局檔準備好之後,接著在 Activity 中把它載入並建立 PopupWindow 物件。這裡要注意焦點、外部觸控與動畫樣式等屬性的設定:

// 將上面的 XML 佈局作為 PopupWindow 的視圖
View inflateView = getLayoutInflater().inflate(R.layout.recipe_popwindow, null);
mPopupWindow = new PopupWindow(inflateView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
mPopupWindow.setFocusable(false);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setAnimationStyle(R.style.AnimationPreview); // 設定進出動畫

三、設定進場與退場動畫

接下來在 anim 資料夾下建立兩組動畫檔。進場動畫模擬由右下角放大浮現的效果:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator">
    <scale android:fromXScale=".1" android:toXScale="1.0"
        android:fromYScale=".1" android:toYScale="1.0"
        android:pivotX="100%p" android:pivotY="100%p"
        android:duration="500"/>
    <alpha android:fromAlpha="0" android:toAlpha="1.0"
        android:duration="@android:integer/config_mediumAnimTime"/>
</set>

退場動畫則是向右下角縮小並逐漸變得透明:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:zAdjustment="top">
    <scale android:fromXScale="1.0" android:toXScale=".5"
        android:fromYScale="1.0" android:toYScale=".5"
        android:pivotX="100%p" android:pivotY="100%p"
        android:duration="@android:integer/config_mediumAnimTime" />
    <alpha android:fromAlpha="1.0" android:toAlpha="0"
        android:duration="@android:integer/config_mediumAnimTime"/>
</set>

四、在 styles 中註冊動畫樣式

接著在 styles 檔案中把動畫綁定成一個樣式,讓 PopupWindow 可以引用:

<style name="AnimationPreview">
    <item name="android:windowEnterAnimation">@anim/zoomin</item>
    <item name="android:windowExitAnimation">@anim/zoomout</item>
</style>

五、控制 PopupWindow 的顯示與隱藏

最後一步,透過一個旗標變數來判斷目前 PopupWindow 是顯示還是隱藏狀態,實現點擊切換的效果:

public void openMenu(View v) {
    if (!flag) { // flag 初始化為 false
        // 設定顯示位置
        // mPopupWindow.showAtLocation(view, Gravity.LEFT, 0, 0);
        mPopupWindow.showAsDropDown(v, 0, 0);
        flag = true;
    } else {
        mPopupWindow.dismiss();
        flag = false;
    }
}

以上就是 PopupWindow 從佈局、初始化到動畫的完整實作心得。掌握這幾個環節,就能輕鬆做出流暢自然的彈出效果。

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