不多說了,直接上代碼吧!!!
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() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubx = 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;}