SVG to Bitmap at runtime conversion in Android [closed]

一笑奈何 提交于 2019-12-21 12:23:55

问题


How can I convert SVG (Scaleable Vector Graphics) to Bitmap at runtime in Android?

Kindly if possible provide me the exact chunk of code or exact links. I am quite new to Android application development.


回答1:


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;



回答2:


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:

  • canvg
  • fabric.js

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.



来源:https://stackoverflow.com/questions/7201542/svg-to-bitmap-at-runtime-conversion-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!