Android仿優酷視訊的懸浮窗播放效果
之前接了需求要讓視訊播放時可以像優酷視訊那樣在懸浮窗裡播放,並且懸浮窗和主播放頁面之間要實現無縫切換,專案中使用的是自封裝的ijkplayer
這個要求就代表不能在懸浮窗中新建視訊控制元件,所以需要在懸浮窗中複用主頁面的視訊控制元件,以達到無縫銜接的效果。
主頁面對應的視訊控制元件的父view
<FrameLayout android:id="@+id/vw_live" 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:id="@+id/floating_video_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/floating_video" android:layout_width="match_parent" android:layout_height="match_parent"/> <ImageView android:id="@+id/close_floating_view" android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="end" android:padding="10dp" android:src="@android:drawable/ic_menu_close_clear_cancel" /> <ImageView android:id="@+id/back_floating_view" android:layout_width="50dp" android:layout_height="50dp" android:padding="10dp" android:src="@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,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 優酷視訊懸浮窗播放內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!