First, the foreword:
I never put these things on my phone, but once I saw my colleague's Android phone, there is a QQ housekeeper floating on the desktop, colleagues drag the housekeeper, the housekeeper becomes a bird, the table below there is a slingshot, the top of the table there is a crow, the butler is the bird dragged to the slingshot that, and then, let go, birds fly out. This process is the animation process, the thing to do, is actually clear memory.
Two: Principle:
In fact, there is no principle, Use is WindowManager and windowmanager.layoutparams, to this layoutparams fuss, when set as a property, then, create a view, add this view to the WindowManager of the line.
Copy Code code as follows:
Package Com.chris.floats.window;
Import Android.os.Bundle;
Import Android.util.DisplayMetrics;
Import android.view.Gravity;
Import Android.view.WindowManager;
Import android.app.Activity;
Import Android.content.Context;
public class Mainactivity extends activity {
private static WindowManager mwindowmgr = null;
Private Windowmanager.layoutparams mwindowmgrparams = null;
private static Floatswindowview Mfloatswindowview = null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
}
/*
* Display the main interface, remove the suspension layer
*/
@Override
public void Onwindowfocuschanged (Boolean hasfocus) {
if (Hasfocus) {
if (Mfloatswindowview!= null) {
Mwindowmgr.removeview (Mfloatswindowview);
Mfloatswindowview = null;
}
}else{
Getwindowlayout ();
}
}
private void InitParams () {
Displaymetrics DM = getresources (). Getdisplaymetrics ();
Mwindowmgrparams.x = dm.widthpixels-136;
MWINDOWMGRPARAMS.Y = 300;
Mwindowmgrparams.width = 136;
Mwindowmgrparams.height = 136;
}
private void Getwindowlayout () {
if (Mfloatswindowview = = null) {
Mwindowmgr = (WindowManager) getbasecontext (). Getsystemservice (Context.window_service);
Mwindowmgrparams = new Windowmanager.layoutparams ();
/*
* 2003 in the point of suspension above all interface
* (in the 4.0+ system, under the Pull-down menu, and in 2.3, above the pull menu)
*/
Mwindowmgrparams.type = 2003;
Mwindowmgrparams.format = 1;
/*
* Code is actually wmparams.flags |= flag_not_focusable;
* The origin of the 40 is the default property of Wmparams (+) + flag_not_focusable (8)
*/
Mwindowmgrparams.flags = 40;
mwindowmgrparams.gravity = Gravity.left | Gravity.top;
InitParams ();
Mfloatswindowview = new Floatswindowview (this);
Mwindowmgr.addview (Mfloatswindowview, mwindowmgrparams);
}
}
}
The above code, mainly in the Getwindowlayout function, the last two lines is to create a view, and add to the WindowManager.
Inherit view's hover view:
Copy Code code as follows:
Package Com.chris.floats.window;
Import Android.content.Context;
Import android.content.Intent;
Import android.graphics.drawable.AnimationDrawable;
Import Android.util.AttributeSet;
Import Android.util.DisplayMetrics;
Import android.view.Gravity;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.ViewTreeObserver.OnPreDrawListener;
Import Android.view.WindowManager;
public class Floatswindowview extends View {
Private context mcontext = null;
Private WindowManager mwindowmgr = null;
Private Windowmanager.layoutparams mwindowmgrparams = null;
Private animationdrawable manimationdrawable = null;
private int iposx = 0;
private int iposy = 0;
private int ilastposx = 0;
private int ilastposy = 0;
Private Boolean bmoved = false;
Public Floatswindowview {
This is (context, NULL, 0);
}
Public Floatswindowview (context context, AttributeSet Attrs) {
This is (context, attrs, 0);
}
Public Floatswindowview (context context, AttributeSet attrs, int defstyle) {
Super (context, attrs, Defstyle);
Mcontext = context;
Mwindowmgr = (WindowManager) getcontext (). Getapplicationcontext (). Getsystemservice ("window");
Mwindowmgrparams = new Windowmanager.layoutparams ();
InitParams ();
manimationdrawable = new Animationdrawable ();
for (int i = 0; i < 4; i++) {
int id = getresources (). Getidentifier ("a" + I, "drawable", Mcontext.getpackagename ());
Manimationdrawable.addframe (Getresources (). getdrawable (ID), 100);
}
Manimationdrawable.setoneshot (FALSE);
This.setbackgrounddrawable (manimationdrawable);
Onpredrawlistener listener = new Onpredrawlistener () {
@Override
public Boolean Onpredraw () {
Manimationdrawable.start ();
return true;
}
};
This.getviewtreeobserver (). Addonpredrawlistener (listener);
}
private void InitParams () {
Displaymetrics DM = getresources (). Getdisplaymetrics ();
Mwindowmgrparams.x = dm.widthpixels-136;
MWINDOWMGRPARAMS.Y = 300;
Mwindowmgrparams.width = 136;
Mwindowmgrparams.height = 136;
}
@Override
public boolean ontouchevent (Motionevent event) {
Switch (event.getaction ()) {
Case Motionevent.action_down:
IPOSX = (int) event.getx ();
Iposy = (int) event.gety ();
Bmoved = false;
Break
Case Motionevent.action_move:
Bmoved = true;
ILASTPOSX = (int) event.getx ();
Ilastposy = (int) event.gety ();
Updatepostion (ILASTPOSX-IPOSX, Ilastposy-iposy);
Break
Case MOTIONEVENT.ACTION_UP:
if (!bmoved) {
Intent it=new Intent (Mcontext, Mainactivity.class);
Mcontext.startactivity (IT);
}
Break
Default
Break
}
return true;
}
private void Updatepostion (int x, int y) {
Mwindowmgrparams.type = 2003;
Mwindowmgrparams.format = 1;
Mwindowmgrparams.flags = 40;
mwindowmgrparams.gravity = Gravity.left | Gravity.top;
mwindowmgrparams.x = x;
Mwindowmgrparams.y = y;
Mwindowmgr.updateviewlayout (this, mwindowmgrparams);
}
}
The argument in Updateposition is the same as setting in the activity to make sure that the relative position is different at move time, causing the flash gravel, if you don't understand it, you can experiment.
Third, Summary:
This article implements a simple levitation window animation effect, if you want to do imaging 360,qq housekeeper, but also need some other operations:
1. For example, start a background service to monitor system information;
2. When Action_down, modify the picture on the suspension window;
3. Action_move when the window follows;
4. When action_up, create a thread to complete the release, upward motion of the animation process, etc.;