我正在使用WebView来显示类似地图的图片,因为内置了平移和缩放功能。然而,我需要在地图上的特定点位上叠加其他信息(标记等),并显示所有内容。
因此,我将基础地图图片作为位图,并在其上叠加其他图像(使用Canvas和更多的位图),从而得到具有所有信息的最终位图。因为我需要在运行时编辑图像,所以不能使用预制文件。实现位图叠加不是问题,问题在于我不知道如何轻松地使用平移和缩放功能显示结果图片。
有没有一种方法可以使用WebView来显示位图?或者有其他建议吗?
我正在使用WebView来显示类似地图的图片,因为内置了平移和缩放功能。然而,我需要在地图上的特定点位上叠加其他信息(标记等),并显示所有内容。
因此,我将基础地图图片作为位图,并在其上叠加其他图像(使用Canvas和更多的位图),从而得到具有所有信息的最终位图。因为我需要在运行时编辑图像,所以不能使用预制文件。实现位图叠加不是问题,问题在于我不知道如何轻松地使用平移和缩放功能显示结果图片。
有没有一种方法可以使用WebView来显示位图?或者有其他建议吗?
使用其rajesh4uguys的链接,我创建了此代码片段:
// 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", "");
如果您想在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
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);
public Bitmap overlayBitmaps(Bitmap baseImg, Bitmap overlayImg) {
Bitmap finalImg = Bitmap.createBitmap(baseImg.getWidth(), baseImg.getHeight(), baseImg.getConfig());
Canvas canvas = new Canvas (finalImg);
canvas.drawBitmap(baseImg, new Matrix(), null);
canvas.drawBitmap(overlayImg, new Matrix(), null);
return finalImg;
}
我从未说过您正在叠加图像,我所问的是如何获取您所说需要使用的图像路径,以便在Webview中显示位图对象? - breadbin