78f22e45ec515a68d715c99d5b2f288558d328e0.svn-base 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package eVVM.apk.helper;
  2. import android.content.Context;
  3. import android.graphics.Color;
  4. import android.graphics.drawable.ColorDrawable;
  5. import android.support.annotation.NonNull;
  6. import android.support.annotation.Nullable;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.view.animation.Animation;
  10. import android.view.animation.ScaleAnimation;
  11. import android.widget.PopupWindow;
  12. /**
  13. * desc : PopupWindow辅助类
  14. */
  15. public final class PopupWindowHelper implements PopupWindow.OnDismissListener {
  16. private PopupWindow mPopupWindow; // PopupWindow对象
  17. private final View mPopupView; //PopupWindow显示的View
  18. private long mDismissTime; // 记录PopupWindow销毁时间
  19. public PopupWindowHelper(View popupView) {
  20. mPopupView = popupView;
  21. }
  22. public PopupWindowHelper(Context context, int layoutId) {
  23. mPopupView = View.inflate(context, layoutId, null);
  24. }
  25. /**
  26. * 初始化PopupWindow
  27. */
  28. private void initPopupWindow() {
  29. // 给PopupWindow的View设置缩放动画
  30. ScaleAnimation sa = new ScaleAnimation(0.5f, 1.0f, 0.5f, 1.0f,
  31. Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  32. sa.setDuration(200);
  33. mPopupView.startAnimation(sa);
  34. mPopupWindow = new PopupWindow(mPopupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  35. // 需要在popupWindow使用动画,必须先设置背景,否则动画不能显示出效果,为了不和当前的背景冲突,这里设置全透明背景的图片
  36. mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  37. // 设置外部可触的,点击其他地方会自动消失
  38. mPopupWindow.setTouchable(true);
  39. mPopupWindow.setFocusable(true);
  40. mPopupWindow.setOutsideTouchable(true);
  41. // 监听PopupWindow销毁监听
  42. mPopupWindow.setOnDismissListener(this);
  43. }
  44. /**
  45. * {@link PopupWindow.OnDismissListener}
  46. */
  47. @Override
  48. public void onDismiss() {
  49. // 记录当前销毁的时间
  50. mDismissTime = System.currentTimeMillis();
  51. }
  52. /**
  53. * 显示一个PopupWindow
  54. *
  55. * @param clickView PopupWindow显示在什么View的下方
  56. */
  57. public void show(View clickView) {
  58. // 如果PopupWindow还未初始化就先进行初始化
  59. if (mPopupWindow == null) {
  60. initPopupWindow();
  61. }
  62. // 避免用户点击clickView导致的销毁后再次显示的Bug
  63. if (System.currentTimeMillis() - mDismissTime < 500) {
  64. return;
  65. }
  66. /*
  67. //获取某个view对象在窗口的位置,然后计算出PopupWindow的位置
  68. int[] location = new int[2];
  69. mClickView.getLocationInWindow(location);
  70. //将PopupWindow显示出来
  71. mPopupWindow.showAtLocation(mParentView, Gravity.LEFT + Gravity.TOP, 0, location[1] + mClickView.getHeight());
  72. */
  73. mPopupWindow.showAsDropDown(clickView);
  74. }
  75. /**
  76. * 销毁当前的PopupWindow
  77. */
  78. public void dismiss() {
  79. if (isShowing()) {
  80. mPopupWindow.dismiss();
  81. }
  82. }
  83. /**
  84. * 当前PopupWindow是否已经显示
  85. */
  86. public boolean isShowing() {
  87. return mPopupWindow != null && mPopupWindow.isShowing();
  88. }
  89. /**
  90. * 获取当前的PopupWindow对象
  91. */
  92. public @Nullable PopupWindow getPopupWindow() {
  93. return mPopupWindow;
  94. }
  95. /**
  96. * 获取当前的PopupWindow的View对象
  97. */
  98. public @NonNull View getPopupView() {
  99. return mPopupView;
  100. }
  101. }