package com.zms.floatwindow;
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 Main extends Activity {
private WindowManager wm = null;
private WindowManager.LayoutParams wmParams = null;
private MyFloatView myFV = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
createView();
}
private void createView() {
myFV = new MyFloatView(getApplicationContext());
myFV.setImageResource(R.drawable.ic_launcher);
//获取WindowManager
wm = (WindowManager) getApplicationContext().getSystemService("window");
//设置LayoutParams(全局变量)相关参数
wmParams = ((MyApplication) getApplication()).getMywmParams();
/**
*以下都是WindowManager.LayoutParams的相关属性
* 具体用途可参考SDK文档
*/
wmParams.type = WindowManager.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.width = 40;
wmParams.height = 40;
//显示myFloatView图像
wm.addView(myFV, wmParams);
}
@Override
public void onDestroy() {
super.onDestroy();
//在程序退出(Activity销毁)时销毁悬浮窗口
wm.removeView(myFV);
}
@Override
protected void onRestart() {
wm.removeView(myFV);
super.onRestart();
}
@Override
protected void onStop() {
//创建悬浮窗口
//createView();
super.onStop();
}
}
package com.zms.floatwindow;
import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.ImageView;
/**
* Created by AlexZhou on 2015/3/27.
* 20:58
*/
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);
// TODO Auto-generated constructor stub
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//getRawX()获取相对屏幕的坐标,即以屏幕左上角为原点
x = event.getRawX();
y = event.getRawY() - 25; //25是系统状态栏的高度
Log.i("currP", "currX" + x + "====currY" + y);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//getX()获取相对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;
}
return true;
}
private void updateViewPosition() {
//更新浮动窗口位置参数,x是鼠标在屏幕的位置,mTouchStartX是鼠标在图片的位置
wmParams.x = (int) (x - mTouchStartX);
System.out.println(mTouchStartX);
wmParams.y = (int) (y - mTouchStartY);
wm.updateViewLayout(this, wmParams);
}
}
原文地址:http://blog.csdn.net/zhoumushui/article/details/44760469