您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關怎么中Android中自定義一個懸浮窗控件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
第一步設計類似Toast的類FloatWindow
package com.floatwindowtest.john.floatwindowtest.wiget; import android.app.Activity; import android.content.Context; import android.graphics.PixelFormat; import android.view.Gravity; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.FrameLayout; import android.widget.LinearLayout; import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; import static android.view.WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; /** * Created by john on 2017/3/10. */ class FloatWindow { private final Context mContext; private WindowManager windowManager; private View floatView; private WindowManager.LayoutParams params; public FloatWindow(Context mContext) { this.mContext = mContext; this.params = new WindowManager.LayoutParams(); } /** * 顯示浮動窗口 * @param view * @param x view距離左上角的x距離 * @param y view距離左上角的y距離 */ void show(View view, int x, int y) { this.windowManager = (WindowManager) this.mContext.getSystemService(Context.WINDOW_SERVICE); params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.gravity = Gravity.TOP | Gravity.LEFT; params.format = PixelFormat.TRANSLUCENT; params.x = x; params.y = y; params.type = WindowManager.LayoutParams.TYPE_TOAST; params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | FLAG_NOT_FOCUSABLE | FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; floatView = view; windowManager.addView(floatView, params); } /** * 顯示浮動窗口 * @param view * @param x * @param y * @param listener 窗體之外的監聽 * @param backListener 返回鍵盤監聽 */ void show(View view, int x, int y, OutsideTouchListener listener, KeyBackListener backListener) { this.windowManager = (WindowManager) this.mContext.getSystemService(Context.WINDOW_SERVICE); final FloatWindowContainerView containerView = new FloatWindowContainerView(this.mContext, listener, backListener); containerView.addView(view, WRAP_CONTENT, WRAP_CONTENT); params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.gravity = Gravity.TOP | Gravity.LEFT; params.format = PixelFormat.TRANSLUCENT; params.x = x; params.y = y; params.type = WindowManager.LayoutParams.TYPE_TOAST; // // params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON // | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH // | WindowManager.LayoutParams. FLAG_NOT_FOCUSABLE ; params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; floatView = containerView; windowManager.addView(floatView, params); } /** * 更新view對象文職 * * @param offset_X x偏移量 * @param offset_Y Y偏移量 */ public void updateWindowLayout(float offset_X, float offset_Y) { params.x += offset_X; params.y += offset_Y; windowManager.updateViewLayout(floatView, params); } /** * 關閉界面 */ void dismiss() { if (this.windowManager == null) { this.windowManager = (WindowManager) this.mContext.getSystemService(Context.WINDOW_SERVICE); } if (floatView != null) { windowManager.removeView(floatView); } floatView = null; } public void justHideWindow() { this.floatView.setVisibility(View.GONE); } private class FloatWindowContainerView extends FrameLayout { private OutsideTouchListener listener; private KeyBackListener backListener; public FloatWindowContainerView(Context context, OutsideTouchListener listener, KeyBackListener backListener) { super(context); this.listener = listener; this.backListener = backListener; } @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (getKeyDispatcherState() == null) { if (backListener != null) { backListener.onKeyBackPressed(); } return super.dispatchKeyEvent(event); } if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { KeyEvent.DispatcherState state = getKeyDispatcherState(); if (state != null) { state.startTracking(event, this); } return true; } else if (event.getAction() == KeyEvent.ACTION_UP) { KeyEvent.DispatcherState state = getKeyDispatcherState(); if (state != null && state.isTracking(event) && !event.isCanceled()) { System.out.println("dsfdfdsfds"); if (backListener != null) { backListener.onKeyBackPressed(); } return super.dispatchKeyEvent(event); } } return super.dispatchKeyEvent(event); } else { return super.dispatchKeyEvent(event); } } @Override public boolean onTouchEvent(MotionEvent event) { final int x = (int) event.getX(); final int y = (int) event.getY(); if ((event.getAction() == MotionEvent.ACTION_DOWN) && ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) { return true; } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { if (listener != null) { listener.onOutsideTouch(); } System.out.println("dfdf"); return true; } else { return super.onTouchEvent(event); } } } }
大家可能會注意到
// params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON // | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH // | WindowManager.LayoutParams. FLAG_NOT_FOCUSABLE ; params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
這些設置有所不同,這就是我們要實現既能夠監聽窗口之外的觸目事件,又不會影響他們自己的操作的關鍵地方 ,同時| WindowManager.LayoutParams. FLAG_NOT_FOCUSABLE ;和| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; 窗體是否監聽到返回鍵的關鍵設置 需要指出的是一旦窗體監聽到返回鍵事件,則當前Activity不會再監聽到返回按鈕事件了,所以大家可根據自己的實際情況出發做出選擇。
為了方便管理這些浮動窗口的顯示和消失,還寫了一個管理窗口顯示的類FloatWindowManager。這是一個單例模式 對應的顯示窗口也是只顯示一個。大家可以根據自己的需求是改變 這里不再明細。
package com.floatwindowtest.john.floatwindowtest.wiget; import android.content.Context; import android.view.View; /** * * Created by john on 2017/3/10. */ public class FloatWindowManager { private static FloatWindowManager manager; private FloatWindow floatWindow; private FloatWindowManager(){ } public static synchronized FloatWindowManager getInstance(){ if(manager==null){ manager=new FloatWindowManager(); } return manager; } public void showFloatWindow(Context context, View view,int x,int y){ if(floatWindow!=null){ floatWindow.dismiss(); } floatWindow=new FloatWindow(context); floatWindow.show(view,x,y); } public void showFloatWindow(Context context, View view, int x, int y, OutsideTouchListener listener,KeyBackListener backListener){ if(floatWindow!=null){ floatWindow.dismiss(); } floatWindow=new FloatWindow(context); floatWindow.show(view,0,0,listener,backListener); } public void dismissFloatWindow(){ if(floatWindow!=null){ floatWindow.dismiss(); } } public void justHideWindow(){ floatWindow.justHideWindow(); } /** * 更新位置 * @param offsetX * @param offsetY */ public void updateWindowLayout(float offsetX, float offsetY){ floatWindow.updateWindowLayout(offsetX,offsetY); }; }
看完上述內容,你們對怎么中Android中自定義一個懸浮窗控件有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。