Android Programming Hover Form

Source: Internet
Author: User

Using the phone 360 and QQ mobile phone butler and other software friends, will find that in these applications, there will be a suspended form, such as QQ phone butler in the phone scene:


This form, in addition to being displayed, can also move its position and always appear. In addition to closing the current program, the window does not actively disappear. In fact, its use of the principle is very simple, is to borrow WindowManager this management class to achieve.
Note: To add the use rights in Androidmanifest.xml:

[HTML]View Plaincopy
    1. <uses-permission
    2. android:name="Android.permission.SYSTEM_ALERT_WINDOW" />


Here, I use the code layout of the way, imitate the QQ this interface effect:

[Java]View Plaincopy
  1. Import Android.content.Context;
  2. Import Android.widget.ImageView;
  3. Import Android.widget.LinearLayout;
  4. Import Android.widget.TextView;
  5. Public class Desktoplayout extends LinearLayout {
  6. Public Desktoplayout (context context) {
  7. super (context);
  8. SetOrientation (linearlayout.horizontal);
  9. Layoutparams mlayoutparams = new Layoutparams (
  10. Layoutparams.wrap_content, layoutparams.wrap_content);
  11. Setlayoutparams (Mlayoutparams);
  12. //Display icon
  13. ImageView Mimageview = new ImageView (context);
  14. Mimageview.setimageresource (R.drawable.icon);
  15. AddView (Mimageview, mlayoutparams);
  16. //Display of text
  17. TextView Mtextview = new TextView (context);
  18. Mtextview.settext ("Hello");
  19. Mtextview.settextsize (30);
  20. AddView (Mtextview, mlayoutparams);
  21. }
  22. }


Next, let it show up in the activity. The first thing to do is to set Windowmanager.layoutparams:

[Java]View Plaincopy
  1. Get System Form
  2. Mwindowmanager = (WindowManager) getapplicationcontext ()
  3. . Getsystemservice ("window");
  4. Layout styles for Forms
  5. Mlayoutparams = new Windowmanager.layoutparams ();
  6. Set the form display Type--type_system_alert (System tip)
  7. Mlayoutparams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
  8. Set the form focus and touch:
  9. Flag_not_focusable (Cannot get key input focus)
  10. Mlayoutparams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
  11. Set the display mode
  12. Mlayoutparams.format = pixelformat.rgba_8888;
  13. To set the Alignment method
  14. mlayoutparams.gravity = Gravity.top | Gravity.left;
  15. Set form width and height
  16. Mlayoutparams.width = WindowManager.LayoutParams.WRAP_CONTENT;
  17. Mlayoutparams.height = WindowManager.LayoutParams.WRAP_CONTENT;
  18. Set the position of the form to display otherwise in the center of the screen
  19. Mlayoutparams.x = 50;
  20. Mlayoutparams.y = 50;


To display a form and close a form:

[Java]View Plaincopy
    1. Mwindowmanager.addview (Mdesktoplayout, mlayoutparams);
    2. Mwindowmanager.removeview (mdesktoplayout);


Here is the original code for activity, which is designed to double-click the effect of closing the form:

[Java]View Plaincopy
  1. Import android.app.Activity;
  2. Import Android.graphics.PixelFormat;
  3. Import Android.os.Bundle;
  4. Import android.view.Gravity;
  5. Import android.view.MotionEvent;
  6. Import Android.view.View;
  7. Import Android.view.View.OnClickListener;
  8. Import Android.view.View.OnTouchListener;
  9. Import Android.view.WindowManager;
  10. Import Android.widget.Button;
  11. Public class Desktip extends Activity {
  12. private WindowManager Mwindowmanager;
  13. private Windowmanager.layoutparams Mlayoutparams;
  14. private Desktoplayout mdesktoplayout;
  15. private long starttime;
  16. /** 
  17. * Create a suspended form
  18. */
  19. private void Createdesktoplayout () {
  20. Mdesktoplayout = New Desktoplayout (this);
  21. Mdesktoplayout.setontouchlistener (new Ontouchlistener () {
  22. Public Boolean OnTouch (View V, motionevent event) {
  23. Onactionmove (event);
  24. return true;
  25. }
  26. });
  27. }
  28. /** 
  29. * Set WindowManager
  30. */
  31. private void Createwindowmanager () {
  32. //Get system form
  33. Mwindowmanager = (WindowManager) getapplicationcontext ()
  34. . Getsystemservice ("window");
  35. //Layout style of the form
  36. Mlayoutparams = new Windowmanager.layoutparams ();
  37. //Set form display Type--type_system_alert (System prompt)
  38. Mlayoutparams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
  39. //Set the form focus and touch:
  40. //Flag_not_focusable (Cannot get key input focus)
  41. Mlayoutparams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
  42. //Set the display mode
  43. Mlayoutparams.format = pixelformat.rgba_8888;
  44. //Set the Alignment method
  45. mlayoutparams.gravity = Gravity.top | Gravity.left;
  46. //Set form width and height
  47. Mlayoutparams.width = WindowManager.LayoutParams.WRAP_CONTENT;
  48. Mlayoutparams.height = WindowManager.LayoutParams.WRAP_CONTENT;
  49. //Set the position of the form to display otherwise in the center of the screen
  50. Mlayoutparams.x = 50;
  51. Mlayoutparams.y = 50;
  52. }
  53. private void Onactionmove (Motionevent event) {
  54. if (event.getaction () = = Motionevent.action_down) {
  55. Long end = System.currenttimemillis ()-starttime;
  56. //Double-click Interval between 200ms to 500ms
  57. if (End > && End < + ) {
  58. Closedesk ();
  59. return;
  60. }
  61. StartTime = System.currenttimemillis ();
  62. }
  63. Mlayoutparams.x = (int) (EVENT.GETRAWX ()-(Mdesktoplayout.getwidth ()));
  64. Mlayoutparams.y = (int) (Event.getrawy ()-(Mdesktoplayout.getheight ()));
  65. Mwindowmanager.updateviewlayout (Mdesktoplayout, mlayoutparams);
  66. }
  67. /** 
  68. * Show Desktoplayout
  69. */
  70. private void Showdesk () {
  71. Mwindowmanager.addview (Mdesktoplayout, mlayoutparams);
  72. Finish ();
  73. }
  74. /** 
  75. * Close Desktoplayout
  76. */
  77. private void Closedesk () {
  78. Mwindowmanager.removeview (mdesktoplayout);
  79. Finish ();
  80. }
  81. public void OnCreate (Bundle savedinstancestate) {
  82. super.oncreate (savedinstancestate);
  83. Setcontentview (R.layout.main);
  84. Createwindowmanager ();
  85. Createdesktoplayout ();
  86. Button btn = (button) Findviewbyid (R.ID.BTN);
  87. Btn.setonclicklistener (new Onclicklistener () {
  88. public void OnClick (View v) {
  89. Showdesk ();
  90. }
  91. });
  92. }
  93. }


Show the effect:



Example of a suspended form

http://blog.csdn.net/xyz_fly/article/details/7546096

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.