一、WebView中图片点击放大,浏览显示
1、给这个html中所有的img标签添加点击事件,通过JS来调用java的方法。
mWebView.setWebViewClient(new PrWebViewClient(mWebView) {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//添加图片监听事件
addImageClickListener();
if (ivError.getVisibility() != View.VISIBLE) {
mWebView.setVisibility(View.VISIBLE);
}
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if (request.isForMainFrame() || TextUtils.equals(request.getUrl().toString(), view.getUrl())) {
}
}
@Override
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
handler.cancel();
}
});
private void addImageClickListener() {
// 这段js函数的功能就是,遍历所有的img几点,并添加onclick函数,在还是执行的时候调用本地接口传递url过去
String url = "javascript:(function(){"
+ "var imgs = document.getElementsByTagName(\"img\");"
+ "var imgUrl='';"
+ "var arr = new Array();"
+ "for(var i=0;i<imgs.length;i++){"
+ " imgs[i].pos = i;"
+ " arr[i] = imgs[i].src;"
+ " imgUrl+=imgs[i].src+',,,';"
+ " imgs[i].onclick=function(){ "
+ " imageListener.openImage(arr,this.pos); "
+ " } "
+ "}"
+ "})()";
mWebView.loadUrl(url);
}
2、获取图片的URL
mWebView.addJavascriptInterface(new JavascriptInterface(),"imagelistner");
/**
* js通信接口
*/
public class JavascriptInterface {
public JavascriptInterface() {
}
@android.webkit.JavascriptInterface
public void openImage(String[] arr, int postion) {
ArrayList<String> imgUrlList = new ArrayList<>();
for (int i =0; i < arr.length; i ++) {
imgUrlList.add(arr[i]);
}
//实现自己的图片浏览页面
ImagePagerActivity.showActivity(context, imgsUrl, postion);
}
@android.webkit.JavascriptInterface
public void openImage(String img, String chickUrl, int postion) {
String[] imgs = img.split(",");
ArrayList<String> imgsUrl = new ArrayList<>();
for (int i =0; i < imgs.length; i ++) {
imgsUrl.add(imgs[i]);
}
//实现自己的图片浏览页面
ImagePagerActivity.showActivity(context, imgsUrl, postion);
}
//注解 很重要,必不可少
@android.webkit.JavascriptInterface
public void openImage(String img) {
String[] imgs = img.split(",");
int postion = 0;
ArrayList<String> imgsUrl = new ArrayList<>();
for (int i =0; i < imgs.length; i ++) {
if (imgs[i].equals(chickUrl)){
postion = i;
}
imgsUrl.add(imgs[i]);
}
//实现自己的图片浏览页面
ImagePagerActivity.showActivity(context, imgsUrl, postion);
}
}
二、webview 记录滑动位置
1、设置位置
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mWebView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//webviewcontent.getContentHeight 获取内容的高度
//webviewcontent.getScale 缩放比例
//webviewcontent.getHeight 现在高度
//webviewcontent.getScrollY 滚动高度
//此处是判断是否滑动到底部的代码
if ((mWebView.getContentHeight() * mWebView.getScale() - 100) <= (mWebView.getHeight() + mWebView.getScrollY())) {
//TODO 判断是否滑动到底部
}
}
});
}
mWebView.setWebViewClient(new PrWebViewClient(mWebView) {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onScaleChanged(WebView view, float oldScale, float newScale) {
super.onScaleChanged(view, oldScale, newScale);
float scale = oldScale < newScale ? oldScale : newScale;
try {
String position = SPUtils.readStringFromConfig("web" + mNewsId, "0");
int webScrollY = (int) (Integer.valueOf(position) * scale);
view.setScrollY(DensityUtils.px2dp(NewsAudioDetailActivity.this, webScrollY));
} catch (Exception e) {
e.printStackTrace();
}
}
});
2、在onPause
中记录滑动位置
@Override
protected void onPause() {
super.onPause();
if (mWebView != null) {
int webScrollY = mWebView.getScrollY();
//保存访问的位置
SPUtils.saveStringToConfig("web" + mNewsId, String.valueOf(webScrollY));
}
}
``
网友评论