Android仿優酷視頻的懸浮窗播放效果
之前接了需求要讓視頻播放時可以像優酷視頻那樣在懸浮窗里播放,并且懸浮窗和主播放頁面之間要實現無縫切換,項目中使用的是自封裝的ijkplayer這個要求就代表不能在懸浮窗中新建視頻控件,所以需要在懸浮窗中復用主頁面的視頻控件,以達到無縫銜接的效果。
主頁面對應的視頻控件的父view
<FrameLayout android: android:layout_width='match_parent' android:layout_height='match_parent' android:layout_centerInParent='true'/>
用FrameLayout作為添加視頻控件的ParentView,通過addview方法將新建的播放器控件添加到父控件內部
vw_live = new IjkVideoView(this);
video_frame = findViewById(R.id.vw_live);video_frame.addView(vw_live);
主播放界面的啟動模式
播放主界面的activity的啟動模式不能為默認,因為我們要保證播放主界面在顯示懸浮窗的時候退到后臺,但是整個的應用不能退到后臺,所以activity的啟動模式改為singleInstance
android:launchMode='singleInstance'
退到后臺我們通過moveTaskToBack(true)方法;
moveTaskToBack(true);
可以讓播放界面退到后臺而整個應用不會退回后臺
權限請求
要使用懸浮窗需要申請權限
<uses-permission android:name='android.permission.SYSTEM_OVERLAY_WINDOW' />
if (!Settings.canDrawOverlays(this)) { Toast.makeText(this, '當前無權限,請授權', Toast.LENGTH_SHORT); startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse('package:' + getPackageName())), 2); }
懸浮窗
@SuppressLint('ClickableViewAccessibility') public void showFloatingWindowView(IjkVideoView view) { // 懸浮窗顯示視圖 LayoutInflater layoutInflater = LayoutInflater.from(activity); mShowView = layoutInflater.inflate(R.layout.video_floating_window_layout, null);; // 獲取系統窗口管理服務 mWindowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE); // 懸浮窗口參數設置及返回 mFloatParams = getParams(); //floatingWindow內部控件實例 init(view); // 設置窗口觸摸移動事件 mShowView.setOnTouchListener(new FloatViewMoveListener()); // 懸浮窗生成 mWindowManager.addView(mShowView, mFloatParams); } private void init(IjkVideoView viewGroup){ videoLayout = mShowView.findViewById(R.id.floating_video); videoLayout.removeAllViews(); if (viewGroup != null){ ijkVideoView = viewGroup; videoLayout.addView(ijkVideoView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT ,ViewGroup.LayoutParams.MATCH_PARENT)); } mBtnCloseFloatingWindow = mShowView.findViewById(R.id.close_floating_view); mBtnCloseFloatingWindow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); mBtnBackFloatingWindow = (ImageView)mShowView.findViewById(R.id.back_floating_view); mBtnBackFloatingWindow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } private WindowManager.LayoutParams getParams() { WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(); //設置懸浮窗口類型 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; } else { layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; } //設置懸浮窗口屬性 layoutParams.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; //設置懸浮窗口透明 layoutParams.format = PixelFormat.TRANSLUCENT; //設置懸浮窗口長寬數據 layoutParams.width = 500; layoutParams.height = 340; //設置懸浮窗顯示位置 layoutParams.gravity = Gravity.START | Gravity.TOP; layoutParams.x = 100; layoutParams.y = 100; return layoutParams; }
懸浮窗的xml,可通過自定義獲得自己想要的效果
<FrameLayout xmlns:android='http://schemas.android.com/apk/res/android' android: android:layout_width='match_parent' android:layout_height='match_parent'> <FrameLayout android: android:layout_width='match_parent' android:layout_height='match_parent'/> <ImageView android: android:layout_width='50dp' android:layout_height='50dp' android:layout_gravity='end' android:padding='10dp' android:src='http://www.lshqa.cn/bcjs/@android:drawable/ic_menu_close_clear_cancel' /> <ImageView android: android:layout_width='50dp' android:layout_height='50dp' android:padding='10dp' android:src='http://www.lshqa.cn/bcjs/@android:drawable/ic_menu_revert' /></FrameLayout>
懸浮窗的滑動,我們可以通過自定義點擊監聽實現
/** * 浮窗移動/點擊監聽 */ private class FloatViewMoveListener implements View.OnTouchListener { //開始觸控的坐標,移動時的坐標(相對于屏幕左上角的坐標) private int mTouchStartX; private int mTouchStartY; //開始時的坐標和結束時的坐標(相對于自身控件的坐標) private int mStartX, mStartY; //判斷懸浮窗口是否移動,這里做個標記,防止移動后松手觸發了點擊事件 private boolean isMove; @Override public boolean onTouch(View view, MotionEvent motionEvent) { int action = motionEvent.getAction(); int x = (int) motionEvent.getX(); int y = (int) motionEvent.getY(); switch (action) {case MotionEvent.ACTION_DOWN: isMove = false; mTouchStartX = (int) motionEvent.getRawX(); mTouchStartY = (int) motionEvent.getRawY(); mStartX = x; mStartY = y; break;case MotionEvent.ACTION_MOVE: int mTouchCurrentX = (int) motionEvent.getRawX(); int mTouchCurrentY = (int) motionEvent.getRawY(); mFloatParams.x += mTouchCurrentX - mTouchStartX; mFloatParams.y += mTouchCurrentY - mTouchStartY; mWindowManager.updateViewLayout(mShowView, mFloatParams); mTouchStartX = mTouchCurrentX; mTouchStartY = mTouchCurrentY; float deltaX = x - mStartX; float deltaY = y - mStartY; if (Math.abs(deltaX) >= 5 || Math.abs(deltaY) >= 5) { isMove = true; } break;case MotionEvent.ACTION_UP: break;default: break; } //如果是移動事件不觸發OnClick事件,防止移動的時候一放手形成點擊事件 return isMove; } }
懸浮窗的消失,在這里調用videoLayout.removeAllViews()是為了將復用的視頻控件的父View清空,返回主播放activity的時候調用addview方法不會再報 child view has Parent,you have to call removeView()的錯
public void dismiss() { if (mWindowManager != null && mShowView != null) { videoLayout.removeAllViews(); if (mShowView.getParent() != null){mWindowManager.removeView(mShowView); } } }
啟動懸浮窗
public videoFloatingWindow(Context context){ super(context); this.activity = context; }
對于懸浮窗的調用
用hasBind來記錄是否調用了懸浮窗
private void startFloatingWindow(){ if (!Settings.canDrawOverlays(this)) { Toast.makeText(this, '當前無權限,請授權', Toast.LENGTH_SHORT); startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse('package:' + getPackageName())), 2); } else { video_frame.removeView(vw_live); videoFloatingWindow.getInstance(this).showFloatingWindowView(vw_live); hasBind = true; moveTaskToBack(true); } }
注意
一.由于主界面activity使用了singleInstance啟動模式,所以從懸浮窗返回主界面activity時,要添加flag
Intent intent = new Intent(activity, activity.getClass());intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);activity.startActivity(intent);
二.當主界面的activity退回后臺,再重新進入主界面的時候,注意,不再調用onCreate方法,而是調用onNewIntent,所以重寫onNewIntent方法,重新進入主界面,懸浮窗消失
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); Log.d('RemoteView', '重新顯示了'); //不顯示懸浮框 if (hasBind){ videoFloatingWindow.getInstance(this).dismiss(); video_frame.removeAllViews(); if (vw_live != null){video_frame.addView(vw_live); } hasBind = false; } }
總結
到此這篇關于Android仿優酷視頻的懸浮窗播放的文章就介紹到這了,更多相關android 優酷視頻懸浮窗播放內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. 京東怎么購買禮品卡2. excel篩選不了的原因介紹3. 剪映怎么設置上層文本4. 如何開啟京東再次驗密5. 抖音怎么知道自己是不是被封號了?被封號了怎么申訴恢復?6. 剪映視頻局部靜音怎么設置7. 釘釘頭像掛件怎么設置8. wps怎么給文件加保密水印9. cdr衣服繪制10. 大眾點評收藏數量怎么看到
