Android Webview中的视屏全屏功能实现
2016-07-04 本文已影响2534人
燊在锦官城
Webview中的视屏全屏的时候是会出现问题的,我们对其做如下的修改:
- 设置webview的WebChromeClient
WebSettings webSettings = mWebview.getSettings();
mWebview.setWebChromeClient(new MyWebChromeClient());
- 重写WebChromeClient的onShowCustomView和onHideCustomView方法
class MyWebChromeClient extends WebChromeClient {
private View myView = null;
// 全屏
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
ViewGroup parent = (ViewGroup) mWebview.getParent();
parent.removeView(mWebview);
// 设置背景色为黑色
view.setBackgroundColor(mActivity.getResources().getColor(R.color.black));
parent.addView(view);
myView = view;
setFullScreen();
titleBar.setVisibility(View.GONE);
}
// 退出全屏
@Override
public void onHideCustomView() {
super.onHideCustomView();
if (myView != null) {
ViewGroup parent = (ViewGroup) myView.getParent();
parent.removeView(myView);
parent.addView(mWebview);
myView = null;
titleBar.setVisibility(View.VISIBLE);
quitFullScreen();
}
}
}
/**
* 设置全屏
*/
private void setFullScreen() {
// 设置全屏的相关属性,获取当前的屏幕状态,然后设置全屏
mActivity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
/**
* 退出全屏
*/
private void quitFullScreen() {
// 声明当前屏幕状态的参数并获取
final WindowManager.LayoutParams attrs = mActivity.getWindow().getAttributes();
attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
mActivity.getWindow().setAttributes(attrs);
mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
以上。
参考:
http://blog.csdn.net/lygscg123/article/details/38084769