Android:让 WebView 显示位图的最简单方法?

发布于 2024-09-03 05:02:31 字数 252 浏览 3 评论 0原文

我有一些从存储在位图变量中的远程源加载的图像,我想显示它们。除了在这些图像之间切换之外,用户还应该能够缩放和平移它们。我的第一个想法是以某种方式通过意图将它们传递到内置图库应用程序,但这似乎不可能。 在多个地方建议的解决方案是使用 WebView,因为它已经支持缩放和平移。 我的问题是我的位图数据如何进入 WebView?我是否必须先将其写入文件,稍后必须再次删除该文件,还是有更简单的方法?

或者是否有更好的方法来实现我的主要目标,即将位图数据显示为可缩放和平移的图像?

I have some images that I loaded from a remote source stored in Bitmap variables and I want to display them. In addition to switching between these images the user should also be able to zoom and pan them. My first idea was to somehow pass them via an intent to the built-in gallery application but this doesn't seem to be possible.
A solution that is suggested in several places is using a WebView since it already supports zooming and panning.
My question is how does my Bitmap data get into the WebView? Do I have to write it to a file first, which I would have to remove again later, or is there an easier way?

Or are there even better ways to accomplish my main goal, which is displaying Bitmap data as zoomable and panable images?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷心人i 2024-09-10 05:02:31

您可以使用 webview 直接远程查看您的图像。您不再需要将图像保存在文件中。
这是一个示例代码片段。

myWebView.getSettings().setBuiltInZoomControls(true); //to get zoom functionalities

String url = "http://....."; //url of your image

String x= "<html><head><meta name=\"viewport\" content=\"width=device-width, minimum-scale=1.0\"/><style type=\"text/css\">html, body {margin: 0;padding: 0;} img {border: none;}</style><head><body style=\"background: black;\"><table><tr><td align=\"center\"><img src=\"" + url + "\" /></td></tr></table></body></html>";

myWebView.loadData(x, "text/html", "UTF-8");

关于切换图片,只需更改url的值并再次调用webview的loadData即可。

You can just use webview to directly view your image remotely. You do not need to save anymore the image in a file.
Here is a sample code snippet.

myWebView.getSettings().setBuiltInZoomControls(true); //to get zoom functionalities

String url = "http://....."; //url of your image

String x= "<html><head><meta name=\"viewport\" content=\"width=device-width, minimum-scale=1.0\"/><style type=\"text/css\">html, body {margin: 0;padding: 0;} img {border: none;}</style><head><body style=\"background: black;\"><table><tr><td align=\"center\"><img src=\"" + url + "\" /></td></tr></table></body></html>";

myWebView.loadData(x, "text/html", "UTF-8");

About switching images, you can just change the value of the url and call the loadData again of the webview.

眉黛浅 2024-09-10 05:02:31

毕竟我对 WebView 不满意,所以我最终创建了自己的图像查看 Activity。有关我的操作方法的更多说明,请参阅这篇有关 Google 群组的文章

I wasn't satisfied with WebView after all so I ended up creating my own image viewing Activity. Further descriptions on how I did it can be found in this post on google groups.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文