android實(shí)現(xiàn)可自由移動(dòng)、監(jiān)聽點(diǎn)擊事件的懸浮窗
最近因?yàn)轫?xiàng)目需要,自己實(shí)現(xiàn)了個(gè)可以自由移動(dòng),并且長按可以跳出一個(gè)控制播放的,大的懸浮窗。
好,開始吧。首先我們先聊權(quán)限,懸浮窗需要在manifest中聲明一個(gè)權(quán)限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
然后呢,嗯,我們來講講關(guān)于懸浮窗實(shí)現(xiàn)的原理。
在Andriod中,所有的界面元素都要通過windowmanger來實(shí)現(xiàn),像Activity、Fragment等等這些也是在其上實(shí)現(xiàn)。因此,我們的懸浮窗自然要通過這個(gè)實(shí)現(xiàn)。
這個(gè)項(xiàng)目中,我們自定義了兩個(gè)懸浮窗view。我們以其中一個(gè)比較簡單的為例:
我們自定義一個(gè)管理可以統(tǒng)一管理懸浮窗的類MyWindowManager,負(fù)責(zé)創(chuàng)建,刪除懸浮窗
/**
* Created by shiwe on 2017/3/7.
* 懸浮窗管理
* 創(chuàng)建,移除
* 單例模式
*/
public class MyWindowManager {
private FloatNormalView normalView;
private FloatControlView controlView;
private static MyWindowManager instance;
private MyWindowManager() {
}
public static MyWindowManager getInstance() {
if (instance == null)
instance = new MyWindowManager();
return instance;
}
/**
* 創(chuàng)建小型懸浮窗
*/
public void createNormalView(Context context) {
if (normalView == null)
normalView = new FloatNormalView(context);
}
/**
* 移除懸浮窗
*
* @param context
*/
public void removeNormalView(Context context) {
if (normalView != null) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.removeView(normalView);
normalView = null;
}
}
/**
* 創(chuàng)建小型懸浮窗
*/
public void createControlView(Context context) {
if (controlView == null)
controlView = new FloatControlView(context);
}
/**
* 移除懸浮窗
*
* @param context
*/
public void removeControlView(Context context) {
if (controlView != null) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.removeView(controlView);
controlView = null;
}
}
}
然后看看我們自定義的一個(gè)view,其繼承自LinearLayout,我們在initLayoutParams初始化這個(gè)控件的位置等其他參數(shù);在initEvent方法中定義隨手指移動(dòng)的監(jiān)聽事件以及長按的監(jiān)聽事件。
public class FloatNormalView extends LinearLayout {
private Context context = null;
private View view = null;
private ImageView ivShowControlView = null;
private WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
private static WindowManager windowManager;
private float mTouchStartX;
private float mTouchStartY;
private float x;
private float y;
private boolean initViewPlace = false;
private MyWindowManager myWindowManager;
private boolean isControlViewShowing = false;
public FloatNormalView(Context context) {
super(context);
this.context = context;
myWindowManager = MyWindowManager.getInstance();
LayoutInflater.from(context).inflate(R.layout.float_normal_view, this);
view = findViewById(R.id.ll_float_normal);
ivShowControlView = (ImageView) findViewById(R.id.iv_show_control_view);
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
initLayoutParams();
initEvent();
}
/**
* 初始化參數(shù)
*/
private void initLayoutParams() {
//屏幕寬高
int screenWidth = windowManager.getDefaultDisplay().getWidth();
int screenHeight = windowManager.getDefaultDisplay().getHeight();
//總是出現(xiàn)在應(yīng)用程序窗口之上。
lp.type = WindowManager.LayoutParams.TYPE_PHONE;
// FLAG_NOT_TOUCH_MODAL不阻塞事件傳遞到后面的窗口
// FLAG_NOT_FOCUSABLE 懸浮窗口較小時(shí),后面的應(yīng)用圖標(biāo)由不可長按變?yōu)榭砷L按,不設(shè)置這個(gè)flag的話,home頁的劃屏?xí)袉栴}
lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
//懸浮窗默認(rèn)顯示的位置
lp.gravity = Gravity.START | Gravity.TOP;
//指定位置
lp.x = screenWidth - view.getLayoutParams().width * 2;
lp.y = screenHeight / 2 + view.getLayoutParams().height * 2;
//懸浮窗的寬高
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.format = PixelFormat.TRANSPARENT;
windowManager.addView(this, lp);
}
/**
* 設(shè)置懸浮窗監(jiān)聽事件
*/
private void initEvent() {
ivShowControlView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if (!isControlViewShowing) {
myWindowManager.createControlView(context);
isControlViewShowing = true;
} else {
myWindowManager.removeControlView(context);
isControlViewShowing = false;
}
return true;
}
});
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (!initViewPlace) {
initViewPlace = true;
//獲取初始位置
mTouchStartX += (event.getRawX() - lp.x);
mTouchStartY += (event.getRawY() - lp.y);
} else {
//根據(jù)上次手指離開的位置與此次點(diǎn)擊的位置進(jìn)行初始位置微調(diào)
mTouchStartX += (event.getRawX() - x);
mTouchStartY += (event.getRawY() - y);
}
break;
case MotionEvent.ACTION_MOVE:
// 獲取相對(duì)屏幕的坐標(biāo),以屏幕左上角為原點(diǎn)
x = event.getRawX();
y = event.getRawY();
updateViewPosition();
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
});
}
/**
* 更新浮動(dòng)窗口位置
*/
private void updateViewPosition() {
lp.x = (int) (x - mTouchStartX);
lp.y = (int) (y - mTouchStartY);
windowManager.updateViewLayout(this, lp);
}
最后,只需要在Activity中調(diào)用mywindowManager中調(diào)用createxxx方法就可以。
public class MainActivity extends AppCompatActivity {
MyWindowManager myWindowManager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWindowManager = MyWindowManager.getInstance();
myWindowManager.createNormalView(this.getApplicationContext());
}
}
最后,附上demo項(xiàng)目的下載地址: android實(shí)現(xiàn)懸浮窗
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Native.js獲取監(jiān)聽開關(guān)等操作Android藍(lán)牙設(shè)備實(shí)例代碼
- android監(jiān)聽View加載完成的示例講解
- android View 繪制完成監(jiān)聽的實(shí)現(xiàn)方法
- Android控件Spinner實(shí)現(xiàn)下拉列表及監(jiān)聽功能
- Android 7.0 監(jiān)聽網(wǎng)絡(luò)變化的示例代碼
- Android EditText 監(jiān)聽用戶輸入完成的實(shí)例
- android輸入框內(nèi)容改變的監(jiān)聽事件實(shí)例
- Android監(jiān)聽橫豎屏切換功能
- Android編程自定義View時(shí)添加自己的監(jiān)聽器示例
- Android Usb設(shè)備的監(jiān)聽(Dev)外設(shè)端口的判定以及耳機(jī)的插拔
相關(guān)文章
Android:下拉刷新+加載更多+滑動(dòng)刪除實(shí)例講解
本文主要講解 Android下拉刷新+加載更多+滑動(dòng)刪除的示例,這里整理了相關(guān)資料并附示例代碼供大家學(xué)習(xí)參考,有需要的小伙伴可以參考下2016-08-08
Android實(shí)現(xiàn)定時(shí)自動(dòng)靜音小助手
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)定時(shí)自動(dòng)靜音小助手,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android實(shí)現(xiàn)上拉加載更多以及下拉刷新功能(ListView)
這篇文章主要介紹了Android實(shí)現(xiàn)上拉加載更多功能以及下拉刷新功能的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android EditText 監(jiān)聽用戶輸入完成的實(shí)例
下面小編就為大家分享一篇Android EditText 監(jiān)聽用戶輸入完成的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02
Android自定義view實(shí)現(xiàn)圓形與半圓形菜單
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)圓形與半圓形菜單的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android中手機(jī)錄屏并轉(zhuǎn)換GIF的兩種方式
本文主要介紹了android中手機(jī)錄屏并轉(zhuǎn)換GIF的兩種方式,具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01
Android的簡單前后端交互(okHttp+springboot+mysql)
這篇文章主要介紹了Android的簡單前后端交互(okHttp+springboot+mysql),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Android 媒體開發(fā)之MediaPlayer狀態(tài)機(jī)接口方法實(shí)例解析
這篇文章主要介紹了Android 媒體開發(fā)之MediaPlayer狀態(tài)機(jī)接口方法實(shí)例解析,需要的朋友可以參考下2017-08-08
實(shí)例分析Android中HandlerThread線程用法
本篇文章主要給大家介紹了Android HandlerThread使用介紹以及源碼解析,有需要的朋友參考學(xué)習(xí)下吧。2017-12-12

