在Android中, 大图片加载或者处理很容易出来OutOfMemory,也就是内存溢出
所以在这里就使用WebView来加载大图片,但是WebView不好控制图片显示大小,也就是说
如果图片太大,webview将出一滚动条,图片过小则不可拉伸,极其影响美观。为此,作了些
修改,在一定区域内做一些图片尺寸的压缩。
主要实现方式:由WebView的父控件居中布局来控制垂直方向的居中,WebView的元素居中
来控制图片的水平居中 ,最终达到图片在屏幕的中间显示。
核心Java代码如下:
private void showImage2WebView(String path, boolean isScale) {
float nowDisenty = disenty;
String url = "file://" + path;
String style = "";
if (isScale) {
if (webScaleImgHeightPx > webImgHeightPx) {
webScaleImgWidthPx = webImgWidthPx;
webScaleImgHeightPx = webImgHeightPx;
} else {
webScaleImgWidthPx = webImgWidthPx + webImgWidthPx / 2;
webScaleImgHeightPx = webImgHeightPx + webImgHeightPx / 2;
}
style = "style=\"width: " + webScaleImgWidthPx + "px; height: " + webScaleImgHeightPx + "px;\"";
} else {
shortContext.setVisibility(View.GONE);
webView = new WebView(this);
Rect outRect = new Rect();
webViewLayout.getWindowVisibleDisplayFrame(outRect);
int contentHeight = (int) (wh[1] - this.getResources().getDimension(R.dimen.title_normal_height) - outRect.top - 30);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options); // 因为设置了inJustDecodeBounds=true 所以此时返回的 bitmap 为 null
options.inJustDecodeBounds = false;
int bitmapWidth = options.outWidth;
int bitmapHeight = options.outHeight;
webImgWidthPx = bitmapWidth;
webImgHeightPx = bitmapHeight;
int[] imgDis = new int[2];
int minHeight = getResources().getDimensionPixelSize(R.dimen.loadview_img_minheight);
double scale = 0.00f;
boolean isScaleByHeight = (double) contentHeight / bitmapHeight < (double) wh[0] / bitmapWidth;
if (bitmapHeight > (float) contentHeight / nowDisenty && isScaleByHeight) {
scale = (double) contentHeight / bitmapHeight;
// if(scale >= 1) scale = 0.9f;
imgDis[0] = (int) (scale * bitmapWidth / (nowDisenty + 0.1));
imgDis[1] = (int) (scale * bitmapHeight / (nowDisenty + 0.1));
webImgWidthPx = imgDis[0];
webImgHeightPx = imgDis[1];
style = "style=\"width: " + webImgWidthPx + "px; height: " + webImgHeightPx + "px;\"";
} else if (bitmapWidth > wh[0]) {
scale = (double) wh[0] / bitmapWidth;
imgDis[0] = (int) (scale * bitmapWidth / (nowDisenty + 0.04));
imgDis[1] = (int) (scale * bitmapHeight / (nowDisenty + 0.04));
webImgWidthPx = imgDis[0];
webImgHeightPx = imgDis[1];
style = "style=\"width: " + webImgWidthPx + "px; height: " + webImgHeightPx + "px;\"";
} else if (bitmapHeight < minHeight) {
scale = (double) minHeight / bitmapHeight;
int width = (int) (scale * bitmapWidth);
webImgWidthPx = (int) (width / nowDisenty);
webImgHeightPx = (int) (minHeight / nowDisenty);
style = "style=\"width: " + webImgWidthPx + "px; height: " + webImgHeightPx + "px;\"";
}
}
StringBuffer data = new StringBuffer();
data.append("<html><center><img src=\"").append(url).append("\" ").append(style).append("></center></html>");
// webView.loadDataWithBaseURL(url, data.toString(), "text/html","UTF-8","");
webView.setScrollContainer(false);
webView.setScrollbarFadingEnabled(false);
webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
WebSettings settings = webView.getSettings();
settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
settings.setBuiltInZoomControls(false); // 设置显示缩放按钮
settings.setSupportZoom(false); // 支持缩放
// settings.setLoadsImagesAutomatically(true);
// webView.setInitialScale(100);
// settings.setUseWideViewPort(true);
// settings.setLoadWithOverviewMode(true);
webView.setPadding(webView.getPaddingLeft(), webView.getPaddingTop(), webView.getPaddingRight() + 5, webView.getPaddingBottom());
if (!isScale) {
webView.setBackgroundColor(0x00000000);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.CENTER);
params.width = LayoutParams.WRAP_CONTENT;
params.height = LayoutParams.WRAP_CONTENT;
webView.setMinimumHeight(500);
webViewLayout.addView(webView, params);
webView.loadDataWithBaseURL(url, data.toString(), "text/html", "UTF-8", "");
} else {
webView.clearDisappearingChildren();
webView.clearFormData();
webView.clearView();
webView.loadDataWithBaseURL(url, data.toString(), "text/html", "UTF-8", "");
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.CENTER);
params.width = LayoutParams.WRAP_CONTENT;
params.height = LayoutParams.WRAP_CONTENT;
//params.height = (int) (webScaleImgHeightPx * disenty);
webViewLayout.removeView(webView);
webViewLayout.addView(webView, params);
webView.invalidate();
webViewLayout.invalidate();
}
}
分享到:
相关推荐
Android webview 加载网页以及本地资源以及SD资源demo,以及加载缓存资源
Android实现WebView懒加载,提前进行页面JS资源加载。减少WebView加载时间及加载资源问题。Android 8.0以前需要引入X5内核,Android 8.0以后无特殊要求。
实现Android中WebView图片的缓存,并且实现如何替换WebView中默认图片以及如何实现图片的点击。
android webview加载gif图片,控件显示gif图片、加载网络图片,不卡,流畅,是开发者使用的好demo
android webView加载html 并引用本地资源(图片、字体库)
Android webview加载网页
android使用webview加载网页,自己搞的,很简单,有需要的可以看看啊
现在web app越来越多,webview控件的使用也越来越重要。 有时我们会在启动页或引导页之后,加载一个webview为控件的主页。 但如果跳转之后再加载,会看到webview有一个加载的过程,如果网页较大可能加载起来会很慢。...
Android WebView 加载 PDF 文件实现。其原理就是在 app 本地搭建一个 website ,然后利用 WebView 加载这个站点,站点去实现加载显示 PDF 的目的,站点其原理也是采用 js 框架去实现的。
android Webview加载本地图片,自适应布局大小
做webview加载网页时,遇到一个问题,自带的webview错误页面不美观,于是自定义一个view提示用户,网上很多方法,也不稳定实现替换,结合网上的一些例子,加以优化和拓展,特此写一个完整版本的Demo供大家拿来直接...
主要介绍了详解android 用webview加载网页(https和http),详细的介绍了两个错误的解决方法,有兴趣的可以了解一下
demo中介绍使用webview加载本地页面,并在java代码中提供js调用事件,在html中动态生成组件button
android webview加载网页获取对应url 参数值的方法(csdn)————程序
实现webview加载时显示进度条,类似微信公众号文章加载。
android用webview加载assert中的html并且可拨打页面中的电话。
本人自己写的一个android用webview加载html5播放视频,支持全屏播放!以前上传了个版本,那个要积分下载,但修改不了,所以重新上传一下!这个版本加了个全屏双击时会退出全屏!
Android通过JS在线用webview加载PDF文件(无限放大版本),支持webview无限放大
android webview 加载网页 样例; 实现简单的加载网页;
webview加载失败,自定义错误页面处理