Android 中运行时将 SVG 转换为位图

发布于 2024-12-02 01:15:24 字数 1435 浏览 8 评论 0原文

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

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

发布评论

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

评论(2

七分※倦醒 2024-12-09 01:15:24

按照 svg-android 教程获取 PictureDrawable 来自您的 SVG 文件。然后,您需要根据 PictureDrawable 的大小创建一个 Bitmap 并将其提供给 Canvas。当 Canvas 现在从 PictureDrawable 绘制 Picture 时,您需要的当前位图将在运行时绘制(创建)。

PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888); 
Canvas canvas = new Canvas(bitmap); 
canvas.drawPicture(pictureDrawable.getPicture()); 
currentBitmap = bitmap;

Follow the svg-android tutorial to get a PictureDrawable from your SVG file. Then you need to create a Bitmap from the size of the PictureDrawable and give it to a Canvas. When the Canvas now draws a Picture from the PictureDrawable, the current bitmap you need is drawn(created) at runtime.

PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888); 
Canvas canvas = new Canvas(bitmap); 
canvas.drawPicture(pictureDrawable.getPicture()); 
currentBitmap = bitmap;
若沐 2024-12-09 01:15:24

您是否尝试在本机 Android 应用程序中执行此操作?或者在Android浏览器中使用JavaScript?

如果您属于后者,您可以使用 JavaScript 来解析 SVG 并将结果渲染到 HTML5 画布元素(这是一个位图表面)。有两个库可以帮助您执行此操作:

使用这些库将 SVG 渲染到画布后,您可以进一步从画布中获取静态图像文件。 请参阅此 Stack Overflow 线程,了解有关该时刻的更多详细信息步。

Are you trying to do this in a native Android app? Or in the Android browser using JavaScript?

If you're in the later camp, you can use JavaScript to parse SVG and render the results to an HTML5 canvas element (which is a bitmap surface). There are two libraries that can help you do this:

Once you use these libraries to render the SVG to canvas, you could further grab a static image file from the canvas. Refer to this Stack Overflow thread for more detail on that second step.

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