在Android開發(fā)中使用WindowManager實現一個懸浮按鈕

這篇文章將為大家詳細講解有關在Android開發(fā)中使用WindowManager實現一個懸浮按鈕,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

創(chuàng)新互聯公司主營扎賚諾爾網站建設的網絡公司,主營網站建設方案,成都App制作,扎賚諾爾h5微信小程序定制開發(fā)搭建,扎賚諾爾網站營銷推廣歡迎扎賚諾爾等地區(qū)企業(yè)咨詢

效果:

在Android開發(fā)中使用WindowManager實現一個懸浮按鈕

整體思路

360手機衛(wèi)士的內存球其實就是一個沒有畫面的應用程序,整個應用程序的主體是一個Service。我們的程序開始以后,啟動一個service,同時關閉activity即可:

public class MainActivity extends Activity {
  private static final String TAG = MainActivity.class.getSimpleName();
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startService(new Intent(this, FloatWindowService.class));
    finish();
  }
}
import android.os.IBinder;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

public class FloatWindowService extends Service {
  private static final String TAG = FloatWindowService.class.getSimpleName();

  public FloatWindowService() {
  }
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "on start command");
    FloatWindowManager.instance(getApplicationContext()).createFloatWindow();
    return super.onStartCommand(intent, flags, startId);
  }

  @Override
  public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
  }

}

我們要注意的是,傳統(tǒng)的Service默認是運行在UI線程中的,這點與封裝了一個Thread和Handler的intentService不同,所以我們可以直接在Service中更改UI相關的內容。

再來看一下FloatWindowManager中的方法:

  public void createFloatWindow() {
    if (isWindowShowing()) return;
    WindowManager windowManager = getWindowManger(context);
    int screenWidth = windowManager.getDefaultDisplay().getWidth();
    int screenHeight = windowManager.getDefaultDisplay().getHeight();
    if (floatLayout == null) {
      floatLayout = new FloatLayout(context);
      if (smallLayoutParams == null) {
        smallLayoutParams = new WindowManager.LayoutParams();
        smallLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        smallLayoutParams.format = PixelFormat.RGBA_8888;
        smallLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
            | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        smallLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        smallLayoutParams.width = FloatLayout.viewWidth;
        smallLayoutParams.height = FloatLayout.viewHeight;
        smallLayoutParams.x = screenWidth;
        smallLayoutParams.y = screenHeight / 2;
      }
    }
    windowManager.addView(floatLayout,smallLayoutParams);
  }

以及自定義的View:

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/small_layout"
  android:background="@drawable/bg_small"
  android:orientation="vertical" android:layout_width="60dip"
  android:layout_height="25dip">
<TextView
  android:layout_width="match_parent"
  android:gravity="center"
  android:text="懸浮窗"
  android:layout_height="match_parent" />
</LinearLayout>
public class FloatLayout extends LinearLayout {
  public static int viewWidth;
  public static int viewHeight;
  private WindowManager windowManager;
  public FloatLayout(final Context context) {
    super(context);
    windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    LayoutInflater.from(context).inflate(R.layout.small_layout, this);
    View view = findViewById(R.id.small_layout);
    viewWidth = view.getLayoutParams().width;
    viewHeight = view.getLayoutParams().height;
    setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        FloatWindowManager.instance(context).createFloatMenu();
        return true;
      }
    });
  }

}

自定義的View除了加載了一個布局,就是設置了一個Touch監(jiān)聽器,用于點擊懸浮窗彈出菜單。注意這里要使用 view.getLayoutParams() 來獲取視圖的寬和高,因為在構造方法中,這個View并沒有被measure完成,所以采用view.getHeight得到的寬高是0。

創(chuàng)建菜單的方法類似,同樣通過WindowManager:

  public void createFloatMenu() {
    if (menuLayout != null) return;
    Log.d(TAG, "create float menu");
    WindowManager windowManager = getWindowManger(context);
    if (menuLayout == null){
      menuLayout = new MenuLayout(context);
      menuLayoutParams = new WindowManager.LayoutParams();
      menuLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
      menuLayoutParams.format = PixelFormat.RGBA_8888;

    }
    windowManager.addView(menuLayout,menuLayoutParams);

  }

自定義的菜單將背景設置成半透明,同時分成上下兩部分,上部分點擊刪除菜單,下部分是一些展示的內容:

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:background="#96000000"
  android:layout_height="match_parent">
<LinearLayout
  android:layout_width="match_parent"
  android:id="@+id/trans_part"
  android:orientation="horizontal"
  android:layout_weight="1"
  android:layout_height="0dp"></LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:background="@color/colorPrimary"
    android:layout_height="0dp">
    <TextView
      android:layout_width="match_parent"
      android:text="存放content"
      android:layout_height="match_parent" />

  </LinearLayout>
</LinearLayout>
public class MenuLayout extends LinearLayout {
  public MenuLayout(final Context context) {
    super(context);
    LayoutInflater.from(context).inflate(R.layout.transparent_layout,this);
    View view = findViewById(R.id.trans_part);
    view.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        FloatWindowManager.instance(context).removeMenuLayout();
      }
    });
  }
}

可以看見,實現懸浮窗,其實就是通過windowManager.addView 時,在LayoutParam 的type設置為TYPE_PHONE,這樣你的視圖就是系統(tǒng)級視圖,可以覆蓋在全部程序的最上面。其余的,更多的是自定義View的知識。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家

關于在Android開發(fā)中使用WindowManager實現一個懸浮按鈕就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網頁名稱:在Android開發(fā)中使用WindowManager實現一個懸浮按鈕
網頁鏈接:http://muchs.cn/article20/ghieco.html

成都網站建設公司_創(chuàng)新互聯,為您提供微信小程序、品牌網站建設、手機網站建設、云服務器、做網站網站營銷

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

手機網站建設