2012-06-01 95 views
5

由于内置的​​平移和缩放功能,我正在使用WebView来显示类似地图的图像。但是,我需要在地图图像的某些点上叠加一些其他信息(标记等),并显示所有这些信息。Android:如何在WebView中显示位图?

所以我把我的基本地图图像作为位图,并覆盖我的其他图像(使用画布和更多位图),给我一个包含所有信息的最终位图。因为我需要在运行时编辑图像,所以我无法使用预先创建的文件。实现位图覆盖不是问题,问题是我不知道如何轻松显示带有平移和缩放功能的结果图像。

有没有办法使用WebView显示位图?还是有其他建议?

+0

有很多漂亮的库支持平移和缩放Android。一个例子 - https://github.com/sephiroth74/ImageViewZoom – Heitara

回答

20

使用从itsrajesh4uguys的链接,我已经创造了这个代码片段:

// Desired Bitmap and the html code, where you want to place it 
Bitmap bitmap = YOUR_BITMAP; 
String html="<html><body><img src='{IMAGE_PLACEHOLDER}' /></body></html>"; 

// Convert bitmap to Base64 encoded image for web 
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); 
byte[] byteArray = byteArrayOutputStream.toByteArray(); 
String imgageBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT); 
    String image = "data:image/png;base64," + imgageBase64; 

// Use image for the img src parameter in your html and load to webview 
html = html.replace("{IMAGE_PLACEHOLDER}", image); 
webview.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "utf-8", ""); 
+0

你测试过这个代码吗?似乎没有工作 – 2cupsOfTech

+0

封闭的图像字符串与标签解决了问题 – 2cupsOfTech

+0

感谢您的评论。你是对的。我在回答中添加了一行额外的解释。 –

-1

你不要求位图,只是它的名字就足够了。请尝试以下操作:

html = "<html><img src=\"" + imageUrl 
         + "\"></html>"; 
final WebView webView = (WebView) view 
        .findViewById(R.id.yourWebView); 
wvImageView.getSettings().setJavaScriptEnabled(true); 
wvImageView.loadDataWithBaseURL("", html, "text/html", "utf-8", ""); 

这应该在WebView上显示您的图像。 如果要提供缩放工具,试试这个:

webView.getSettings().setSupportZoom(true); 
webView.getSettings().setBuiltInZoomControls(true); 
+0

这是用于显示从URL获取的图像的代码?这不是我想要做的。 – breadbin

+0

上面的代码只需要图像的完全限定路径。尝试给imagePath并检查它是否工作。 – Shrikant

+0

所以我需要将我的修改过的位图保存为一个文件,然后读取它? – breadbin

1

的答案似乎是:没有,显示在网页视图本地的位图对象,如果没有它保存到一个文件,是不可能的。

+2

错误。创建Img标签以显示图像,如带有内联图像的html电子邮件。使用标记,但src不是url,而是base64编码的图像数据。 img src仍然可以通过javascript进行修改。 – JSON

+0

可以加载和显示位图。检查圆锥形的SONY索尼答案。它解释了如何做到这一点。 – Heitara

2

你不需要占位如果u想在web视图加载图像

您可以直接加载dataurl在webview。 例如:

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); 
byte[] byteArray = byteArrayOutputStream.toByteArray(); 
String imgageBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT); 
    String dataURL= "data:image/png;base64," + imgageBase64; 

webview.loadUrl(dataURL); //pass the bitmap base64 dataurl in URL parameter 

希望它可以帮助别人! :-)