不多说了,直接上代码吧!!!
package com.example.suspendedwindowdemo;
import android.content.Context;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;
import android.view.WindowManager;
/**
* 漂浮窗口触摸移动处理类
*/
public class FloatViewTouch {
private float mTouchStartX;
private float mTouchStartY;
private float x;
private float y;
private Context mContext;
private View mView;
private WindowManager windowManager;
private WindowManager.LayoutParams layoutParams;
private static boolean isShowing = false;
private static FloatViewTouch mFloatViewTouch;
public static synchronized FloatViewTouch getInstance(Context context) {
if (mFloatViewTouch == null) {
mFloatViewTouch = new FloatViewTouch(context);
}
return mFloatViewTouch;
}
private FloatViewTouch(Context context) {
init(context);
}
private void init(Context context) {
this.mContext = context;
this.windowManager = (WindowManager) mContext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
this.layoutParams = new LayoutParams();
this.layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
this.layoutParams.format = 1;
this.layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;// 不接受任何按键事件
this.layoutParams.flags = this.layoutParams.flags | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
this.layoutParams.flags = this.layoutParams.flags | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
this.layoutParams.alpha = 0.5f;
this.layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
this.layoutParams.x = 0;
this.layoutParams.y = 0;
this.layoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
this.layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
}
public void addView(View view) {
this.mView = view;
mView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
x = event.getRawX();
y = event.getRawY() - 38;// 减去状态栏高度
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mTouchStartX = event.getX();
mTouchStartY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
updateViewPosition();
break;
case MotionEvent.ACTION_UP:
updateViewPosition();
mTouchStartX = mTouchStartY = 0.0f;
break;
default:
break;
}
return true;
}
});
}
private void updateViewPosition() {
layoutParams.x = (int) (x - mTouchStartX);
layoutParams.y = (int) (y - mTouchStartY);
windowManager.updateViewLayout(mView, layoutParams);
}
public boolean isShowing() {
return isShowing;
}
public void showView() {
if (mView != null) {
isShowing = true;
windowManager.addView(mView, layoutParams);
}
}
public void removeView() {
if (mView != null) {
windowManager.removeView(mView);
mView = null;
isShowing = false;
}
}
}
调用方法(当然,可以在Activity,也可以在Service里面调用):
View view = LayoutInflater.from(this).inflate(R.layout.floatview, null);
FloatViewTouch.getInstance(getApplicationContext()).addView(view);
FloatViewTouch.getInstance(getApplicationContext()).showView();
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/flowing"
android:layout_width="fill_parent"
android:layout_height="32dip"
android:background="@android:color/darker_gray"
android:gravity="center_vertical"
android:paddingLeft="8dip"
android:text="提示文字"
android:textColor="@android:color/black" />
</LinearLayout>
另外,权限也是必不可少的啊
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
获取状态栏高度,需要在view绘制完成后获取,也就是在onResume方法里面
/**
* 获取状态栏高度
*
* @return
*/
private int getStatusBarHeight() {
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
return statusBarHeight;
}
/**
* 获取标题栏高度
*
* @return
*/
private int getTitleBarHeight() {
int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
// statusBarHeight是上面所求的状态栏的高度
int titleBarHeight = contentTop - getStatusBarHeight();
return titleBarHeight;
}