First, the Knowledge reserve
1. Display a view on the desktop
WindowManager MWM = (windowmanager) this.getsystemservice (Context.window_service); Windowmanager.layoutparams params = new Windowmanager.layoutparams ();p arams.height = WindowManager.LayoutParams.WRAP _content;params.width = Windowmanager.layoutparams.wrap_content;params.format = pixelformat.translucent; Params.type = windowmanager.layoutparams.type_phone;//Phone window, which is placed in all windows Params.flags = WindowManager.LayoutParams.FLAG_ keep_screen_on | Windowmanager.layoutparams.flag_not_focusable;params.gravity=gravity.left + gravity.top;//Set the center of gravity to the top left, Instead of the default central Params.settitle ("Toast"); View view = View.inflate (this, r.layout.rocket, null); Mwm.addview (view, params);
Attention:
1, if the last time left, the icon on the edge (beyond the screen), then the system in order to display the icon to reduce the icon
Prevent icons from exceeding the screen in drag and drop
Update View Method 1
@Overridepublic boolean OnTouch (View V, motionevent event) {switch (event.getaction ()) {case Motionevent.action_down: {/ /Gets the starting coordinate startx = (int) event.getrawx (); starty = (int) Event.getrawy (); break;} Case Motionevent.action_move: {endx = (int) event.getrawx (); endY = (int) event.getrawy ();//calculate offset int dx = Endx-startx;in T dy = endy-starty;//update the float window position params.x + = Dx;params.y + = dy;//Prevent coordinates from deviating from the screen if (Params.x < 0) {params.x = 0;} if (Params.y < 0) {params.y = 0;} if (Params.x > Winwidth-view.getwidth ()) {params.x = Winwidth-view.getwidth ();} if (Params.y > Winwidth-view.getheight ()) {params.y = Winwidth-view.getheight ();} Mwm.updateviewlayout (view, params);//Update view//update start coordinates startx = Endx;starty = Endy;break;} Case MOTIONEVENT.ACTION_UP://Record coordinate point Editor editor=mpref.edit (); Editor.putint ("Lastx", params.x); Editor.putint ("Lasty", params.y); Editor.commit (); Sendrocket (); break;default:break;} return true;}
Update View Method 2
@Overridepublic boolean OnTouch (View V, motionevent event) {switch (event.getaction ()) {case Motionevent.action_down: {/ /Gets the starting coordinate startx = (int) event.getrawx (); starty = (int) Event.getrawy (); break;} Case Motionevent.action_move: {endx = (int) event.getrawx (); endY = (int) event.getrawy ();//calculate offset int dx = Endx-startx;in T dy = endy-starty;int left = view.getleft () + dx;int top = view.gettop () + Dy;int right = View.getright () + dx;int Bott Om = view.getbottom () + dy;//prevent coordinates from deviating from the screen (note the status bar height) if (left < 0 | | right>winwidth | | top<0 | | bottom>winheight-20) {break;} View.layout (Left,top,right,bottom);//update start coordinates startx = Endx;starty = Endy;break;} Case MOTIONEVENT.ACTION_UP://Record coordinate point Editor editor=mpref.edit (); Editor.putint ("Lastx", params.x); Editor.putint ("Lasty", params.y); Editor.commit (); Sendrocket (); break;default:break;} return true;}
2, Android bottom in the picture icon time three steps onmeasure (measurement view), OnLayout (placement), OnDraw (Draw)
Cannot use View.layout (Left,top,right,bottom) in the activity's oncreate (not yet measured);
Workaround:
Save last left position PRIVATE sharedpreferences mpref;mpref = getsharedpreferences ("config", mode_private); mpref = Getsharedpreferences ("config", mode_private); Editor Editor=mpref.edit (); Editor.putint ("Lastx", params.x); Editor.putint ("Lasty", Params.y); Editor.commit ()// Restore the position of the last departure int lastx=mpref.getint ("Lastx", "ten"), int lasty=mpref.getint ("lasty", 10); Layoutparams layoutparams= (Layoutparams) view.getlayoutparams (); layoutparams.leftmargin=lastx;// Set the left margin layoutparams.topmargin=lasty;//set the right margin View.setlayoutparams (layoutparams);
Small Rocket Example