问题
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