zhanaotian / svg-android

Automatically exported from code.google.com/p/svg-android
0 stars 0 forks source link

Scale issue, Is it possible to specify size of the rendered picture? #7

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Firstly, thanks your guy for sharing such a great project.

After go through the code, I found there is no way to scale the output picture, 
and the following code

        SVG svg = SVGParser.getSVGFromInputStream(inputStream);

        PictureDrawable drawable = svg.createPictureDrawable();

    drawable.setBounds(0, 0, newWidth, newHeight);

is only affect 

<svg version="1.1" baseProfile="basic" x="0px" y="0px" width="374px" 
height="367.723px"
     viewBox="0 0 374 367.723" xml:space="preserve">

but the shape is not scaled.

Original issue reported on code.google.com by kouxing2...@gmail.com on 5 Jun 2011 at 5:03

GoogleCodeExporter commented 9 years ago
I have changed the SVGParser to enable this function, no issues in my test. Do 
you need to commit this function in?

Original comment by kouxing2...@gmail.com on 6 Jun 2011 at 3:22

GoogleCodeExporter commented 9 years ago
I would like a copy of your patch, is it possible to post it here?

Original comment by mccor...@gmail.com on 11 Jul 2011 at 9:11

GoogleCodeExporter commented 9 years ago
One way I worked around this problem was to scale and translate the canvas 
before the draw. E.g.

        ImageView view = new ImageView(this) {
            @Override
            public void draw(Canvas canvas) {
                canvas.scale((float)1.0, (float)1.5);
                canvas.translate(0, -100);
                super.draw(canvas);
            }
        };

Original comment by par.stef...@gmail.com on 8 Aug 2011 at 8:06

GoogleCodeExporter commented 9 years ago
i was shocked to find that this is not actually possible, that you have to 
create the drawable and resize it ? seems extremely wasteful to me, is there no 
way to ask the SVG object to give me a drawable of a certain size?

Original comment by sla...@gmail.com on 18 Nov 2011 at 10:20

GoogleCodeExporter commented 9 years ago
I think this is a crucial potential advantage of svg (I'm using it to import 
flash assets into an android app). The canvas override trick works beautifully 
in my situation (a composite multistate button class), but it is a bit 
perverse, and it would be nice to scale the svg directly. imho this seems to 
cover one of the few really colossal oversights/kludges/painful irritations 
I've found so far in droid apis.

Original comment by dak...@gmail.com on 30 Nov 2011 at 2:23

GoogleCodeExporter commented 9 years ago
For resizing, I use something like:

SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.sample);
canvas.drawPicture(svg.getPicture(), new Rect(0,0,100,100));

Original comment by javifina...@gmail.com on 4 Dec 2011 at 1:40

GoogleCodeExporter commented 9 years ago
I've struggled a couple of days to render SVG properly on Android. When I came 
across this library, my first impression was that it produces a raster image 
the size of the underlying SVG document. This impression was supported by the 
existence of this issue.

As long as you know what a Picture is and does (I unfortunately did NOT know 
the very important difference from a raster image or Bitmap at first, because I 
had not used a Picture before), the scaling of the SVG can be done (as 
mentioned above) by scaling your Canvas and redrawing the Picture. It is indeed 
very, very simple to use. 

Just do NOT render the Picture to a Bitmap once and expect the Bitmap to look 
good when scaled up ;) That's what led... "someone else" to believe that he had 
to spend days looking for an alternative to svg-android...

Original comment by reimar.t...@googlemail.com on 8 Feb 2012 at 10:00

GoogleCodeExporter commented 9 years ago
I had a problem to scale up image and set to the layout background. In this 
case I could not override draw method for the view so did something like this: 

    SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.menu_svg);
        Drawable drawable = new CustomPictureDrawable(svg.getPicture(), 2, 2);

And my CustomPictureDrawable class:

public class CustomPictureDrawable extends PictureDrawable {

    private float scalex, scaley;

    public CustomPictureDrawable(Picture picture, float scalex, float scaley) {
        super(picture);
        this.scalex = scalex;
        this.scaley = scaley;
    }

    @Override
    public void draw(Canvas canvas) {
        Matrix original = canvas.getMatrix();
        canvas.scale(scalex, scaley);
        super.draw(canvas);
        canvas.setMatrix(original);
    }
}

Now I can easily control display for individual pictures.

Original comment by dalius.s...@gmail.com on 21 Jul 2012 at 2:14

GoogleCodeExporter commented 9 years ago
I'm not having much luck figuring out canvas scaling, can anyone give me some 
pointers? Everything I've tried either clips the image without scaling it, or 
uses a ScaledBitmap, which reduces the quality significantly.

Kouxing, is there any way you could share your patch to the SVGParser? I think 
that might do what I need.

Original comment by daffydle...@gmail.com on 4 Jan 2013 at 11:16

GoogleCodeExporter commented 9 years ago
hi, please refer to this: http://code.google.com/p/svg-android-2/ thanks to 
this nice guy, my solution is similar to his.

Original comment by kouxing2...@gmail.com on 7 Jan 2013 at 9:16

GoogleCodeExporter commented 9 years ago
Hi Kouxing

I'm using a variant of svg-android (com.applantation.android.svg). I would like 
to import the changes to support SVG scaling. Could you show which modification 
should I import in SVGParser ?

Original comment by a.diba...@gmail.com on 19 May 2013 at 9:08

GoogleCodeExporter commented 9 years ago
a,

The solution that I found was the following:

In SVG.java I added the following function:

public Picture resizePicture(int height, int width){
        Picture newPicture = new Picture();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas = newPicture.beginRecording(width, height);
        canvas.drawPicture(picture, new Rect(0,0,width,height));
        newPicture.endRecording();
        limits.right=width;
        limits.bottom=height;
        return newPicture;
    }

It works, but I think that it may be terribly inefficient. I think it should be 
possible to do it in one or two steps with canvas.scale, but I'm afraid of 
trying to fix what isn't broken here.

Original comment by whiskyne...@gmail.com on 3 Jul 2013 at 4:38