现在很多应用都有这样的功能,比如360等安全卫士,手机管家之内的应用。
效果图:
一、实现原理及移动思路
调用WindowManager,并设置WindowManager.LayoutParams的相关属性,通过WindowManager的addView方法创建View,这样产生出来的View根据WindowManager.LayoutParams属性不同,效果也就不同了。比如创建系统顶级窗口,实现悬浮窗口效果!然后通过覆写悬浮View中onTouchEvent方法来改变windowMananager.LayoutParams中x和y的值来实现自由移动悬浮窗口。
二、示例代码
先看看悬浮View的代码把
package com.example.suspend; import android.content.Context; import android.util.Log; import android.view.MotionEvent; import android.view.WindowManager; import android.widget.ImageView; public class MyFloatView extends ImageView { private float mTouchStartX; private float mTouchStartY; private float x; private float y; private WindowManager wm = (WindowManager) getContext() .getApplicationContext().getSystemService("window"); // 此wmParams变量为获取的全局变量,用以保存悬浮窗口的属性 private WindowManager.LayoutParams wmParams = ((MyApplication) getContext() .getApplicationContext()).getMywmParams(); public MyFloatView(Context context) { super(context); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub // 获取相对屏幕的坐标,即以屏幕左上角为原点 x = event.getRawX(); y = event.getRawY() - 25; // 25是系统状态栏的高度 Log.i("currP", "currX" + x + "====currY" + y); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 捕获手指触摸按下动作 // 获取相对View的坐标,即以此View左上角为原点 mTouchStartX = event.getX(); mTouchStartY = event.getY(); Log.i("startP", "startX" + mTouchStartX + "====startY" + mTouchStartY); break; case MotionEvent.ACTION_MOVE://捕获手指触摸移动动作 updateViewPosition(); break; case MotionEvent.ACTION_UP://捕获手指触摸离开动作 updateViewPosition(); mTouchStartX=mTouchStartY=0; break; default: break; } return true; } private void updateViewPosition() { //更新浮动窗口位置参数 wmParams.x = (int)(x - mTouchStartX); wmParams.y = (int)(y - mTouchStartY); wm.updateViewLayout(this, wmParams);//刷新显示 } }
上面的wmParams变量(即WindowManager.LayoutParams)的存储采用了extends Application的方式来创建全局变量,
示例代码如下:
package com.example.suspend; import android.app.Application; import android.view.WindowManager; public class MyApplication extends Application { private WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams(); @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); } public WindowManager.LayoutParams getMywmParams() { return wmParams; } }
再来看一看Activity中的代码:
package com.example.suspend; import android.app.Activity; import android.graphics.PixelFormat; import android.os.Bundle; import android.view.Gravity; import android.view.WindowManager; import android.view.WindowManager.LayoutParams; public class MainActivity extends Activity { private WindowManager wm; private WindowManager.LayoutParams wmParams; private MyFloatView myFV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 创建悬浮窗口 createFloatView(); } private void createFloatView() { myFV = new MyFloatView(getApplicationContext()); myFV.setImageResource(R.drawable.ic_launcher); // 获取WindowManager wm = (WindowManager) getApplicationContext().getSystemService("window"); // 设置LayoutParams(全局变量)相关参数 wmParams = ((MyApplication) getApplication()).getMywmParams(); wmParams.type = LayoutParams.TYPE_PHONE;// 设置window type wmParams.format = PixelFormat.RGBA_8888;// 设置图片格式,效果为背景透明 // 设置Window flag wmParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE; /* * * 下面的flags属性的效果形同“锁定”。 * * 悬浮窗不可触摸,不接受任何事件,同时不影响后面的事件响应。 * * wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL * * | LayoutParams.FLAG_NOT_FOCUSABLE * * | LayoutParams.FLAG_NOT_TOUCHABLE; */ wmParams.gravity = Gravity.LEFT | Gravity.TOP;// 调整悬浮窗口至左上角,便于调整坐标 // 以屏幕左上角为原点,设置x、y初始值 wmParams.x = 0; wmParams.y = 0; // 设置悬浮窗口长宽数据 wmParams.; wmParams.; // 显示myFloatView图像 wm.addView(myFV, wmParams); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); // 在程序退出(Activity销毁)时销毁悬浮窗口 wm.removeView(myFV); } }
最后,别忘了在AndroidManifest.xml中添加权限: