发表于 2013-6-24 20:26:57 | 只看该作者 一、前言: 我手机从来不装这些东西,不过,有次看到同事的android手机上,有个QQ管家在桌面上浮着,同事拖动管家时,管家就变成一只鸟,桌面下方还有个弹弓,桌面顶部有只乌鸦,把管家也就是鸟拖动到弹弓那,然后,松手,鸟就飞出去。这个过程是动画过程,做的事,实际上是清楚内存。 二:原理: 其实,没什么原理,用到的就是WindowManager以及WindowManager.LayoutParams,对这个LayoutParams做文章,当设置为属性后,然后,创建一个View,将这个View添加到WindowManager中就行。 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); } /* * 显示应用主界面时,去除悬浮层 */ @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 在指悬浮在所有界面之上 * (4.0+系统中,在下拉菜单下面,而在2.3中,在上拉菜单之上) */ mWindowMgrParams.type = 2003; mWindowMgrParams.format = 1; /* * 代码实际是wmParams.flags |= FLAG_NOT_FOCUSABLE; * 40的由来是wmParams的默认属性(32)+ FLAG_NOT_FOCUSABLE(8) */ mWindowMgrParams.flags = 40; mWindowMgrParams.gravity = Gravity.LEFT | Gravity.TOP; initParams(); mFloatsWindowView = new FloatsWindowView(this); mWindowMgr.addView(mFloatsWindowView, mWindowMgrParams); } } } 复制代码 链接地址:http://www.apkbus.com/android-121930-1-3.html