android webview loading,Android WebView+ProgressBar Loading...

import android.content.Context;

import android.os.Build;

import android.text.TextUtils;

import android.util.AttributeSet;

import android.view.View;

import android.webkit.WebChromeClient;

import android.webkit.WebSettings;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.LinearLayout;

import android.widget.ProgressBar;

/**

* File: WebLoadingView.java

* Author: ejiang

* Version: V100R001C01

* Create: 2017-12-13 10:21

*/

public class WebLoadingView extends LinearLayout {

private Context mContext;

private LayoutParams mParams;

private WebView mWebView;

public WebLoadingView(Context context) {

this(context, null);

}

public WebLoadingView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public WebLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

this.mContext = context;

setOrientation(LinearLayout.VERTICAL); //默认横向排列

init(attrs);

}

private void init(AttributeSet attrs) {

if (attrs != null) {

}

this.setWillNotDraw(false);

//设置进度条

mParams = new LayoutParams(LayoutParams.MATCH_PARENT, 5);

ProgressBar progressBar = new ProgressBar(mContext, null, android.R.attr.progressBarStyleHorizontal);

progressBar.setProgress(0);

progressBar.setMax(100);

progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.progress_bar_states));

addView(progressBar, mParams);

//设置WebView

mParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

mWebView = new WebView(mContext);

mWebView.setLayoutParams(mParams);

mWebView.setHorizontalScrollBarEnabled(false);//水平不显示

mWebView.setVerticalScrollBarEnabled(false); //垂直不显示

initWebViewSettings(mWebView);

initWebViewChromeClient(mWebView, progressBar);

initWebViewClient(mWebView);

addView(mWebView, mParams);

}

/**

* @param webView

*/

private void initWebViewClient(WebView webView) {

webView.setWebViewClient(new WebViewClient() {

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

if (!TextUtils.isEmpty(url) && (url.contains("http://") || url.contains("https://")))

return false;

else return true;

}

});

}

private boolean isPlay = false;//是否播放WebView music

public void setPlay(boolean play) {

isPlay = play;

}

private void initWebViewChromeClient(final WebView webView, final ProgressBar progressBar) {

webView.setWebChromeClient(new WebChromeClient() {

@Override

public void onProgressChanged(WebView view, int newProgress) {

progressBar.setVisibility(View.VISIBLE);

progressBar.setProgress(newProgress);

if (newProgress == 100) {

progressBar.setVisibility(View.GONE);

progressBar.setProgress(0);

if (isPlay) {

webView.loadUrl("javascript:audio.cutoff(" + true + ")");//暂停播放

}

}

super.onProgressChanged(view, newProgress);

}

});

}

public void destroy() {

if (mWebView != null) {

mWebView.clearCache(false); //清除缓存

mWebView.destroy();

}

}

public void loadUrl(String url) {

if (mWebView != null) {

mWebView.loadUrl(url);

}

}

//初始化webviewSettings

private void initWebViewSettings(WebView webView) {

WebSettings settings = webView.getSettings();

//支持获取手势焦点

webView.requestFocusFromTouch();

//自动播放音乐

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {

settings.setMediaPlaybackRequiresUserGesture(false);

}

// 不使用缓存

settings.setCacheMode(WebSettings.LOAD_NO_CACHE);

// 启用js交互

settings.setJavaScriptEnabled(true);

//注入js调用代码

// webView.addJavascriptInterface(new AndroidJavaScriptObject(this),

// "growingjs");

//支持插件

settings.setPluginState(WebSettings.PluginState.ON);

//设置适应屏幕

settings.setUseWideViewPort(true);

settings.setLoadWithOverviewMode(true);

//支持缩放

settings.setSupportZoom(true);

settings.setBuiltInZoomControls(true);

//隐藏原生的缩放控件

settings.setDisplayZoomControls(false);

//支持内容重新布局

settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

settings.supportMultipleWindows();

settings.setSupportMultipleWindows(true);

//设置缓存模式

settings.setDomStorageEnabled(true);

settings.setDatabaseEnabled(true);

settings.setCacheMode(WebSettings.LOAD_DEFAULT);

settings.setAppCacheEnabled(true);

settings.setAppCachePath(webView.getContext().getCacheDir().getAbsolutePath());

//设置可访问文件

settings.setAllowFileAccess(true);

//当webview调用requestFocus时为webview设置节点

settings.setNeedInitialFocus(true);

//支持自动加载图片

if (Build.VERSION.SDK_INT >= 19) {

settings.setLoadsImagesAutomatically(true);

} else {

settings.setLoadsImagesAutomatically(false);

}

settings.setNeedInitialFocus(true);

//设置编码格式

settings.setDefaultTextEncodingName("UTF-8");

}

/**

* 重定向退出

*

* @return

*/

public boolean canGoBack() {

if (mWebView != null && mWebView.canGoBack()) {

mWebView.goBack();

return true;

} else {

return false;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值