Take the 2-minute tour ×

I have implemented a long Ondraw method which draws a set of rectangles. the rectangles are too small and i want them to appear bigger. But unfortunately i cant change the rectangle coordinates becoz they are stored in a database. So is there any way i can Zoom in the canvas using the canvas.scale() ?

share|improve this question
"But unfortunately i cant change the rectangle coordinates becoz they are stored in a database" Why? you get the coordinates from the DB and multiply by whatever scale factor you need before drawRectangle(). –  Simon Nov 12 '12 at 17:49
can i implement a zoom in function that way? –  Ruby Nov 12 '12 at 17:58
x = getValueFromDatabase(), y = getValueFromDatabase(), x=x*5;y=y*5; drawRectangle(x,y). What's wrong with something like that? My point is, why does having the values stored in a database stop you drawing the rectangles bigger? I don't get it. Of course you can change the rectangle coordinates. You can make them anything you want. –  Simon Nov 12 '12 at 18:20
i know but as i have already have the data in the database and done drawing the map, i just need to know a method to zoom in the canvas or the view related to it :) –  Ruby Nov 12 '12 at 18:45

1 Answer 1

I'm going to preface this answer by saying you will need to draw everything at 0,0 and then scale it, and finally translate it to behave properly.

Simply do the following in your onDraw method:

    canvas.translate(xValue, yValue);
    canvas.scale(xScale, yScale)
    /* draw whatever you want scaled at 0,0*/

xScale shrinks or stretches in the X direction, yScale shrinks or stretches in the Y direction.

1.0 is the default for these, so 2.0 would stretch it by double and 0.5 would shrink it by half.


    canvas.translate(50, 50);
    canvas.scale(0.5f, 0.5f);
    canvas.drawRect(0.0, 0.0, 5.0, 5.0, paint);

This will draw a rectangle with length 5.0, and width 5.0, scale it down to 2.5 for length and width, and then move it to (50, 50).

The result will be a rectangle drawn as if you did this:

canvas.drawRect(50.0, 50.0, 52.5, 52.5, paint);

I hope this helps!

share|improve this answer

Your Answer


