Android漂浮視窗

來源:互聯網
上載者:User

不多說了,直接上代碼吧!!!

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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.