FloatWindow.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package org.yczbj.ycvideoplayerlib.window;
  2. import android.animation.TimeInterpolator;
  3. import android.content.Context;
  4. import android.support.annotation.LayoutRes;
  5. import android.support.annotation.MainThread;
  6. import android.support.annotation.NonNull;
  7. import android.support.annotation.Nullable;
  8. import android.view.Gravity;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. /**
  15. * 记得添加下面这个权限
  16. * uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"
  17. */
  18. public class FloatWindow {
  19. private FloatWindow() {
  20. }
  21. private static final String mDefaultTag = "default_float_window_tag";
  22. private static Map<String, IFloatWindow> mFloatWindowMap;
  23. public static IFloatWindow get() {
  24. return get(mDefaultTag);
  25. }
  26. public static IFloatWindow get(@NonNull String tag) {
  27. return mFloatWindowMap == null ? null : mFloatWindowMap.get(tag);
  28. }
  29. @MainThread
  30. public static B with(@NonNull Context applicationContext) {
  31. return new B(applicationContext);
  32. }
  33. public static void destroy() {
  34. destroy(mDefaultTag);
  35. }
  36. public static void destroy(String tag) {
  37. if (mFloatWindowMap == null || !mFloatWindowMap.containsKey(tag)) {
  38. return;
  39. }
  40. mFloatWindowMap.get(tag).dismiss();
  41. mFloatWindowMap.remove(tag);
  42. }
  43. public static class B {
  44. Context mApplicationContext;
  45. View mView;
  46. private int mLayoutId;
  47. int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
  48. int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
  49. int gravity = Gravity.TOP | Gravity.START;
  50. int xOffset;
  51. int yOffset;
  52. boolean mShow = true;
  53. Class[] mActivities;
  54. int mMoveType = MoveType.fixed;
  55. long mDuration = 300;
  56. TimeInterpolator mInterpolator;
  57. private String mTag = mDefaultTag;
  58. private B() {
  59. }
  60. B(Context applicationContext) {
  61. mApplicationContext = applicationContext;
  62. }
  63. public B setView(@NonNull View view) {
  64. mView = view;
  65. return this;
  66. }
  67. public B setView(@LayoutRes int layoutId) {
  68. mLayoutId = layoutId;
  69. return this;
  70. }
  71. public B setWidth(int width) {
  72. mWidth = width;
  73. return this;
  74. }
  75. public B setHeight(int height) {
  76. mHeight = height;
  77. return this;
  78. }
  79. public B setWidth(@WindowScreen.screenType int screenType, float ratio) {
  80. mWidth = (int) ((screenType == WindowScreen.width ?
  81. WindowUtil.getScreenWidth(mApplicationContext) :
  82. WindowUtil.getScreenHeight(mApplicationContext)) * ratio);
  83. return this;
  84. }
  85. public B setHeight(@WindowScreen.screenType int screenType, float ratio) {
  86. mHeight = (int) ((screenType == WindowScreen.width ?
  87. WindowUtil.getScreenWidth(mApplicationContext) :
  88. WindowUtil.getScreenHeight(mApplicationContext)) * ratio);
  89. return this;
  90. }
  91. public B setX(int x) {
  92. xOffset = x;
  93. return this;
  94. }
  95. public B setY(int y) {
  96. yOffset = y;
  97. return this;
  98. }
  99. public B setX(@WindowScreen.screenType int screenType, float ratio) {
  100. xOffset = (int) ((screenType == WindowScreen.width ?
  101. WindowUtil.getScreenWidth(mApplicationContext) :
  102. WindowUtil.getScreenHeight(mApplicationContext)) * ratio);
  103. return this;
  104. }
  105. public B setY(@WindowScreen.screenType int screenType, float ratio) {
  106. yOffset = (int) ((screenType == WindowScreen.width ?
  107. WindowUtil.getScreenWidth(mApplicationContext) :
  108. WindowUtil.getScreenHeight(mApplicationContext)) * ratio);
  109. return this;
  110. }
  111. /**
  112. * 设置 Activity 过滤器,用于指定在哪些界面显示悬浮窗,默认全部界面都显示
  113. *
  114. * @param show  过滤类型,子类类型也会生效
  115. * @param activities  过滤界面
  116. */
  117. public B setFilter(boolean show, @NonNull Class... activities) {
  118. mShow = show;
  119. mActivities = activities;
  120. return this;
  121. }
  122. public B setMoveType(@MoveType.MOVE_TYPE int moveType) {
  123. mMoveType = moveType;
  124. return this;
  125. }
  126. public B setMoveStyle(long duration, @Nullable TimeInterpolator interpolator) {
  127. mDuration = duration;
  128. mInterpolator = interpolator;
  129. return this;
  130. }
  131. public B setTag(@NonNull String tag) {
  132. mTag = tag;
  133. return this;
  134. }
  135. public void build() {
  136. if (mFloatWindowMap == null) {
  137. mFloatWindowMap = new HashMap<>();
  138. }
  139. if (mFloatWindowMap.containsKey(mTag)) {
  140. throw new IllegalArgumentException("FloatWindow of this tag has been added, Please set a new tag for the new FloatWindow");
  141. }
  142. if (mView == null && mLayoutId == 0) {
  143. throw new IllegalArgumentException("View has not been set!");
  144. }
  145. if (mView == null) {
  146. LayoutInflater inflate = (LayoutInflater)
  147. mApplicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  148. if (inflate != null) {
  149. mView = inflate.inflate(mLayoutId, null);
  150. }
  151. }
  152. IFloatWindow floatWindowImpl = new IFloatWindowImpl(this);
  153. mFloatWindowMap.put(mTag, floatWindowImpl);
  154. }
  155. }
  156. }