溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何實現(xiàn)Android浮動窗口

發(fā)布時間:2020-07-30 09:18:06 來源:億速云 閱讀:679 作者:小豬 欄目:開發(fā)技術(shù)

小編這次要給大家分享的是如何實現(xiàn)Android浮動窗口,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

1.浮動窗口的實現(xiàn)原理

看到上圖的那個小Android圖標(biāo)了吧,它不會被其他組建遮擋,也可以響應(yīng)用戶的點擊和拖動事件,它的顯示和消失由WindowManager直接管理,它就是Android浮動窗口。Android浮動窗口的實現(xiàn)主要是靠WindowManager這個類。通過WindowManager類的addView(),updateViewLayout(),removeView()這幾個方法,我們可以直接在Window中添加,更新,移除View。

2.浮動窗口實現(xiàn)的具體步驟

1)既然浮動窗口的實現(xiàn)依賴與WindowManager,那么毫無疑問,我們得先拿到WindowManger對象。考慮到浮動窗口通常在應(yīng)用程序退出后依然顯示,所以我們需要在Service中實現(xiàn)浮動窗口的添加和更新,當(dāng)然別忘了提供給用戶一個取消浮動窗口的功能。

2)定義你要顯示的View??梢栽诓季治募卸x,也可以自定義視圖。

3)設(shè)置必要的參數(shù),其中有幾個比較重要的參數(shù)需要設(shè)置,具體請參考下面的代碼。

4)將View添加到Window中,接收并處理事件,更新View。

5)在Manifest中加入對應(yīng)的權(quán)限。<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

3.浮動窗口實現(xiàn)代碼

 package com.spreadst.floatwindow;
 import android.app.Service;
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.PixelFormat;
 import android.os.IBinder;
 import android.util.Log;
 import android.view.Gravity;
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.WindowManager;

 public class FloatWindowService extends Service {

  private static final TAG = "FloatWindowService";

  private WindowManager mWindowManager;
  private WindowManager.LayoutParams mLayoutParams;
  private LayoutInflater mLayoutInflater;
  private View mFloatView;
  private int mCurrentX;
  private int mCurrentY;
  private static int mFloatViewWidth = 50;
  private static int mFloatViewHeight = 80;
  @Override
  public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    //初始化WindowManager對象和LayoutInflater對象
    mWindowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    mLayoutInflater = LayoutInflater.from(this);
  }
  @Override
  public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
    Log.i(TAG, "onStart()");
    createView();
  }
  private void createView() {
    // TODO Auto-generated method stub
    //加載布局文件
    mFloatView = mLayoutInflater.inflate(R.layout.main, null);
    //為View設(shè)置監(jiān)聽,以便處理用戶的點擊和拖動
    mFloatView.setOnTouchListener(new OnFloatViewTouchListener());
    /*為View設(shè)置參數(shù)*/
    mLayoutParams = new WindowManager.LayoutParams();
    //設(shè)置View默認(rèn)的擺放位置
    mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
    //設(shè)置window type
    mLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
    //設(shè)置背景為透明
    mLayoutParams.format = PixelFormat.RGBA_8888;
    //注意該屬性的設(shè)置很重要,F(xiàn)LAG_NOT_FOCUSABLE使浮動窗口不獲取焦點,若不設(shè)置該屬性,屏幕的其它位置點擊無效,應(yīng)為它們無法獲取焦點
    mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    //設(shè)置視圖的顯示位置,通過WindowManager更新視圖的位置其實就是改變(x,y)的值
    mCurrentX = mLayoutParams.x = 50;
    mCurrentY = mLayoutParams.y = 50;
    //設(shè)置視圖的寬、高
    mLayoutParams.width = 100;
    mLayoutParams.height = 100;
    //將視圖添加到Window中
    mWindowManager.addView(mFloatView, mLayoutParams);
  }
  /*由于直接startService(),因此該方法沒用*/
  @Override
  public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
  }
  /*該方法用來更新視圖的位置,其實就是改變(LayoutParams.x,LayoutParams.y)的值*/
  private void updateFloatView() {
    mLayoutParams.x = mCurrentX;
    mLayoutParams.y = mCurrentY;
    mWindowManager.updateViewLayout(mFloatView, mLayoutParams);
  }
  /*處理視圖的拖動,這里只對Move事件做了處理,用戶也可以對點擊事件做處理,例如:點擊浮動窗口時,啟動應(yīng)用的主Activity*/
  private class OnFloatViewTouchListener implements View.OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
      // TODO Auto-generated method stub
      Log.i(TAG, "mCurrentX: " + mCurrentX + ",mCurrentY: "
          + mCurrentY + ",mFloatViewWidth: " + mFloatViewWidth
          + ",mFloatViewHeight: " + mFloatViewHeight);
      /*
       * getRawX(),getRawY()這兩個方法很重要。通常情況下,我們使用的是getX(),getY()來獲得事件的觸發(fā)點坐標(biāo),
       * 但getX(),getY()獲得的是事件觸發(fā)點相對與視圖左上角的坐標(biāo);而getRawX(),getRawY()獲得的是事件觸發(fā)點
       * 相對與屏幕左上角的坐標(biāo)。由于LayoutParams中的x,y是相對與屏幕的,所以需要使用getRawX(),getRawY()。
       */
      mCurrentX = (int) event.getRawX() - mFloatViewWidth;
      mCurrentY = (int) event.getRawY() - mFloatViewHeight;
      int action = event.getAction();
      switch (action) {
      case MotionEvent.ACTION_DOWN:
        break;
      case MotionEvent.ACTION_MOVE:
        updateFloatView();
        break;
      case MotionEvent.ACTION_UP:
        break;
      }
      return true;
    }
  }
 }

4.如何只在Launcher界面顯示浮動窗口

大家應(yīng)該都熟悉360安全衛(wèi)士的浮動窗口,它的浮動窗口只會在Launcher界面顯示,當(dāng)用戶切到其它界面,浮動窗口自動被移除了。

要實現(xiàn)該功能,我們就必須知道當(dāng)前所在的界面,如果只去監(jiān)聽Activity的category,那么我們只能知道什么時候進(jìn)入Launcher界面了,卻無法知道是否離開了Launcher界面。那么360是如何實現(xiàn)該功能呢?大家可以反編譯一下它的代碼。這里提供一種可行的方法,我們的目前其實很簡單,就是要知道當(dāng)前的Activity是否是Launcher界面的Activity。由于Activity是以堆棧的形式被管理的,因此,只要我們查看棧頂?shù)腁ctivity是否是Launcher的Activity即可。要獲取Activity的Task信息,需要在Manifest中添加對應(yīng)權(quán)限,<uses-permission android:name = “android.permission.GET_TASKS”/>。

private String getTopActivity(Context context) {
   //獲取ActivityManager對象
   ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE) ;
   /*
   * 拿到當(dāng)前正在運行的Task列表,該列表按照最近使用的時間順序排列,其中的參數(shù)表示需要返回的最大列表項數(shù)目。
   * 這里我們只需要拿到處于onResume狀態(tài)的Activity所在的Task。
   */
   List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1) ;
   if(runningTaskInfos != null) {
     //拿到該task中的棧頂Activity
     return (runningTaskInfos.get(0).topActivity).toString() ;
   } else {
     return null;
   }
 }

看完這篇關(guān)于如何實現(xiàn)Android浮動窗口的文章,如果覺得文章內(nèi)容寫得不錯的話,可以把它分享出去給更多人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI