今天就跟大家聊聊有关怎么中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中自定义一个悬浮窗控件有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。
新闻标题:怎么中Android中自定义一个悬浮窗控件
新闻来源:http://lswzjz.com/article/ppdjpi.html