WebView的問題總結(jié)
目錄介紹
1.關(guān)于加載H5頁面閃爍問題
2.WebView頁面中播放了音頻,退出Activity后音頻仍然在播放
3.為WebView自定義錯(cuò)誤顯示界面
4.判斷WebView是否已經(jīng)滾動(dòng)到頁面底端
5.在頁面中先顯示圖片
好消息
- 博客筆記大匯總【16年3月到至今】忘闻,包括Java基礎(chǔ)及深入知識(shí)點(diǎn),Android技術(shù)博客也殖,Python學(xué)習(xí)筆記等等,還包括平時(shí)開發(fā)中遇到的bug匯總哗咆,當(dāng)然也在工作之余收集了大量的面試題膘掰,長(zhǎng)期更新維護(hù)并且修正吠昭,持續(xù)完善……開源的文件是markdown格式的!同時(shí)也開源了生活博客饺律,從12年起窃页,積累共計(jì)47篇[近20萬字],轉(zhuǎn)載請(qǐng)注明出處复濒,謝謝脖卖!
- 鏈接地址:https://github.com/yangchong211/YCBlogs
- 如果覺得好,可以star一下巧颈,謝謝畦木!當(dāng)然也歡迎提出建議,萬事起于忽微砸泛,量變引起質(zhì)變十籍!
1.關(guān)于加載H5頁面閃爍問題
- 方案1:加載進(jìn)度條【該方法在夜間模式下無法解決閃爍問題】
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
view.getSettings().setJavaScriptEnabled(true);
if (progressDialog == null) {
progressDialog=new ProgressDialog(PNewsContentActivity.this);
progressDialog.setMessage("數(shù)據(jù)加載中蛆封,請(qǐng)稍后。勾栗。惨篱。");
progressDialog.show();
webView.setEnabled(false);// 當(dāng)加載網(wǎng)頁的時(shí)候?qū)⒕W(wǎng)頁進(jìn)行隱藏
}
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
view.getSettings().setJavaScriptEnabled(true);
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
webView.setEnabled(true);
}
super.onPageFinished(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url); //在當(dāng)前的webview中跳轉(zhuǎn)到新的url
return true;
}
- 方案2:硬件加速器【夜間模式無效】
android webview 在3.0+后顯示flash和視頻要啟用硬件加速
開啟硬件加速是在manifest中加入: android:hardwareAccelerated="true"
開啟硬件加速后webview有可能會(huì)出現(xiàn)閃爍的問題
webView.setBackgroundColor(Color.parseColor("#000000")); //ok 不會(huì)閃黑屏
webView.setBackgroundColor(0x000000); //會(huì)閃黑屏
由于webview的背景顏色默認(rèn)是白色,在一些場(chǎng)合下會(huì)顯得很突兀(比如背景是黑色【夜間模式】)围俘。
android:hardwareAccelerated="false" 清單文件
android:layerType="software" 布局文件
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE,null); 代碼中
- 方案3:設(shè)置延遲執(zhí)行夜間模式方法
1.布局
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"/>
//注意WebView默認(rèn)是白色背景砸讳,記得設(shè)置高度為包裹,否則夜間模式下無法用進(jìn)度條過渡閃頻
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="200dp"
android:background="?containerBackground"
android:visibility="gone"/>
2.代碼設(shè)置
private class DataWebViewClient extends WebViewClient {
//H5頁面加載完成后執(zhí)行
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//執(zhí)行H5夜間模式點(diǎn)擊事件
if (isNight) {
wvDetailInvestor.loadUrl("javascript:toggleClassTest()");
}
if (handler == null) {
handler = new Handler();
}
//延遲代碼執(zhí)行【避免刷屏】
handler.postDelayed(new Runnable() {
@Override
public void run() {
wvDetailInvestor.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE); //進(jìn)度條隱藏
}
}, 500);
}
//H5頁面加載開始就執(zhí)行
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
wvDetailInvestor.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE); //進(jìn)度條顯示
}
}
2.WebView頁面中播放了音頻,退出Activity后音頻仍然在播放
* 錯(cuò)誤:需要在Activity的onDestory()中調(diào)用webView.destroy();
* 正確:webview調(diào)用destory時(shí),webview仍綁定在Activity上.這是由于自定義webview構(gòu)建時(shí)傳入了該Activity的context對(duì)象,因此需要先從父容器中移除webview,然后再銷毀webview:rootLayout.removeView(webView);
3.為WebView自定義錯(cuò)誤顯示界面
覆寫WebViewClient中的onReceivedError()方法:
/**
* 顯示自定義錯(cuò)誤提示頁面界牡,用一個(gè)View覆蓋在WebView
*/
protected void showErrorPage() {
LinearLayout webParentView = (LinearLayout)mWebView.getParent();
initErrorPage();
while (webParentView.getChildCount() > 1) {
webParentView.removeViewAt(0);
}
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
webParentView.addView(mErrorView, 0, lp);
mIsErrorPage = true;
}
protected void hideErrorPage() {
LinearLayout webParentView = (LinearLayout)mWebView.getParent();
mIsErrorPage = false;
while (webParentView.getChildCount() > 1) {
webParentView.removeViewAt(0);
}
}
protected void initErrorPage() {
if (mErrorView == null) {
mErrorView = View.inflate(this, R.layout.online_error, null);
Button button = (Button)mErrorView.findViewById(R.id.online_error_btn_retry);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mWebView.reload();
}
});
mErrorView.setOnClickListener(null);
}
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mErrorView.setVisibility(View.VISIBLE);
super.onReceivedError(view, errorCode, description, failingUrl);
}
4.判斷WebView是否已經(jīng)滾動(dòng)到頁面底端
getScrollY()方法返回的是當(dāng)前可見區(qū)域的頂端距整個(gè)頁面頂端的距離,也就是當(dāng)前內(nèi)容滾動(dòng)的距離.
getHeight()或者getBottom()方法都返回當(dāng)前WebView 這個(gè)容器的高度
getContentHeight 返回的是整個(gè)html 的高度,但并不等同于當(dāng)前整個(gè)頁面的高度,因?yàn)閃ebView 有縮放功能, 所以當(dāng)前整個(gè)頁面的高度實(shí)際上應(yīng)該是原始html 的高度再乘上縮放比例. 因此,更正后的結(jié)果,準(zhǔn)確的判斷方法應(yīng)該是:
if(WebView.getContentHeight*WebView.getScale() == (webview.getHeight()+WebView.getScrollY())){
//已經(jīng)處于底端
}
5.在頁面中先顯示圖片
@Override
public void onLoadResource(WebView view, String url) {
mEventListener.onWebViewEvent(CustomWebView.this, OnWebViewEventListener.EVENT_ON_LOAD_RESOURCE, url);
if (url.indexOf(".jpg") > 0) {
hideProgress(); //請(qǐng)求圖片時(shí)即顯示頁面
mEventListener.onWebViewEvent(CustomWebView.this, OnWebViewEventListener.EVENT_ON_HIDE_PROGRESS, view.getUrl());
}
super.onLoadResource(view, url);
}
后續(xù):
平時(shí)喜歡寫寫文章簿寂,筆記。別人建議我把筆記宿亡,以前寫的東西整理常遂,然后寫成博客,所以我會(huì)陸續(xù)整理文章挽荠,只發(fā)自己寫的東西克胳,敬請(qǐng)期待:
知乎:https://www.zhihu.com/people/yang-chong-69-24/pins/posts
領(lǐng)英:https://www.linkedin.com/in/chong-yang-049216146/
簡(jiǎn)書:http://www.reibang.com/u/b7b2c6ed9284
csdn:http://my.csdn.net/m0_37700275
網(wǎng)易博客:http://yangchong211.blog.163.com/
新浪博客:http://blog.sina.com.cn/786041010yc
github:https://github.com/yangchong211
喜馬拉雅聽書:http://www.ximalaya.com/zhubo/71989305/