9

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

所以我将我的基本地图图像作为位图,并将我的其他图像覆盖在上面(使用 Canvas 和更多位图),给我一个包含所有信息的最终位图。因为我需要在运行时编辑图像,所以我不能使用预制文件。实现位图的叠加不是问题,问题是我不知道如何使用平移和缩放功能轻松显示生成的图像。

有没有办法使用 WebView 显示位图?或者有什么其他建议?

4

5 回答 5

33

使用来自 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", "");
于 2013-03-30T10:55:49.003 回答
2

如果您想在 webview 中加载图像,则不需要占位符

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

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

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

于 2015-12-19T07:55:19.410 回答
1

通常在 webview 中,我们可以使用 html 标签设置图像。这不会产生任何问题。

以任何方式使用此链接获取示例问题和答案

为什么Bitmap to Base64 String在android的webview上显示黑色背景?

于 2012-06-01T11:17:31.533 回答
1

答案似乎是:不,在 web 视图中显示本地位图对象而不将其保存到文件是不可能的。

于 2012-06-11T10:26:17.317 回答
-2

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

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);
于 2012-06-01T11:25:59.510 回答