您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)怎么中Android中自定義一個(gè)懸浮窗控件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
第一步設(shè)計(jì)類似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(); } /** * 顯示浮動(dòng)窗口 * @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); } /** * 顯示浮動(dòng)窗口 * @param view * @param x * @param y * @param listener 窗體之外的監(jiān)聽 * @param backListener 返回鍵盤監(jiān)聽 */ 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); } /** * 關(guān)閉界面 */ 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); } } } }
大家可能會(huì)注意到
// 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;
這些設(shè)置有所不同,這就是我們要實(shí)現(xiàn)既能夠監(jiān)聽窗口之外的觸目事件,又不會(huì)影響他們自己的操作的關(guān)鍵地方 ,同時(shí)| WindowManager.LayoutParams. FLAG_NOT_FOCUSABLE ;和| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; 窗體是否監(jiān)聽到返回鍵的關(guān)鍵設(shè)置 需要指出的是一旦窗體監(jiān)聽到返回鍵事件,則當(dāng)前Activity不會(huì)再監(jiān)聽到返回按鈕事件了,所以大家可根據(jù)自己的實(shí)際情況出發(fā)做出選擇。
為了方便管理這些浮動(dòng)窗口的顯示和消失,還寫了一個(gè)管理窗口顯示的類FloatWindowManager。這是一個(gè)單例模式 對應(yīng)的顯示窗口也是只顯示一個(gè)。大家可以根據(jù)自己的需求是改變 這里不再明細(xì)。
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); }; }
看完上述內(nèi)容,你們對怎么中Android中自定義一個(gè)懸浮窗控件有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。