//创建创建全局变量类
1 public class MyApplication extends Application { 2 3 /** 4 * 创建全局变量 5 * 全局变量一般都比较倾向于创建一个单独的数据类文件,并使用static静态变量 6 * 7 * 这里使用了在Application中添加数据的方法实现全局变量 8 * 注意在AndroidManifest.xml中的Application节点添加android:name=".MyApplication"属性 9 * 10 */ 11 private WindowManager.LayoutParams wmParams= new WindowManager.LayoutParams(); 12 13 public WindowManager.LayoutParams getMywmParams(){ 14 return wmParams; 15 }
//Activity类
1 public class MyFloatViewActivity extends Activity { 2 3 4 /** Called when the activity is first created. */ 5 6 private WindowManager wm= null; 7 private WindowManager.LayoutParams wmParams= null; 8 9 private MyFloatView myFV= null; 10 11 12 @Override 13 public void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.main); 16 18 } 19 22 private void createView(){ 23 myFV= new MyFloatView(getApplicationContext()); 24 myFV.setImageResource(R.drawable.icon); 25 // 获取WindowManager 26 wm=(WindowManager)getApplicationContext().getSystemService("window"); 27 // 设置LayoutParams(全局变量)相关参数 28 wmParams = ((MyApplication)getApplication()).getMywmParams(); 29 30 /** 31 *以下都是WindowManager.LayoutParams的相关属性 32 * 具体用途可参考SDK文档 33 */ 34 wmParams.type=LayoutParams.TYPE_PHONE; // 设置window type 35 wmParams.format=PixelFormat.RGBA_8888; // 设置图片格式,效果为背景透明 36 37 // 设置Window flag 38 wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL 39 | LayoutParams.FLAG_NOT_FOCUSABLE; 40 /* 41 * 下面的flags属性的效果形同“锁定”。 42 * 悬浮窗不可触摸,不接受任何事件,同时不影响后面的事件响应。 43 wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL 44 | LayoutParams.FLAG_NOT_FOCUSABLE 45 | LayoutParams.FLAG_NOT_TOUCHABLE; 46 */ 47 48 49 wmParams.gravity=Gravity.LEFT|Gravity.TOP; // 调整悬浮窗口至左上角 50 // 以屏幕左上角为原点,设置x、y初始值 51 wmParams.x=0; 52 wmParams.y=0; 53 54 // 设置悬浮窗口长宽数据 55 wmParams.width=40; 56 wmParams.height=40; 57 58 // 显示myFloatView图像 59 wm.addView(myFV, wmParams); 60 61 } 62 63 @Override 64 public void onDestroy(){ 65 super.onDestroy(); 66 // 在程序退出(Activity销毁)时销毁悬浮窗口 67 wm.removeView(myFV); 68 } 69 70 @Override 71 protected void onRestart() { 72 // TODO Auto-generated method stub 73 wm.removeView(myFV); 74 super.onRestart(); 75 } 76 79 @Override 80 protected void onStop() { 81 // TODO Auto-generated method stub 82 // 创建悬浮窗口 83 createView(); 84 super.onStop(); 85 }
//浮动窗口内容类
1 public class MyFloatView extends ImageView { 2 private float mTouchStartX; 3 private float mTouchStartY; 4 private float x; 5 private float y; 6 8 private WindowManager wm=(WindowManager)getContext().getApplicationContext().getSystemService("window"); 9 10 // 此wmParams为获取的全局变量,用以保存悬浮窗口的属性 11 private WindowManager.LayoutParams wmParams = ((MyApplication)getContext().getApplicationContext()).getMywmParams(); 12 13 public MyFloatView(Context context) { 14 super(context); 15 // TODO Auto-generated constructor stub 16 } 17 18 @Override 19 public boolean onTouchEvent(MotionEvent event) { 20 21 // getRawX()获取相对屏幕的坐标,即以屏幕左上角为原点 22 x = event.getRawX(); 23 y = event.getRawY()-25; // 25是系统状态栏的高度 24 Log.i("currP", "currX"+x+"====currY"+y); 25 switch (event.getAction()) { 26 case MotionEvent.ACTION_DOWN: 27 // getX()获取相对View的坐标,即以此View左上角为原点 28 mTouchStartX = event.getX(); 29 mTouchStartY = event.getY(); 30 31 Log.i("startP", "startX"+mTouchStartX+"====startY"+mTouchStartY); 32 33 break; 34 case MotionEvent.ACTION_MOVE: 35 updateViewPosition(); 36 break; 37 38 case MotionEvent.ACTION_UP: 39 updateViewPosition(); 40 mTouchStartX=mTouchStartY=0; 41 break; 42 } 43 return true; 44 } 45 46 private void updateViewPosition(){ 47 // 更新浮动窗口位置参数,x是鼠标在屏幕的位置,mTouchStartX是鼠标在图片的位置 48 wmParams.x=( int)( x-mTouchStartX); 49 System.out.println(mTouchStartX); 50 wmParams.y=( int) (y-mTouchStartY); 51 wm.updateViewLayout( this, wmParams); 52 53 } 54 55 }
在androidManifest中设置:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />