krysanify / androidsvg

Automatically exported from code.google.com/p/androidsvg
0 stars 0 forks source link

Scale images to canvas? how to? #19

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hello and thank you for gread library!

I'm trying to use it in my app.
Idea is to use svg icons, and render them to bitmap cache objects to fit 
different screen resolutions.

Here is code example.

SVG svg = SVG.getFromResource(getActivity(), R.raw.avatar_default);
Bitmap mBitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
Canvas c=new Canvas(mBitmap);
svg.renderToCanvas(c);
img.setImageBitmap(mBitmap);

by passing width, height I assume to scale svg to fit bitmap rectangle.

Here is example in attachment.
Selected picture is one from code above. How to scale svg to different size 
canvas? 
I also attach couple source pictures from grid on screenshot. As you see, when 
I'm trying to render svg with defaults 96x96 to 120x120 pixels Canvas\Bitmap 
I've got artefacts on picture...
I tried different scale modes on ImageViews but no luck :(

Original issue reported on code.google.com by swe...@gmail.com on 21 Oct 2013 at 11:52

Attachments:

GoogleCodeExporter commented 9 years ago
Hi swexru

I'm glad you are finding the library useful.  Thanks.

The solution to your problem is the viewBox attribute.  It tells the renderer 
what the bounds of your picture content is, so it knows how to scale it.  
Inkscape doesn't add viewBox, so you either have to add them to the file 
yourself, or add a viewBox after it is loaded, but before you render to canvas.

See the FAQ for more info.  https://code.google.com/p/androidsvg/wiki/FAQ

In your case, adding the following lines should work for all or most of your 
files:

svg.setDocumentViewBox(0, 0, svg.getDocumentWidth(), svg.getDocumentHeight());
svg.setDocumentWidth("100%");
svg.setDocumentHeight("100%");

Original comment by paul.leb...@gmail.com on 22 Oct 2013 at 2:08

GoogleCodeExporter commented 9 years ago
Thank you! Sorry for my blindness...

Original comment by swe...@gmail.com on 22 Oct 2013 at 7:58

GoogleCodeExporter commented 9 years ago
No worries. You're welcome.

Original comment by paul.leb...@gmail.com on 23 Oct 2013 at 12:29