参考:(61条消息) Android WebView Video完全详解(第一篇)-Android开发人员_HarmonyOS Developer的博客-CSDN博客_webview video
前言:通常富文本是可能同时含图片和视频的,普通的富文本组件可能无法满足了,这时使用WebView可以解决。
使用:
不在布局中创建webview而是在代码中创建
//初始化
WebView wv = new WebView(this.context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
lp.leftMargin = AutoSizeUtils.dp2px(this.context,10f);
lp.rightMargin = AutoSizeUtils.dp2px(this.context,10f);
wv.setLayoutParams(lp);
setUpWebViewDefaults(wv);
setChromeClient(wv);
//这里entity.getTitle()就是你要放入的富文本
wv.loadDataWithBaseURL(null, entity.getTitle(), "text/html", "utf-8", null);
wv.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mWaitDialog.dismiss();
}
});
llHtml.addView(wv);
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setUpWebViewDefaults(WebView webView) {
WebSettings settings = webView.getSettings();
// Enable Javascript
settings.setJavaScriptEnabled(true);
// Use WideViewport and Zoom out if there is no viewport defined
settings.setUseWideViewPort(false); //原文模式
settings.setLoadWithOverviewMode(true);
// Enable pinch to zoom without the zoom buttons
settings.setBuiltInZoomControls(false);
// Allow use of Local Storage
settings.setDomStorageEnabled(true);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
// Hide the zoom controls for HONEYCOMB+
settings.setDisplayZoomControls(false);
}
// Enable remote debugging via chrome://inspect
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
webView.setWebViewClient(new WebViewClient());
}
//getDefaultVideoPoster该方法中有一个可以更换安卓视频组件视频默认图的方法,自己尝试了以下但是没用。
void setChromeClient(WebView mWebView) {
mWebView.setWebChromeClient(new WebChromeClient() {
private Bitmap xdefaltvideo;
@Override
public Bitmap getDefaultVideoPoster() { //这个地方是加载h5的视频列表 默认图 点击前的视频图
// if (this == null) {
// return null;
// }
// return BitmapFactory.decodeResource(activity.getApplicationContext().getResources(),
// R.mipmap.ic_launcher);
if (xdefaltvideo == null) {
xdefaltvideo = BitmapFactory.decodeResource(
activity.getResources(), R.mipmap.ic_launcher);
}
return xdefaltvideo;
}
@Override
public void onShowCustomView(View view,
WebChromeClient.CustomViewCallback callback) {
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
onHideCustomView();
return;
}
// 1. Stash the current state
mCustomView = view;
mOriginalSystemUiVisibility = activity.getWindow().getDecorView().getSystemUiVisibility();
mOriginalOrientation = activity.getRequestedOrientation();
// 2. Stash the custom view callback
mCustomViewCallback = callback;
// 3. Add the custom view to the view hierarchy
FrameLayout decor = (FrameLayout) activity.getWindow().getDecorView();
decor.addView(mCustomView, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
// 4. Change the state of the window
activity.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE);
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
@Override
public void onHideCustomView() {
// 1. Remove the custom view
FrameLayout decor = (FrameLayout) activity.getWindow().getDecorView();
decor.removeView(mCustomView);
mCustomView = null;
// 2. Restore the state to it's original form
activity.getWindow().getDecorView()
.setSystemUiVisibility(mOriginalSystemUiVisibility);
activity.setRequestedOrientation(mOriginalOrientation);
// 3. Call the custom view callback
mCustomViewCallback.onCustomViewHidden();
mCustomViewCallback = null;
}
});
}
网友评论