Android:如何在 WebView 中显示位图? [英] Android: How to Display a Bitmap in a WebView?

查看:19
本文介绍了Android:如何在 WebView 中显示位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using a WebView for displaying a map-like image, due to the built-in panning and zooming functionality. However, I need to overlay some other information (markers etc) at certain points on the map image, and display all of this.

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

So I take my base map image as a Bitmap, and overlay my other images over that (using Canvas and more Bitmaps), giving me a final Bitmap with all the info. Because I need to edit the image at runtime, I can't use a pre-made file. Implementing the overlaying of bitmaps is not the problem, the problem is that I don't know how to easily display the resulting image with pan and zoom capabilities.

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

Is there a way to display a Bitmap using a WebView? Or any other suggestions?

推荐答案

使用来自 itsrajesh4uguys 的链接,我创建了以下代码片段:

Using the link from itsrajesh4uguys, I've created this code snippet:

// 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", "");

这篇关于Android:如何在 WebView 中显示位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