這篇文章主要介紹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, "當前無權限,請授權", 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如何實現仿優酷視頻的懸浮窗播放效果的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。