最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android利用WindowManager生成懸浮按鈕及懸浮菜單

 更新時間:2021年09月27日 10:16:22   作者:自在時刻  
這篇文章主要為大家詳細介紹了Android利用WindowManager生成懸浮按鈕及懸浮菜單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

簡介

本文模仿實現(xiàn)的是360手機衛(wèi)士基礎效果,同時后續(xù)會補充一些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:

<?xml version="1.0" encoding="utf-8"?>
<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);

  }

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

<?xml version="1.0" encoding="utf-8"?>
<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();
      }
    });
  }
}

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

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

相關文章

  • Flutter實現(xiàn)網絡請求的方法示例

    Flutter實現(xiàn)網絡請求的方法示例

    這篇文章主要介紹了Flutter實現(xiàn)網絡請求的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • Android如何獲取屏幕、狀態(tài)欄及標題欄的高度詳解

    Android如何獲取屏幕、狀態(tài)欄及標題欄的高度詳解

    在日常開發(fā)中,經常會遇到獲取屏幕高度、狀態(tài)欄高度等需求,所以下面這篇文章就給大家總結介紹了關于Android如何獲取屏幕、狀態(tài)欄及標題欄高度的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友們可以參考下。
    2017-10-10
  • Android實現(xiàn)單行標簽流式布局

    Android實現(xiàn)單行標簽流式布局

    這篇文章主要為大家詳細介紹了Android單行標簽流式布局,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • 移動端開發(fā)之Jetpack?Hilt技術實現(xiàn)解耦

    移動端開發(fā)之Jetpack?Hilt技術實現(xiàn)解耦

    Hilt的出現(xiàn)解決前兩點問題,因為Hilt是Dagger針對Android平臺的場景化框架,比如Dagger需要我們手動聲明注入的地方,而Android聲明的地方不都在onCreate()嗎,所以Hilt就幫我們做了,除此之外還做了很多事情
    2023-02-02
  • Android開發(fā)之注冊登錄方法示例

    Android開發(fā)之注冊登錄方法示例

    這篇文章主要介紹了Android開發(fā)的注冊登錄方法,是針對Android程序設計中版本兼容性的進一步完善,需要的朋友可以參考下
    2014-08-08
  • Android中自定義水平進度條樣式之黑色虛線

    Android中自定義水平進度條樣式之黑色虛線

    這篇文章主要介紹了Android中自定義水平進度條樣式之黑色虛線 的相關資料,需要的朋友可以參考下
    2016-03-03
  • Android實現(xiàn)滑動折疊Header全流程詳解

    Android實現(xiàn)滑動折疊Header全流程詳解

    這篇文章主要介紹了Android實現(xiàn)滑動折疊Header,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-11-11
  • android自定義View圓圈拖動

    android自定義View圓圈拖動

    這篇文章主要為大家詳細介紹了android自定義View圓圈拖動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Android?studio?利用共享存儲進行用戶的注冊和登錄驗證功能

    Android?studio?利用共享存儲進行用戶的注冊和登錄驗證功能

    這篇文章主要介紹了Android?studio?利用共享存儲進行用戶的注冊和登錄驗證功能,包括注冊頁面布局及登錄頁面功能,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2021-12-12
  • Android自定義view實現(xiàn)圓環(huán)效果實例代碼

    Android自定義view實現(xiàn)圓環(huán)效果實例代碼

    本文通過實例代碼給大家介紹了Android自定義view實現(xiàn)圓環(huán)效果,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07

最新評論

五华县| 会同县| 长白| 平凉市| 锦州市| 巴彦淖尔市| 南澳县| 桃江县| 宜春市| 梅河口市| 寿宁县| 会宁县| 安多县| 株洲市| 大安市| 固始县| 佛坪县| 临桂县| 隆化县| 习水县| 贡嘎县| 石柱| 洪雅县| 青海省| 京山县| 平乐县| 桃园县| 江都市| 剑川县| 黎平县| 景东| 左权县| 枣阳市| 兖州市| 获嘉县| 双峰县| 靖边县| 顺昌县| 家居| 武隆县| 屏南县|