Android悬浮窗控件 Android自定义覆盖层控件 悬浮窗控件
lan_hz007 人气:0在我们移动应用开发过程中,偶尔有可能会接到这种需求:
1、在手机桌面创建一个窗口,类似于360的悬浮窗口,点击这个窗口可以响应(至于窗口拖动我们可以后面再扩展)。
2、自己开发的应用去启动一个非本应用B,在B应用的某个界面增加一个引导窗口。
3、在应用的页面上触发启动这个窗口,该窗口悬浮在这个页面上,但又不会影响界面的其他操作。即不像PopupWindow那样要么窗口消失要么页面不可响应
以上需求都有几个共同特点,1、窗口的承载页面不一定不是本应用页面(Activity),即不是类似dialog, PopupWindow之类的页面。2、窗口的显示不会影响用户对其他界面的操作。
根据以上特点,我们发现这类的窗口其不影响其他界面操作特点有点像Toast,但又不完全是,因为Toast是自己消失的。其界面可以恒显示又有点像popupwindow,只当调用了消失方法才会消失。所以我们在做这样的控件的时候可以去参考一下Toast和PopupWIndow如何实现。最主要的时候Toast。好了说了这么多大概的思路我们已经明白了。
透过Toast,PopupWindow源码我们发现,Toast,Popup的实现都是通过windowManager的addview和removeView以及通过设置LayoutParams实现的。因此后面设计就该从这里入手,废话不说了----去实现。
第一步设计类似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浮动窗口
加载全部内容