The method of implementing the floating form with Android programming _android

Source: Internet
Author: User

The example in this article describes the method of implementing the floating form with Android programming. Share to everyone for your reference, specific as follows:

Suddenly interested in the suspension form, look up the data made a small demo, the effect is to click the button, close the current activity, show the suspension window, the window can be dragged, double-click and disappear. The effect chart is as follows:

Its use principle is very simple, is borrows the WindowManager this management class to realize.

1. First add the Use permission in Androidmanifest.xml:

Copy Code code as follows:
<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>

2. floating Window Layout implementation

public class Desktoplayout extends LinearLayout {public
  desktoplayout (context) {
    super);
    SetOrientation (linearlayout.vertical)//horizontal arrangement
    //Set width high
    this.setlayoutparams (new Layoutparams ( Layoutparams.wrap_content,
        layoutparams.wrap_content));
    View view = Layoutinflater.from (context). Inflate ( 
        r.layout.desklayout, null); 
    This.addview (view);
  }

3. Let it show up in the activity.

Gets the system form
Mwindowmanager = (WindowManager) getapplicationcontext ()
    . Getsystemservice ("window");
The layout style of the form
mlayout = new Windowmanager.layoutparams ();
Set form display Type--type_system_alert (System hint)
mlayout.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
Set form focus and touch:
//flag_not_focusable (Cannot get key input focus)
Mlayout.flags = WindowManager.LayoutParams.FLAG_NOT_ focusable;
Set the display mode
Mlayout.format = pixelformat.rgba_8888;
To set the Alignment method
mlayout.gravity = Gravity.top | Gravity.left;
Set the width and height of the form
mlayout.width = WindowManager.LayoutParams.WRAP_CONTENT;
Mlayout.height = WindowManager.LayoutParams.WRAP_CONTENT;

The detailed mainactivity code is as follows:

Package com.yc.yc_suspendingform;
Import android.app.Activity;
Import Android.graphics.PixelFormat;
Import Android.graphics.Rect;
Import Android.os.Bundle;
Import Android.util.Log;
Import android.view.Gravity;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.View.OnTouchListener;
Import Android.view.WindowManager;
Import Android.widget.Button; Import Com.yc.yc_floatingform.
R
  public class Mainactivity extends activity {private WindowManager Mwindowmanager;
  Private Windowmanager.layoutparams mlayout;
  Private Desktoplayout mdesktoplayout;
  Private long starttime;
  Declare the width and height of the screen float x, y;
  int top;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    
    Setcontentview (R.layout.activity_main);
    Createwindowmanager ();
    Createdesktoplayout ();
    Button btn = (button) Findviewbyid (R.ID.BTN);
   Btn.setonclicklistener (New Onclicklistener () {   public void OnClick (View v) {showdesk ();
  }
    });
    /** * Create a floating form/private void Createdesktoplayout () {mdesktoplayout = new desktoplayout (this);
      Mdesktoplayout.setontouchlistener (New Ontouchlistener () {float mtouchstartx;
      float Mtouchstarty; @Override public boolean Ontouch (View V, motionevent event) {//get the coordinates of the relative screen, that is, the upper-left corner of the screen is the origin x = Event.getr
        AWX (); y = Event.getrawy ()-top;
        25 is the height of the system status bar log.i ("STARTP", "startx" + mtouchstartx + "====starty" + mtouchstarty); Switch (event.getaction ()) {case Motionevent.action_down://Get the coordinates of the relative view, that is, the upper-left corner of this view is the original point Mtouch
          StartX = Event.getx ();
          Mtouchstarty = Event.gety ();
          LOG.I ("STARTP", "startx" + mtouchstartx + "====starty" + mtouchstarty);
          Long end = System.currenttimemillis ()-starttime; Double-click the interval below 300ms if (end <) {ClosedesK ();
          } StartTime = System.currenttimemillis ();
        Break
          Case Motionevent.action_move://update floating window Position parameter mlayout.x = (int) (X-MTOUCHSTARTX);
          MLAYOUT.Y = (int) (y-mtouchstarty);
          Mwindowmanager.updateviewlayout (V, mlayout);
        Break
          Case MOTIONEVENT.ACTION_UP://update floating window Position parameter mlayout.x = (int) (X-MTOUCHSTARTX);
          MLAYOUT.Y = (int) (y-mtouchstarty);
          Mwindowmanager.updateviewlayout (V, mlayout);
          The last position you can record here Mtouchstartx = Mtouchstarty = 0;
        Break
      return true;
  }
    });
    @Override public void Onwindowfocuschanged (Boolean hasfocus) {super.onwindowfocuschanged (hasfocus);
    Rect Rect = new Rect ();
    /Get the whole view section, note that if you want to set the heading style, this must appear after the heading style, otherwise there will be an error GetWindow (). Getdecorview (). Getwindowvisibledisplayframe (rect); top = rect.top;//The height of the status bar, so Rect.height,rect.width is the width of the system's height log.i ("Top", "" "+top);
    /** * shows desktoplayout/private void Showdesk () {Mwindowmanager.addview (mdesktoplayout, mlayout);
  Finish ();
    /** * Close Desktoplayout * * private void Closedesk () {Mwindowmanager.removeview (mdesktoplayout);
  Finish (); /** * Set WindowManager/private void Createwindowmanager () {//get system form Mwindowmanager = (windowmanage
    R) Getapplicationcontext (). Getsystemservice ("window");
    The layout style of the form mlayout = new Windowmanager.layoutparams ();
    Set form display Type--type_system_alert (System hint) Mlayout.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    Set form focus and touch://flag_not_focusable (Cannot get key input focus) Mlayout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    Set the display mode Mlayout.format = pixelformat.rgba_8888; To set the Alignment method mlayout.gravity = Gravity.top |
    Gravity.left;
    Set the width and height of the form mlayout.width = WindowManager.LayoutParams.WRAP_CONTENT; Mlayout.height = WindowManager.LayoutParams.WRAP_content; }
}

Full instance code code click here to download the site.

I hope this article will help you with the Android program.

Related Article

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.