Android利用WindowManager生成懸浮按鈕及懸浮菜單
簡(jiǎn)介
本文模仿實(shí)現(xiàn)的是360手機(jī)衛(wèi)士基礎(chǔ)效果,同時(shí)后續(xù)會(huì)補(bǔ)充一些WindowManager的原理知識(shí)。
整體思路
360手機(jī)衛(wèi)士的內(nèi)存球其實(shí)就是一個(gè)沒(méi)有畫(huà)面的應(yīng)用程序,整個(gè)應(yīng)用程序的主體是一個(gè)Service。我們的程序開(kāi)始以后,啟動(dòng)一個(gè)service,同時(shí)關(guān)閉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默認(rèn)是運(yùn)行在UI線程中的,這點(diǎn)與封裝了一個(gè)Thread和Handler的intentService不同,所以我們可以直接在Service中更改UI相關(guān)的內(nèi)容。
再來(lái)看一下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除了加載了一個(gè)布局,就是設(shè)置了一個(gè)Touch監(jiān)聽(tīng)器,用于點(diǎn)擊懸浮窗彈出菜單。注意這里要使用 view.getLayoutParams() 來(lái)獲取視圖的寬和高,因?yàn)樵跇?gòu)造方法中,這個(gè)View并沒(méi)有被measure完成,所以采用view.getHeight得到的寬高是0。
創(chuàng)建菜單的方法類(lèi)似,同樣通過(guò)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); }
自定義的菜單將背景設(shè)置成半透明,同時(shí)分成上下兩部分,上部分點(diǎn)擊刪除菜單,下部分是一些展示的內(nèi)容:
<?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(); } }); } }
可以看見(jiàn),實(shí)現(xiàn)懸浮窗,其實(shí)就是通過(guò)windowManager.addView時(shí),在LayoutParam 的type設(shè)置為T(mén)YPE_PHONE,這樣你的視圖就是系統(tǒng)級(jí)視圖,可以覆蓋在全部程序的最上面。其余的,更多的是自定義View的知識(shí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開(kāi)發(fā)懸浮按鈕 Floating ActionButton的實(shí)現(xiàn)方法
- Android仿知乎懸浮功能按鈕FloatingActionButton效果
- Android自定義可拖拽的懸浮按鈕DragFloatingActionButton
- Android開(kāi)發(fā)之FloatingActionButton懸浮按鈕基本使用、字體、顏色用法示例
- android 應(yīng)用內(nèi)部懸浮可拖動(dòng)按鈕簡(jiǎn)單實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)系統(tǒng)級(jí)懸浮按鈕
- Android懸浮按鈕的使用方法
- Android懸浮窗按鈕實(shí)現(xiàn)點(diǎn)擊并顯示/隱藏多功能列表
- Android自定義APP全局懸浮按鈕
- Android自定義懸浮按鈕效果
相關(guān)文章
Android開(kāi)發(fā)之Android.mk模板的實(shí)例詳解
這篇文章主要介紹了Android開(kāi)發(fā)之Android.mk模板的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10Android studio設(shè)計(jì)簡(jiǎn)易計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android studio設(shè)計(jì)簡(jiǎn)易計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Android中ExpandableListView的用法實(shí)例
這篇文章主要介紹了Android中ExpandableListView的用法,以實(shí)例形式展示了Android中的下拉list控件的用法,需要的朋友可以參考下2014-10-10實(shí)例解析如何在Android應(yīng)用中實(shí)現(xiàn)彈幕動(dòng)畫(huà)效果
這篇文章主要介紹了如何在Android應(yīng)用中實(shí)現(xiàn)彈幕動(dòng)畫(huà)效果的實(shí)例,文中是利用RelativeLayout布局然后控制ViewGroup中view的顯示,細(xì)節(jié)展示得比較詳細(xì),需要的朋友可以參考下2016-04-04Android系統(tǒng)優(yōu)化Ninja加快編譯
這篇文章主要為大家介紹了Android系統(tǒng)優(yōu)化使用Ninja加快編譯示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-085種方法完美解決android軟鍵盤(pán)擋住輸入框方法詳解
我們?cè)陂_(kāi)發(fā)android APP中經(jīng)常會(huì)遇到鍵盤(pán)擋住輸入框的情況,必須先把鍵盤(pán)收起,再去獲取下面輸入框焦點(diǎn),這樣用戶體驗(yàn)也非常不好,今天就給大家介紹5種完美解決android鍵盤(pán)擋住輸入框的方法2018-03-03Android設(shè)置項(xiàng)目為系統(tǒng)APP方法
大家好,本篇文章講的是Android設(shè)置項(xiàng)目為系統(tǒng)APP介紹,感興趣的同學(xué)趕快來(lái)看一看吧,希望本篇文章對(duì)你起到幫助2021-11-11Android編程實(shí)現(xiàn)點(diǎn)擊EditText之外的控件隱藏軟鍵盤(pán)功能
這篇文章主要介紹了Android編程實(shí)現(xiàn)點(diǎn)擊EditText之外的控件隱藏軟鍵盤(pán)功能,涉及Android控件的功能、屬性及相關(guān)操作技巧,需要的朋友可以參考下2017-06-06