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

Android 自定義 PopupWindow 技巧:定位、動畫與尺寸一次搞定

編輯:關於 Android 開發

前言:兩個最頭疼的老大難

自定義 PopupWindow 本身並不難,真正讓人頭疼的其實是兩件事:顯示的定位問題和顯示/消失的動畫問題。

定位之所以麻煩,是因為它常常又牽扯到 PopupWindow 的寬高——我們都知道,在 show 之前根本拿不到寬高,可 show 的時候定位又偏偏需要寬高,這就形成了一個矛盾。本文會把這些問題一次性講清楚,並提供三個實用的定位方法:顯示在控件的下左、下中、下右位置。

如何提前拿到 PopupWindow 的寬高

思路很巧妙:在初始化的時候強制繪制 Layout,就能提前拿到寬高。

 // 用於保存PopupWindow的寬度 private int width; // 用於保存PopupWindow的高度 private int height; public CustomPopupWindow(Activity activity) { super(activity); this.activity = activity; this.initPopupWindow(); } private void initPopupWindow() { LayoutInflater inflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.contentView = inflater.inflate(R.layout.popupwindow_custom, null); this.setContentView(contentView); this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); this.setTouchable(true); this.setFocusable(true); this.setOutsideTouchable(true); this.setAnimationStyle(R.style.PopupAnimation); ColorDrawable background = new ColorDrawable(0x4f000000); this.setBackgroundDrawable(background); this.mandatoryDraw(); } /** * 強制繪制popupWindowView,並且初始化popupWindowView的尺寸 */ private void mandatoryDraw() { this.contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); /** * 強制刷新後拿到PopupWindow的寬高 */ this.width = this.contentView.getMeasuredWidth(); this.height = this.contentView.getMeasuredHeight(); }

定位在下左位置

 /** * 顯示在控件的下左方 * * @param parent parent */ public void showAtDropDownLeft(View parent) { if (parent.getVisibility() == View.GONE) { this.showAtLocation(parent, 0, 0, 0); } else { int[] location = new int[2]; //獲取在整個屏幕內的絕對坐標 parent.getLocationOnScreen(location); this.showAtLocation(parent, 0, location[0], location[1] + parent.getHeight()); } }

定位在下中位置

 /** * 顯示在控件的下中方 * * @param parent parent */ public void showAtDropDownCenter(View parent) { if (parent.getVisibility() == View.GONE) { this.showAtLocation(parent, 0, 0, 0); } else { int[] location = new int[2]; parent.getLocationOnScreen(location); this.showAtLocation(parent, 0, location[0] / 2 + parent.getWidth() / 2 - this.width / 6, location[1] + parent.getHeight()); } }

定位在下右位置

 /** * 顯示在控件的下右方 * * @param parent parent */ public void showAtDropDownRight(View parent) { if (parent.getVisibility() == View.GONE) { this.showAtLocation(parent, 0, 0, 0); } else { int[] location = new int[2]; parent.getLocationOnScreen(location); this.showAtLocation(parent, 0, location[0] + parent.getWidth() - this.width, location[1] + parent.getHeight()); } }

PopupWindow 動畫

這裡設計一個效果:PopWindow 出現時從右上角逐步展開,消失時再向右上角逐步收起。

styles.xml

 

popwindow_in.xml(以右上角為中心,縮放 0→1 並伴隨透明度漸顯):

 <set xmlns:android="https://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator"> <scale android:duration="200" android:fromxscale="0.0" android:fromyscale="0.0" android:pivotx="100%" android:pivoty="0%" android:toxscale="1.0" android:toyscale="1.0"> <alpha android:duration="200" android:fromalpha="0.0" android:toalpha="1.0"> </alpha></scale></set>

popwindow_out.xml(反向操作,縮放 1→0 並伴隨透明度漸隱):

 <set xmlns:android="https://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator"> <scale android:duration="200" android:fromxscale="1.0" android:fromyscale="1.0" android:pivotx="100%" android:pivoty="0%" android:toxscale="0.0" android:toyscale="0.0"> <alpha android:duration="200" android:fromalpha="1.0" android:toalpha="0.0"> </alpha></scale></set>

完整的自定義 PopupWindow

 public class CustomPopupWindow extends android.widget.PopupWindow { private Activity activity; private View contentView; private int width; private int height; public CustomPopupWindow(Activity activity) { super(activity); this.activity = activity; this.initPopupWindow(); } private void initPopupWindow() { LayoutInflater inflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.contentView = inflater.inflate(R.layout.popupwindow_custom, null); this.setContentView(contentView); this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); this.setTouchable(true); this.setFocusable(true); this.setOutsideTouchable(true); this.setAnimationStyle(R.style.PopupAnimation); ColorDrawable background = new ColorDrawable(0x4f000000); this.setBackgroundDrawable(background); this.mandatoryDraw(); } private void mandatoryDraw() { this.contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); this.width = this.contentView.getMeasuredWidth(); this.height = this.contentView.getMeasuredHeight(); } public void showAtDropDownRight(View parent) { if (parent.getVisibility() == View.GONE) { this.showAtLocation(parent, 0, 0, 0); } else { int[] location = new int[2]; parent.getLocationOnScreen(location); this.showAtLocation(parent, 0, location[0] + parent.getWidth() - this.width, location[1] + parent.getHeight()); } } public void showAtDropDownLeft(View parent) { if (parent.getVisibility() == View.GONE) { this.showAtLocation(parent, 0, 0, 0); } else { int[] location = new int[2]; parent.getLocationOnScreen(location); this.showAtLocation(parent, 0, location[0], location[1] + parent.getHeight()); } } public void showAtDropDownCenter(View parent) { if (parent.getVisibility() == View.GONE) { this.showAtLocation(parent, 0, 0, 0); } else { int[] location = new int[2]; parent.getLocationOnScreen(location); this.showAtLocation(parent, 0, location[0] / 2 + parent.getWidth() / 2 - this.width / 6, location[1] + parent.getHeight()); } } public static class PopupWindowBuilder { private static String activityHashCode; private static CustomPopupWindow popupWindow; public static PopupWindowBuilder ourInstance; public static PopupWindowBuilder getInstance(Activity activity) { if (ourInstance == null) ourInstance = new PopupWindowBuilder(); String hashCode = String.valueOf(activity.hashCode()); if (!hashCode.equals(String.valueOf(activityHashCode))) { activityHashCode = hashCode; popupWindow = new CustomPopupWindow(activity); } return ourInstance; } public PopupWindowBuilder setTouchable(boolean touchable) { popupWindow.setTouchable(touchable); return this; } public PopupWindowBuilder setAnimationStyle(int animationStyle) { popupWindow.setAnimationStyle(animationStyle); return this; } public PopupWindowBuilder setBackgroundDrawable(Drawable background) { popupWindow.setBackgroundDrawable(background); return this; } public CustomPopupWindow getPopupWindow() { popupWindow.update(); return popupWindow; } } }

效果圖

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