今天就跟大家聊聊有关怎么中Android中自定义一个悬浮窗控件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。第一步设计类似Toast的类FloatWindowpackage 
今天就跟大家聊聊有关怎么中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; 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(); } 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); } 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); } 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; 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(); } public void updateWindowLayout(float offsetX, float offsetY){ floatWindow.updateWindowLayout(offsetX,offsetY); }; }
看完上述内容,你们对怎么中Android中自定义一个悬浮窗控件有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网精选频道,感谢大家的支持。
--结束END--
本文标题: 怎么中Android中自定义一个悬浮窗控件
本文链接: https://lsjlt.com/news/224675.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0