国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android自定義覆蓋層控件 懸浮窗控件

2019-10-23 18:34:58
字體:
供稿:網(wǎng)友

在我們移動(dòng)應(yīng)用開發(fā)過程中,偶爾有可能會(huì)接到這種需求:

1、在手機(jī)桌面創(chuàng)建一個(gè)窗口,類似于360的懸浮窗口,點(diǎn)擊這個(gè)窗口可以響應(yīng)(至于窗口拖動(dòng)我們可以后面再擴(kuò)展)。

2、自己開發(fā)的應(yīng)用去啟動(dòng)一個(gè)非本應(yīng)用B,在B應(yīng)用的某個(gè)界面增加一個(gè)引導(dǎo)窗口。

3、在應(yīng)用的頁面上觸發(fā)啟動(dòng)這個(gè)窗口,該窗口懸浮在這個(gè)頁面上,但又不會(huì)影響界面的其他操作。即不像PopupWindow那樣要么窗口消失要么頁面不可響應(yīng)

以上需求都有幾個(gè)共同特點(diǎn),1、窗口的承載頁面不一定不是本應(yīng)用頁面(Activity),即不是類似dialog, PopupWindow之類的頁面。2、窗口的顯示不會(huì)影響用戶對(duì)其他界面的操作。

根據(jù)以上特點(diǎn),我們發(fā)現(xiàn)這類的窗口其不影響其他界面操作特點(diǎn)有點(diǎn)像Toast,但又不完全是,因?yàn)門oast是自己消失的。其界面可以恒顯示又有點(diǎn)像popupwindow,只當(dāng)調(diào)用了消失方法才會(huì)消失。所以我們?cè)谧鲞@樣的控件的時(shí)候可以去參考一下Toast和PopupWIndow如何實(shí)現(xiàn)。最主要的時(shí)候Toast。好了說了這么多大概的思路我們已經(jīng)明白了。

透過Toast,PopupWindow源碼我們發(fā)現(xiàn),Toast,Popup的實(shí)現(xiàn)都是通過windowManager的addview和removeView以及通過設(shè)置LayoutParams實(shí)現(xiàn)的。因此后面設(shè)計(jì)就該從這里入手,廢話不說了----去實(shí)現(xiàn)。

第一步設(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對(duì)象文職   *   * @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è)單例模式 對(duì)應(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);  }; } 

還有設(shè)計(jì)類似懸浮球的窗口等 大家可以自己運(yùn)行一遍比這里看千遍更有用。

附件:Android浮動(dòng)窗口

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 扎鲁特旗| 鹤峰县| 蓬莱市| 开远市| 白水县| 绥化市| 故城县| 鄯善县| 张掖市| 阳山县| 晴隆县| 察哈| 宾川县| 共和县| 静海县| 仪征市| 新津县| 阳江市| 曲沃县| 杭锦后旗| 桃园市| 海口市| 抚松县| 谢通门县| 界首市| 张家港市| 盈江县| 彰化市| 报价| 霍邱县| 遂昌县| 高邮市| 万全县| 分宜县| 荥阳市| 灵璧县| 安庆市| 应城市| 横峰县| 鄢陵县| 宜兴市|