分析windowManager中添加一個懸浮框的方式

1
2
3
4
5
6
7
8
9
10
11
12
13
WindowManager.LayoutParams params new LayoutParams();
        params.width = width; 
        params.height = height;
        params.format = PixelFormat.TRANSLUCENT;
        params.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;//後面窗口仍然可以處理點設備事件
        params.setTitle("Toast");
        params.gravity = gravity;
        params.windowAnimations = styleAnimations;
WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
TextView float new TextView(context);
float.settext("this is a float window ");
windowManager.addView(view, this.mLayoutParams)

關於param參數的詳解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
type 的取值: 
      應用程序窗口。
      public static final int FIRST_APPLICATION_WINDOW = 1;    
 
      所有程序窗口的“基地”窗口,其他應用程序窗口都顯示在它上面。     
      public static final int TYPE_BASE_APPLICATION   =1;
        
      普通應用功能程序窗口。token必須設置爲Activity的token,以指出該窗口屬誰。
      public static final int TYPE_APPLICATION       = 2;
 
       用於應用程序啓動時所顯示的窗口。應用本身不要使用這種類型。
      它用於讓系統顯示些信息,直到應用程序可以開啓自己的窗口。   
      public static final int TYPE_APPLICATION_STARTING = 3; 
      
      應用程序窗口結束。
      public static final int LAST_APPLICATION_WINDOW = 99;
 
      子窗口。子窗口的Z序和座標空間都依賴於他們的宿主窗口。
      public static final int FIRST_SUB_WINDOW       = 1000;
 
      面板窗口,顯示於宿主窗口上層。
      public static final int TYPE_APPLICATION_PANEL  = FIRST_SUB_WINDOW;
 
      媒體窗口,例如視頻。顯示於宿主窗口下層。
      public static final int TYPE_APPLICATION_MEDIA  = FIRST_SUB_WINDOW+1;
 
      應用程序窗口的子面板。顯示於所有面板窗口的上層。(GUI的一般規律,越“子”越靠上)
      public static final int TYPE_APPLICATION_SUB_PANEL = FIRST_SUB_WINDOW +2;
 
      對話框。類似於面板窗口,繪製類似於頂層窗口,而不是宿主的子窗口。
      public static final int TYPE_APPLICATION_ATTACHED_DIALOG = FIRST_SUB_WINDOW +3;
 
      媒體信息。顯示在媒體層和程序窗口之間,需要實現透明(半透明)效果。(例如顯示字幕)
      public static final int TYPE_APPLICATION_MEDIA_OVERLAY  = FIRST_SUB_WINDOW +4;
 
      子窗口結束。( End of types of sub-windows )
      public static final int LAST_SUB_WINDOW        = 1999;
 
      系統窗口。非應用程序創建。
      public static final int FIRST_SYSTEM_WINDOW    = 2000;
 
      狀態欄。只能有一個狀態欄;它位於屏幕頂端,其他窗口都位於它下方。
      public static final int TYPE_STATUS_BAR        =  FIRST_SYSTEM_WINDOW;
 
      搜索欄。只能有一個搜索欄;它位於屏幕上方。
      public static final int TYPE_SEARCH_BAR        = FIRST_SYSTEM_WINDOW+1;
 
      電話窗口。它用於電話交互(特別是呼入)。它置於所有應用程序之上,狀態欄之下。
      public static final int TYPE_PHONE            = FIRST_SYSTEM_WINDOW+2;
 
      系統提示。它總是出現在應用程序窗口之上。
      public static final int TYPE_SYSTEM_ALERT      =  FIRST_SYSTEM_WINDOW +3;
 
      鎖屏窗口。
      public static final int TYPE_KEYGUARD          = FIRST_SYSTEM_WINDOW +4;
 
      信息窗口。用於顯示toast。
      public static final int TYPE_TOAST            = FIRST_SYSTEM_WINDOW +5;
 
      系統頂層窗口。顯示在其他一切內容之上。此窗口不能獲得輸入焦點,否則影響鎖屏。
      public static final int TYPE_SYSTEM_OVERLAY    =  FIRST_SYSTEM_WINDOW +6;
 
      電話優先,當鎖屏時顯示。此窗口不能獲得輸入焦點,否則影響鎖屏。
      public static final int TYPE_PRIORITY_PHONE    =  FIRST_SYSTEM_WINDOW +7;
 
      系統對話框。(例如音量調節框)。
      public static final int TYPE_SYSTEM_DIALOG     =  FIRST_SYSTEM_WINDOW +8;
 
      鎖屏時顯示的對話框。
      public static final int TYPE_KEYGUARD_DIALOG   =  FIRST_SYSTEM_WINDOW +9;
 
      系統內部錯誤提示,顯示於所有內容之上。
      public static final int TYPE_SYSTEM_ERROR      =  FIRST_SYSTEM_WINDOW +10;
 
      內部輸入法窗口,顯示於普通UI之上。應用程序可重新佈局以免被此窗口覆蓋。
      public static final int TYPE_INPUT_METHOD      =  FIRST_SYSTEM_WINDOW +11;
 
      內部輸入法對話框,顯示於當前輸入法窗口之上。
      public static final int TYPE_INPUT_METHOD_DIALOG= FIRST_SYSTEM_WINDOW +12;
 
      牆紙窗口。
      public static final int TYPE_WALLPAPER         = FIRST_SYSTEM_WINDOW +13;
 
      狀態欄的滑動面板。
      public static final int TYPE_STATUS_BAR_PANEL   = FIRST_SYSTEM_WINDOW +14;
 
      系統窗口結束。
