title: Scaling Canvas to resize SVG in Android
body: <p>I'm not having much luck wrapping my head around this, so I'm hoping someone can help me out.</p>
  
  <p>I'm getting a drawable from an SVG using svg-android, but the drawable isn't scaling to the view. Everything that I've been able to find says I should draw directly to canvas and rescale the canvas, but when I try that it only seems to change the bounds but not scale the image.</p>
  
  <p>This is what I've tried so far:</p>
  
  <pre><code>ImageView testview = (ImageView)findViewById(R.id.testview);
  
  //Get SVG and convert to drawable
  SVG vector = SVGParser.getSVGFromResource(getResources(),R.drawable.testvector);
  Drawable test = vector.createPictureDrawable();
  
  testview.setBackground(test);  //displays fine, but won't scale to the dimensions of
                                 //the View
  
  //function that clips the image but doesn't scale:
  Drawable testTwo = new CustomPictureDrawable(vector.getPicture(), 
  (float)0.5, (float)0.5);
  
  testView.setBackground(testTwo);
  
  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);
  }
  }
  
  //doesn't display anything
  Picture testThree = vector.getPicture();
  
  Bitmap b = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_4444);
  Canvas c = new Canvas(b);
  
  c.drawPicture(testThree, new Rect(0,0,10,10));
  
  testview.draw(c);
  </code></pre>
  
  <p>I've also found a function that will create a scaled bitmap, but the image quality is significantly reduced, so I may as well just use scaled PNGs.</p>
  
  <p>Obviously I'm missing something, and my lack of experience is making it really frustrating. </p>
  
  <p>What I'd <em>like</em> to be able to do is have svg-android completely re-scale the SVG before pulling a Picture or PictureDrawable out of it, but I can't figure out how to step through the SVGParser, and running multipliers on every coordinate pair would probably be super resource intensive anyway.</p>
  
  <p>[edit] Is the only way to scale and re-draw the Picture and assign that to a view to create custom Views and override OnDraw? </p>
  
  <p>i.e.</p>
  
  <pre><code>Picture testThree = vector.getPicture();
  
  Bitmap b = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_4444);
  Canvas c = new Canvas(b);
  
  c.drawPicture(testThree, new Rect(0,0,10,10));
  
  //CustomView extends ImageView or Button or whatever with OnDraw overridden and no
  //other changes 
  CustomView testview = (CustomView)findViewById(R.id.testview); 
  
  testview.OnDraw(c);
  </code></pre>
  
  <p>Am I on the right track? Canvas c would overwrite the default canvas (which is what I want), wouldn't it?</p>