webview 全屏横屏播放视频
程序员文章站
2022-03-19 16:17:09
参考了这两位大神的博客:android Webview播放视频全屏问题webview播放视频并且点击全屏横屏的方法两个博客的总结:xml需要写两个FrameLayout第一个:放置视频到FrameLayout。(fl_full_video)第二个:webview的父布局。(fl_webview)
参考了这两位大神的博客:
android Webview播放视频全屏问题
webview播放视频并且点击全屏横屏的方法
两个博客的总结:
xml需要写两个FrameLayout
第一个:放置视频到FrameLayout。(fl_full_video)
第二个:webview的父布局。(fl_webview)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fl_webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/web_playback_live"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
<FrameLayout
android:id="@+id/fl_full_video"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:id="@+id/back_playback_live"
android:layout_width="@dimen/dp_100"
android:layout_height="@dimen/dp_40"/>
<RelativeLayout
android:id="@+id/share_live"
android:layout_width="@dimen/dp_100"
android:layout_height="@dimen/dp_40"
android:layout_alignParentRight="true"/>
<include layout="@layout/avi_layout" />
</RelativeLayout>
代码:
webPlaybackDetails.setWebChromeClient(new WebChromeClient() {
private View myView = null;
//监听加载进度
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if (newProgress < 100) {
avi.show();
} else {
avi.hide();
}
}
// 全屏
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
ViewGroup parent = (ViewGroup) webPlaybackDetails.getParent();
parent.removeView(webPlaybackDetails);
fl_full_video.addView(view);
fl_full_video.setVisibility(View.VISIBLE);
myView = view;
setFullScreen();
}
// 退出全屏
@Override
public void onHideCustomView() {
super.onHideCustomView();
if (myView != null) {
fl_full_video.removeAllViews();
fl_webview.addView(webPlaybackDetails);
fl_full_video.setVisibility(View.GONE);
myView = null;
quitFullScreen();
}
}
});
/**
* 设置全屏
*/
@SuppressLint("SourceLockedOrientationActivity")
private void setFullScreen() {
// 设置全屏的相关属性,获取当前的屏幕状态,然后设置全屏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
/**
* 退出全屏
*/
@SuppressLint("SourceLockedOrientationActivity")
private void quitFullScreen() {
// 声明当前屏幕状态的参数并获取
final WindowManager.LayoutParams attrs = this.getWindow().getAttributes();
attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.getWindow().setAttributes(attrs);
this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
清单文件对应 Activity 的配置:
android:hardwareAccelerated="true"
android:configChanges="orientation|keyboardHidden|screenSize"
最后销毁webview:
@Override
protected void onDestroy() {
super.onDestroy();
webPlaybackDetails.destroy();
}
题外话:webview逐级返回
/**
* 调用系统返回键 逐级返回
*
* @param keyCode
* @param event
* @return
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (webPlaybackDetails.canGoBack()) {
webPlaybackDetails.goBack();// 返回前一个页面
} else {
finish();
}
}
return false;
}
本文地址:https://blog.csdn.net/a506656675/article/details/110552706