編輯:關於 Android 程式設計
PopupWindow 是 Android 開發中非常實用的一個浮動視圖元件,適合用來製作彈出菜單、提示框或臨時資訊面板。本文整理了自己在使用 PopupWindow 時的完整實作思路,從 XML 佈局、代碼初始化,到進出場動畫的設定,一步步帶你走過整個流程。
第一步,我們需要為 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>
佈局檔準備好之後,接著在 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 檔案中把動畫綁定成一個樣式,讓 PopupWindow 可以引用:
<style name="AnimationPreview">
<item name="android:windowEnterAnimation">@anim/zoomin</item>
<item name="android:windowExitAnimation">@anim/zoomout</item>
</style>
最後一步,透過一個旗標變數來判斷目前 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 從佈局、初始化到動畫的完整實作心得。掌握這幾個環節,就能輕鬆做出流暢自然的彈出效果。
詳解 Android MVP 開發模式
本文主要講解 MVP 開發模式,並通過一個用戶登錄的具體實例演示其落地方式。 一、簡介 MVP(Model View Presenter)是經典 MVC(Model Vi
打造高質感下拉刷新動畫:Android 自訂 RefreshView 實作
近來看到不少精緻又有質感的動畫效果,若能把這些動畫融入自己的專案,App 的整體觀感勢必加分不少。今天要帶大家拆解一個來自 Yalantis 的精美下拉刷新動畫。 先欣賞
從源碼角度剖析 Android Handler 消息機制
在 Android 中做耗時操作後更新 UI,用得最多的就是 Handler:子線程負責網絡訪問、文件讀寫等重活,然後把消息發送回主線程,由主線程完成界面刷新。 用法大家
基於 Android 的 3D 視頻播放示例代碼解析
作者:Mark Liu 簡介 本文介紹一個名為 Video3D 的示例應用,它基於 Android 4.0 Ice Cream Sandwich(ICS)的 Media