1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar;
@SuppressWarnings("deprecation") public class ProgressWebView extends WebView {
private ProgressBar progress_bar_;
private OnWebCallBack onweb_callback_;
public ProgressWebView(Context context) { super(context); }
public ProgressWebView(Context context, AttributeSet attrs) { this(context, attrs, Resources.getSystem().getIdentifier("webViewStyle","attr","android")); }
public ProgressWebView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); if (!isInEditMode()) { initView(context); } setWebViewClient(new MyWebViewClient()); setWebChromeClient(new WebChromeClient()); }
private void initView(final Context context) { progress_bar_ = new ProgressBar(context,null, android.R.attr.progressBarStyleHorizontal); progress_bar_.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 20, 0, 0)); Drawable drawable = context.getResources().getDrawable(R.drawable.progress_bar_states); progress_bar_.setProgressDrawable(drawable); this.addView(progress_bar_); }
public void setOnWebCallback(OnWebCallBack onwebcallback) { this.onweb_callback_ = onwebcallback; }
public class MyWebViewClient extends WebViewClient { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { goBack() ; }
@Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; }
@Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); }
@Override public void onPageStarted(WebView view, String url, Bitmap favicon) { if (onweb_callback_ != null) { onweb_callback_.getUrl(url); } } }
public class WebChromeClient extends android.webkit.WebChromeClient { @Override public void onProgressChanged(WebView view, int newProgress) { LogUtils.i("当前进度",newProgress + "%"); if (newProgress == 100) { progress_bar_.setVisibility(GONE); } else { if (progress_bar_.getVisibility() == GONE) { progress_bar_.setVisibility(VISIBLE); } progress_bar_.setProgress(newProgress); } super.onProgressChanged(view,newProgress); }
@Override public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view,title); if (onweb_callback_ != null ) { onweb_callback_.getTitle(title); } } }
@Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { LayoutParams lp = (LayoutParams) progress_bar_.getLayoutParams(); lp.x = l; lp.y = t; progress_bar_.setLayoutParams(lp); super.onScrollChanged(l, t, oldl, oldt); } }
|