前序
按項目交互要求,需要把視頻通話界面漓雅,縮小至懸浮窗顯示衣迷,基本實現(xiàn)思路這個比較好想,就是啟用一個service邮破,在里面用WindowManager去addView來展示懸浮窗畫面诈豌。基本效果是有了抒和,但填坑之路才剛開始矫渔。。
坑一:WindowManager.LayoutParam.type的選取
選擇TYPE_TOAST摧莽,如果期間有toast彈出庙洼,在android7.1.1會崩潰
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
}
注意:在android9.0以上就算按照上面方法設(shè)置,如果不提前授予懸浮窗權(quán)限,也會崩潰
坑二:SingleInstance和onActivityResult搭配問題
當(dāng)處于懸浮窗口時油够,此時通話界面會推到后臺蚁袭,要調(diào)用moveTaskToBack(true),如果通話的Activity的啟動模式不設(shè)置為SingleInstance石咬,就會導(dǎo)致整個應(yīng)用退到后臺揩悄,不是我們想要的效果,為了只把VIdeoCallActivity(通話界面)退到后臺鬼悠,那么此界面就必須得在單獨的一個棧里删性,所以要設(shè)置為SingleInstance。
用一個啟動模式為SingleInstance的Activity去打開一個界面焕窝,在onActivityResult是拿不到結(jié)果的蹬挺,所以只能用startActivity,然后再VIdeoCallActivity判斷懸浮是否已打開它掂,如果打開汗侵,則懸浮顯示。
坑三:跳轉(zhuǎn)開啟懸浮窗設(shè)置界面的機型適配問題
適配了市面上大部分廠商機型群发,小米晰韵、華為、魅族熟妓、oppo雪猪、vivo、360起愈、錘子
跳轉(zhuǎn)開啟權(quán)限工具類
懸浮窗服務(wù)FloatVideoWindowService
FloatVideoWindowService extends Service {
private static final String TAG = "FloatVideoWindowService";
private WindowManager mWindowManager;
private WindowManager.LayoutParams wmParams;
private View mFloatingLayout;
private RelativeLayout smallSizePreviewLayout;
private BaseVCManager mVCManager;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
public class MyBinder extends Binder {
public FloatVideoWindowService getService() {
return FloatVideoWindowService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
initWindow();//設(shè)置懸浮窗基本參數(shù)(位置只恨、寬高等)
initFloating();//懸浮框點擊事件的處理
mVCManager = VideoChatStateManager.getInstance().getManager();
initSurface();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
LogUtil.d(TAG, "onDestroy");
if (mFloatingLayout != null) {
// 移除懸浮窗口
mWindowManager.removeView(mFloatingLayout);
}
}
/**
* 初始化預(yù)覽窗口
*/
private synchronized void initSurface() {
if (mVCManager != null) {
mVCManager.switchToFloatWindow(this, smallSizePreviewLayout);
}
}
/**
* 設(shè)置懸浮框基本參數(shù)(位置、寬高等)
*/
private void initWindow() {
mWindowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
wmParams = getParams();
// 懸浮窗默認顯示以左上角為起始坐標(biāo)
wmParams.gravity = Gravity.RIGHT | Gravity.TOP;
//懸浮窗的開始位置抬虽,因為設(shè)置的是從左上角開始官觅,所以屏幕左上角是x=0;y=0
wmParams.x = AndroidUtil.dpToPx(this, 10);
wmParams.y = 110;
//得到容器,通過這個inflater來獲得懸浮窗控件
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
// 獲取浮動窗口視圖所在布局
mFloatingLayout = inflater.inflate(R.layout.view_videochat_services_float_layout, null);
// 添加懸浮窗的視圖
mWindowManager.addView(mFloatingLayout, wmParams);
}
private WindowManager.LayoutParams getParams() {
wmParams = new WindowManager.LayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
}
//wmParams.windowAnimations = R.style.default_style;
//設(shè)置可以顯示在狀態(tài)欄上
wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams
.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
int fullCropWidth = ScreenUtil.getPxWidth(this);
int cropHeight = fullCropWidth * 4 / 3;
//設(shè)置懸浮窗口長寬數(shù)據(jù)
wmParams.width = (int) (fullCropWidth * 0.26);
wmParams.height = (int) (cropHeight * 0.26);
return wmParams;
}
private void initFloating() {
smallSizePreviewLayout = mFloatingLayout.findViewById(R.id.small_size_preview);
//懸浮框點擊事件
smallSizePreviewLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//HomeApi.startHomeActivity(FloatVideoWindowService.this);
Intent intent = new Intent(new Intent(FloatVideoWindowService.this, VideoChatActivity.class));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
//懸浮框觸摸事件阐污,設(shè)置懸浮框可拖動
smallSizePreviewLayout.setOnTouchListener(new FloatingListener());
}
/**
* 開始觸控的坐標(biāo)休涤,移動時的坐標(biāo)(相對于屏幕左上角的坐標(biāo))
*/
private int mTouchStartX;
private int mTouchStartY;
/**
* 開始時的坐標(biāo)和結(jié)束時的坐標(biāo)(相對于自身控件的坐標(biāo))
*/
private int mStartX;
private int mStartY;
/**
* 判斷懸浮窗口是否移動,這里做個標(biāo)記笛辟,防止移動后松手觸發(fā)了點擊事件
*/
private boolean isMove;
private class FloatingListener implements View.OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
isMove = false;
mTouchStartX = (int) event.getRawX();
mTouchStartY = (int) event.getRawY();
mStartX = (int) event.getX();
mStartY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
int mTouchCurrentX = (int) event.getRawX();
int mTouchCurrentY = (int) event.getRawY();
wmParams.x -= mTouchCurrentX - mTouchStartX;
wmParams.y += mTouchCurrentY - mTouchStartY;
mWindowManager.updateViewLayout(mFloatingLayout, wmParams);
mTouchStartX = mTouchCurrentX;
mTouchStartY = mTouchCurrentY;
break;
case MotionEvent.ACTION_UP:
int mStopX = (int) event.getX();
int mStopY = (int) event.getY();
if (Math.abs(mStartX - mStopX) >= 1 || Math.abs(mStartY - mStopY) >= 1) {
isMove = true;
}
break;
default:
LogUtil.d(TAG, "onTouch: other");
}
//如果是移動事件不觸發(fā)OnClick事件功氨,防止移動的時候一放手形成點擊事件
return isMove;
}
}
}