Activity清单加入
android:hardwareAccelerated = "true"
android:configChanges="keyboardHidden|orientation|screenSize"
使用方法
MyWebView.with(this).setParentView(parentView).loadUrl(url);
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import com.yzjdev.utils.DensityUtil;
import com.yzjdev.utils.ScreenUtil;
public class MyWebView extends LinearLayout {
Context mContext;
Activity mActivity;
private View mParentView;
ProgressBar progressBar;
WebView webView;
WebSettings webSettings;
public MyWebView(Context context) {
this(context, null);
}
public MyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
setOrientation(LinearLayout.VERTICAL);
initView();
initWebView();
addView(progressBar);
addView(webView);
}
private void initView() {
progressBar = new ProgressBar(mContext, null, android.R.attr.progressBarStyleHorizontal);
progressBar.setVisibility(View.GONE);
progressBar.setMax(100);
progressBar.setLayoutParams(new LinearLayout.LayoutParams(-1, DensityUtil.dp2px(2)));
webView = new WebView(mContext);
webView.setLayoutParams(new LinearLayout.LayoutParams(-1, -1));
}
private void initWebView() {
webSettings = webView.getSettings();
//开启支持javascript
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
//开启缓存
webSettings.setAppCacheEnabled(true);
//从5.0开始,默认不允许混合模式,https中不能加载http,需要则开启
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webSettings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new MyWebChromeClient());
}
public MyWebView with(Activity activity) {
mActivity = activity;
return this;
}
public MyWebView setParentView(View parentView) {
mParentView = parentView;
return this;
}
public void loadUrl(String url) {
webView.loadUrl(url);
}
public WebView getWebView() {
return webView;
}
class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String scheme=Uri.parse(url).getScheme();
if (scheme.equals("http") || scheme.equals("https")) {
return false;
}
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
class MyWebChromeClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressBar.setProgress(newProgress);
}
@Override
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
super.onShowCustomView(view, callback);
if (mCustomViewCallback != null) {
mCustomViewCallback.onCustomViewHidden();
mCustomViewCallback = null;
return;
}
mParentView.setVisibility(View.GONE);
((ViewGroup)mParentView.getParent()).addView(view, new ViewGroup.LayoutParams(-1, -1));
mCustomView = view;
mCustomViewCallback = callback;
ScreenUtil.setFullScreen(mActivity, true);
}
@Override
public void onHideCustomView() {
super.onHideCustomView();
if (mCustomView != null) {
if (mCustomViewCallback != null) {
mCustomViewCallback.onCustomViewHidden();
mCustomViewCallback = null;
}
if (mCustomView != null && mCustomView.getParent() != null) {
ViewGroup parent = (ViewGroup) mCustomView.getParent();
parent.removeView(mCustomView);
if (mParentView != null) {
mParentView.setVisibility(View.VISIBLE);
}
}
mCustomView = null;
}
ScreenUtil.setFullScreen(mActivity, false);
}
}
}
ScreentUtil.java
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.view.View;
import android.view.WindowManager;
public class ScreenUtil {
public static void setFullScreen(Activity activity, boolean fullScreen) {
if (fullScreen) {
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
// 全屏展示
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// 全屏显示,隐藏状态栏和导航栏,拉出状态栏和导航栏显示一会儿后消失。
activity.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
} else {
// 全屏显示,隐藏状态栏
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
}
}
} else {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}
}
网友评论