webview 中的视频应在 android 中的 onShowCustomView() 方法上全屏显示
Posted
技术标签:
【中文标题】webview 中的视频应在 android 中的 onShowCustomView() 方法上全屏显示【英文标题】:video in webview should display full screen on onShowCustomView() method in android 【发布时间】:2017-05-09 02:52:15 【问题描述】:我想全屏显示视频(水平全屏)横向.. 当 onShowCustomView() 方法被调用时,
在我的情况下发生的事情是视频以相同的方向显示,其中 onShowCustomView() 方法被调用...也就是说屏幕是纵向的,而 onShowCustomView() 被称为视频垂直全屏显示,我想要它应该水平全屏显示
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.Toast;
public class Main26Activity extends Activity
private WebView view6;
@Override
protected void onCreate(Bundle savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main26);
String url6 = "https://www.youtube.com/watch?v=MPK43O87atA";
view6=(WebView)findViewById(R.id.webView6);
view6.getSettings().setjavascriptEnabled(true);
view6.setWebViewClient(new WebViewClient());
view6.setWebViewClient(new Browser());
view6.setWebChromeClient(new MyWebClient());
if (savedInstanceState != null)
view6.restoreState(savedInstanceState);
else
view6.loadUrl(url6);
Context context = getApplicationContext();
CharSequence text = "please wait while episodes are loaded";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
public void onPause()
view6.onPause();
super.onPause();
class Browser
extends WebViewClient
Browser()
public boolean shouldOverrideUrlLoading(WebView paramWebView, String paramString)
paramWebView.loadUrl(paramString);
return true;
public class MyWebClient
extends WebChromeClient
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
protected FrameLayout mFullscreenContainer;
private int mOriginalOrientation;
private int mOriginalSystemUiVisibility;
public MyWebClient()
public Bitmap getDefaultVideoPoster()
if (Main26Activity.this == null)
return null;
return BitmapFactory.decodeResource(Main26Activity.this.getApplicationContext().getResources(), 2130837573);
public void onHideCustomView()
((FrameLayout)Main26Activity.this.getWindow().getDecorView()).removeView(this.mCustomView);
this.mCustomView = null;
Main26Activity.this.getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
Main26Activity.this.setRequestedOrientation(this.mOriginalOrientation);
this.mCustomViewCallback.onCustomViewHidden();
this.mCustomViewCallback = null;
public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback)
if (this.mCustomView != null)
onHideCustomView();
return;
this.mCustomView = paramView;
this.mOriginalSystemUiVisibility = Main26Activity.this.getWindow().getDecorView().getSystemUiVisibility();
this.mOriginalOrientation = Main26Activity.this.getRequestedOrientation();
this.mCustomViewCallback = paramCustomViewCallback;
((FrameLayout)Main26Activity.this.getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
Main26Activity.this.getWindow().getDecorView().setSystemUiVisibility(3846);
@Override
protected void onSaveInstanceState(Bundle outState )
super.onSaveInstanceState(outState);
view6.saveState(outState);
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
super.onRestoreInstanceState(savedInstanceState);
view6.restoreState(savedInstanceState);
public void onBackPressed()
if (view6.canGoBack())
view6.goBack();
else
super.onBackPressed();
【问题讨论】:
【参考方案1】:我刚刚遇到了你的问题,我用另一种方式来处理它。
其实onShowCustomView返回的view是一个VideoView或者它的父亲。它是垂直的,因为您的设备方向是纵向的。所以我们可以在全屏视图的容器上做点什么。只需将“横向”布局作为您的容器。
这是我的简单代码:
public class LandscapeFrameLayout extends FrameLayout
public LandscapeFrameLayout(Context context, AttributeSet attrs)
super(context, attrs);
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
//Attention: The arithmetic below is based on padding == 0.
final int count = getChildCount();
for (int i = 0; i < count; i++)
View child = getChildAt(i);
if (child != null)
child.setRotation(90);
int width = right - left;
int height = bottom - top;
child.setTranslationX((width - height) / 2);
child.setTranslationY((height - width) / 2);
super.onLayout(changed, 0, 0, bottom, right);
上面的代码所做的是将它的子元素旋转 90 度并调整那里的位置。
【讨论】:
以上是关于webview 中的视频应在 android 中的 onShowCustomView() 方法上全屏显示的主要内容,如果未能解决你的问题,请参考以下文章
android 6中的android webview youtube视频中的全屏模式问题
Android WebView 中的 HTML5 视频不一致
通过单击 Android Studio 中的按钮在 webview 中获取随机 Youtube 视频