<br>      public static final int LAST_SYSTEM_WINDOW     = 2999;

這裏需要注意的地方是:

  對於需要依附於activity的選擇2000以下的,獨立於activity,使用2002

2.對於flag,這裏詳細的說明
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int FLAGS_CHANGED   
int FLAG_ALLOW_LOCK_WHILE_SCREEN_ON Window flag: as long as this window is visible to the user, allow the lock screen to activate while the screen is on.
int FLAG_ALT_FOCUSABLE_IM   Window flag: invert the state of FLAG_NOT_FOCUSABLE with respect to how this window interacts with the current method.
int FLAG_BLUR_BEHIND     This constant was deprecated in API level 14. Blurring is no longer supported.
int FLAG_DIM_BEHIND Window flag: everything behind this window will be dimmed.
int FLAG_DISMISS_KEYGUARD   Window flag: when set the window will cause the keyguard to be dismissed, only if it is not a secure lock keyguard.
int FLAG_DITHER  This constant was deprecated in API level 17. This flag is no longer used.
int FLAG_FORCE_NOT_FULLSCREEN   Window flag: override FLAG_FULLSCREEN and force the screen decorations (such as the status bar) to be shown.
int FLAG_FULLSCREEN Window flag: hide all screen decorations (such as the status bar) while this window is displayed.
int FLAG_HARDWARE_ACCELERATED   
Indicates whether this window should be hardware accelerated.
int FLAG_IGNORE_CHEEK_PRESSES   Window flag: intended for windows that will often be used when the user is holding the screen against their face, it will aggressively filter the event stream to prevent unintended presses in this situation that may not be desired for a particular window, when such an event stream is detected, the application will receive a CANCEL motion event to indicate this so applications can handle this accordingly by taking no action on the event until the finger is released.
int FLAG_KEEP_SCREEN_ON Window flag: as long as this window is visible to the user, keep the device's screen turned on and bright.
int FLAG_LAYOUT_INSET_DECOR Window flag: a special option only for use in combination with FLAG_LAYOUT_IN_SCREEN.
int FLAG_LAYOUT_IN_OVERSCAN Window flag: allow window contents to extend in to the screen's overscan area, if there is one.
int FLAG_LAYOUT_IN_SCREEN   Window flag: place the window within the entire screen, ignoring decorations around the border (such as the status bar).
int FLAG_LAYOUT_NO_LIMITS   Window flag: allow window to extend outside of the screen.
int FLAG_NOT_FOCUSABLE  Window flag: this window won't ever get key input focus, so the user can not send key or other button events to it.
int FLAG_NOT_TOUCHABLE  Window flag: this window can never receive touch events.
int FLAG_NOT_TOUCH_MODAL    Window flag: even when this window is focusable (its FLAG_NOT_FOCUSABLE is not set), allow any pointer events outside of the window to be sent to the windows behind it.
int FLAG_SCALED Window flag: a special mode where the layout parameters are used to perform scaling of the surface when it is composited to the screen.
int FLAG_SECURE Window flag: treat the content of the window as secure, preventing it from appearing in screenshots or from being viewed on non-secure displays.
int FLAG_SHOW_WALLPAPER Window flag: ask that the system wallpaper be shown behind your window.
int FLAG_SHOW_WHEN_LOCKED   Window flag: special flag to let windows be shown when the screen is locked.
int FLAG_SPLIT_TOUCH    Window flag: when set the window will accept for touch events outside of its bounds to be sent to other windows that also support split touch.
int FLAG_TOUCHABLE_WHEN_WAKING  Window flag: when setif the device is asleep when the touch screen is pressed, you will receive this first touch event.
int FLAG_TURN_SCREEN_ON Window flag: when set as a window is being added or made visible, once the window has been shown then the system will poke the power manager's user activity (as if the user had woken up the device) to turn the screen on.
int FLAG_WATCH_OUTSIDE_TOUCH    Window flag: if you have set FLAG_NOT_TOUCH_MODAL, you can set this flag to receive a single special MotionEvent with the action MotionEvent.ACTION_OUTSIDE for touches that occur outside of your window.

  3.對於windowAnimation

很多人說爲什麼我設置了windowAnimation,但是沒有動畫效果呢?

這裏一定要注意,這裏需要使用style,在style中添加如下:

     

1
2
3
4
5
6
7
  <style name="top">
 
    <item name="@android:windowEnterAnimation">@anim/sanqiwan_toast_slide_top_enter</item>
 
   <item name="@android:windowExitAnimation">@anim/sanqiwan_toast_slide_top_exit</item>
 
</style>

  然後將style的resourceId賦給params.windowAnimation,如果是將動畫的resourceId賦值給params.windowAnimation,死也看得不到動畫效果滴。

發佈了18 篇原創文章 · 獲贊 20 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章