Android仿微信視屏懸浮窗效果
在項(xiàng)目中需要對接入的騰訊云音視頻,可以懸浮窗顯示,懸浮窗可拖拽,并且在懸浮窗不影響其他的activity的焦點(diǎn)。
這個(gè)大神的文章Android基于騰訊云實(shí)時(shí)音視頻仿微信視頻通話最小化懸浮,他講的是視頻通話時(shí),將遠(yuǎn)端視頻以懸浮窗形式展示,根據(jù)他的代碼我進(jìn)行了部分簡化
1.懸浮窗效果:點(diǎn)擊縮小按鈕,將當(dāng)前遠(yuǎn)端視屏加載進(jìn)懸浮窗,且懸浮窗可拖拽,不影響其他界面焦點(diǎn);點(diǎn)擊懸浮窗可返回原來的Activity
2.實(shí)現(xiàn)懸浮窗需要:
在androidManifest中申請懸浮窗權(quán)限<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
在androidManifest中注冊FloatWindowService
3.視屏activity實(shí)現(xiàn):
-將activity置于后臺關(guān)鍵代碼:moveTaskToBack(true);//將activity置于后臺
-開啟懸浮窗
/** * 定義服務(wù)綁定的回調(diào) 開啟視頻通話服務(wù)連接 */ private ServiceConnection mVideoCallServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { // 獲取服務(wù)的操作對象 FloatWindowService.MyBinder binder = (FloatWindowService.MyBinder) service; binder.getService(); } @Override public void onServiceDisconnected(ComponentName name) { } }; /* * 開啟懸浮Video服務(wù) */ private void startVideoService() { //最小化Activity moveTaskToBack(true);//將activity置于后臺 //開啟服務(wù)顯示懸浮框 Intent serviceVideoIntent = new Intent(this, FloatWindowService.class); mServiceBound = bindService(serviceVideoIntent, mVideoCallServiceConnection, Context.BIND_AUTO_CREATE);//綁定Service }
-懸浮窗結(jié)束時(shí)
//在onDestroy()與onReStart()中解綁并銷毀相關(guān)內(nèi)容 if (mServiceBound) { unbindService(mVideoCallServiceConnection);//解綁 mServiceBound = false; }
4.懸浮窗實(shí)現(xiàn)相關(guān)代碼:
/** * 視頻懸浮窗服務(wù) */ public class FloatWindowService extends Service implements View.OnTouchListener { private WindowManager mWindowManager; private WindowManager.LayoutParams wmParams; private LayoutInflater inflater; //浮動布局view private View mFloatingLayout; //容器父布局 private View mMainVIew; //開始觸控的坐標(biāo),移動時(shí)的坐標(biāo)(相對于屏幕左上角的坐標(biāo)) private int mTouchStartX, mTouchStartY, mTouchCurrentX, mTouchCurrentY; //開始時(shí)的坐標(biāo)和結(jié)束時(shí)的坐標(biāo)(相對于自身控件的坐標(biāo)) private int mStartX, mStartY, mStopX, mStopY; //判斷懸浮窗口是否移動,這里做個(gè)標(biāo)記,防止移動后松手觸發(fā)了點(diǎn)擊事件 private boolean isMove; @Override public void onCreate() { super.onCreate(); initWindow();//設(shè)置懸浮窗基本參數(shù)(位置、寬高等) } @Nullable @Override public IBinder onBind(Intent intent) { currentBigUserId = intent.getStringExtra("localUserId"); remoteUserId = intent.getStringExtra("remoteUserId"); initFloating();//懸浮框點(diǎn)擊事件的處理 return new MyBinder(); } public class MyBinder extends Binder { public FloatWindowService getService() { return FloatWindowService.this; } } @Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); if (mFloatingLayout != null) { // 移除懸浮窗口 mWindowManager.removeView(mFloatingLayout); mFloatingLayout = null; } } /** * 設(shè)置懸浮框基本參數(shù)(位置、寬高等) */ private void initWindow() { mWindowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); //設(shè)置好懸浮窗的參數(shù) wmParams = getParams(); // 懸浮窗默認(rèn)顯示以左上角為起始坐標(biāo) wmParams.gravity = Gravity.RIGHT | Gravity.TOP; //懸浮窗的開始位置,因?yàn)樵O(shè)置的是從右上角開始,所以屏幕左上角是x=屏幕最大值;y=0 wmParams.x = 10; wmParams.y = 120; //得到容器,通過這個(gè)inflater來獲得懸浮窗控件 inflater = LayoutInflater.from(getApplicationContext()); // 獲取浮動窗口視圖所在布局 mFloatingLayout = inflater.inflate(R.layout.dlg_floatview, 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; } //設(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; //設(shè)置懸浮窗口長寬數(shù)據(jù) wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT; wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT; return wmParams; } //加載遠(yuǎn)端視屏:在這對懸浮窗內(nèi)內(nèi)容做操作 private void initFloating() { //將子View加載進(jìn)懸浮窗View mMainView = mFloatingLayout.findViewById(R.id.trtc_video_view_layout_float);//懸浮窗父布局 View mChildView = renderView.getChildView();//加載進(jìn)懸浮窗的子View,這個(gè)VIew來自天轉(zhuǎn)過來的那個(gè)Activity里面的那個(gè)需要加載的View mMainView.addView(mChildView);//將需要懸浮顯示的Viewadd到mTXCloudVideoView中 //懸浮框觸摸事件,設(shè)置懸浮框可拖動 mTXCloudVideoView.setOnTouchListener(this::onTouch); //懸浮框點(diǎn)擊事件 mTXCloudVideoView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //在這里實(shí)現(xiàn)點(diǎn)擊重新回到Activity Intent intent = new Intent(FloatWindowService.this, RtcActivity.class);//從該service跳轉(zhuǎn)至該activity會將該activity從后臺喚醒,所以activity會走onReStart() intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//從Service跳轉(zhuǎn)至RTCActivity,需要Intent.FLAG_ACTIVITY_NEW_TASK,不然會崩潰 startActivity(intent); } }); } //觸摸事件 @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: mTouchCurrentX = (int) event.getRawX(); mTouchCurrentY = (int) event.getRawY(); wmParams.x += mTouchStartX - mTouchCurrentX; wmParams.y += mTouchCurrentY - mTouchStartY; ALog.dTag("FloatingListener() onTouch",mTouchCurrentX,mTouchStartX,mTouchCurrentY,mTouchStartY); mWindowManager.updateViewLayout(mFloatingLayout, wmParams); mTouchStartX = mTouchCurrentX; mTouchStartY = mTouchCurrentY; break; case MotionEvent.ACTION_UP: mStopX = (int) event.getX(); mStopY = (int) event.getY(); if (Math.abs(mStartX - mStopX) >= 1 || Math.abs(mStartY - mStopY) >= 1) { isMove = true; } break; default: break; } //如果是移動事件不觸發(fā)OnClick事件,防止移動的時(shí)候一放手形成點(diǎn)擊事件 return isMove; } }
ps:使用Service做懸浮窗的載體是為了,將懸浮框的開啟關(guān)閉與服務(wù)Service的綁定解綁所關(guān)聯(lián)起來,開啟服務(wù)即相當(dāng)于開啟我們的懸浮框,解綁服務(wù)則相當(dāng)于關(guān)閉懸浮框,以此來達(dá)到更好的控制效果。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
AndroidStudio插件GsonFormat之Json快速轉(zhuǎn)換JavaBean教程
這篇文章主要介紹了AndroidStudio插件GsonFormat之Json快速轉(zhuǎn)換JavaBean教程,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Android DynamicGrid實(shí)現(xiàn)拖曳交換位置功能
這篇文章主要為大家詳細(xì)介紹了Android DynamicGrid實(shí)現(xiàn)拖曳交換位置功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android基于AccessibilityService制作的釘釘自動簽到程序代碼
這篇文章主要介紹了Android基于AccessibilityService制作的釘釘自動簽到程序代碼,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05Android?App頁面滑動標(biāo)題欄顏色漸變詳解
這篇文章主要為大家詳細(xì)介紹了Android?App頁面滑動標(biāo)題欄顏色漸變,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02用Android Studio3.0新功能加快構(gòu)建速度
本文主要介紹了使用Android Studio3.0新功能,加快Android Studio的構(gòu)建速度等相關(guān)做法。2017-11-11Android開發(fā)筆記之Android中數(shù)據(jù)的存儲方式(一)
這篇文章主要介紹了Android開發(fā)筆記之Android中數(shù)據(jù)的存儲方式(一) 的相關(guān)資料,需要的朋友可以參考下2016-01-01Android編程實(shí)現(xiàn)滑動按鈕功能詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)滑動按鈕功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android實(shí)現(xiàn)滑動按鈕的功能、布局及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-02-02Android下的POS打印機(jī)調(diào)用的簡單實(shí)現(xiàn)
本篇文章主要介紹了Android下的POS打印機(jī)調(diào)用的簡單實(shí)現(xiàn),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-04-04