2012-07-10 92 views
0

我在代碼中創建webview。在這個webview中,我顯示圖片。現在我有兩個問題。如何以全尺寸顯示圖像,因爲我的圖片的分辨率爲1381x1829,我只能看到這張圖片的一部分。第二。正如您在代碼中看到的,我從資產加載圖片,但我想從SD卡加載圖片。我該怎麼做?WebView和縮放圖像

這是代碼:

WebView web = new WebView(getContext()); 
web.getSettings().setJavaScriptEnabled(true); 
web.getSettings().setBuiltInZoomControls(true); 
web.loadUrl("file:///android_asset/lj.png"); 


addView(web); 
+0

沒有ü嘗試縮小這樣'webview.setInitialScale([規模])' – Hein 2012-07-10 07:25:03

+0

效果很好,但是我不會在全屏幕上顯示圖片,我想填充像ScaleType.fill_XY那樣的屏幕。這個有可能? 。以及如何從SD卡加載這張照片?任何想法? – user1302569 2012-07-10 07:41:40

回答

1

這種方法webview.getSettings().setBuiltInZoomControls(true)會讓你非multitletouch屏幕

您可以從任何地方的SD卡裝入圖像使用此代碼的WebView實現變焦控制建設。 這就是如果你想從多個位置加載多個圖像。

String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString() + "/Your/Folder"; 
String imagePath = "file:/"+ base + "/test.jpg"; 
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>"; 
mWebView.loadData(html, "text/html","utf-8"); 

但如果ü要加載相同的父文件夾下存在的圖像,這將這樣的伎倆

String imagePath = "test.jpg"; 
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>"; 
mWebView.loadDataWithBaseURL("file:///mnt/sdcard/Your/Folder/", html, "text/html","utf-8",null); 

BE WARN,如果U嘗試像1600x1840加載高分辨率圖像的WebView,網頁流量WILL降低圖像清晰度保持內存使用情況,這導致難看的圖像

+0

工作,但圖片有白色邊距。我想填充webview這個圖像沒有任何利潤。 – user1302569 2012-07-10 08:25:50

+0

Pal,這是你的HTML問題。 沒有冒犯,但我認爲你應該谷歌關於那一個。 – Hein 2012-07-10 08:35:45

1

添加到您的HTML:

<style type='text/css'> 
    img {max-width: 100%;height:initial;} div,p,span,a {max-width: 100%;} 
    </style> 

這將使您的圖像縮放以適合屏幕尺寸。

0

對於刪除默認填充 - 不要忘記設置樣式:

<html><head></head><body style="padding:0; margin:0;"><img src=""+ gifUrl + "" 
width="100%"></body></html> 
1

該解決方案完全適用於我:

webView.setInitialScale(30); 
WebSettings webSettings = webView.getSettings(); 
webSettings.setUseWideViewPort(true);