Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaling canvas and keeping center

Tags:

android

canvas

I'm want to scale a canvas with a bitmap drawn on it. I'm able to scale the canvas but the bitmap drawn on it moves to the upper left respectively lower right.

@Override
protected void onDraw(Canvas canvas) {
    canvas.translate(mPosX, mPosY);
    canvas.scale(mScaleFactor, mScaleFactor);

    //draw bitmap
}

For the last few days I tried many different approaches from manipulating translation coordinates to pivot points for scaling. But nothing did work for me. I'm pretty sure there must be an easy solution for my problem.

Thanks in advance

like image 903
daniel m Avatar asked Aug 07 '12 21:08

daniel m


People also ask

How do you scale in canvas?

You can scale your canvas with content by "bouncing" the content off a temporary canvas while you resize the original canvas. This save+redraw process is necessary because canvas content is automatically cleared when you resize the canvas width or height.

How does canvas scaling work?

The scale() method scales the current drawing, bigger or smaller. Note: If you scale a drawing, all future drawings will also be scaled. The positioning will also be scaled. If you scale(2,2); drawings will be positioned twice as far from the left and top of the canvas as you specify.

What is scaling in javascript?

The scaling transformation changes this default. For example, a scaling factor of 0.5 will change the unit size to 0.5 pixels. Similarly, a scaling factor of 2 will increase the unit size to two pixels. When the unit size is scaled, shapes are also drawn at the new units.


1 Answers

Like you mentioned, the pivot point is the correct way to do this:

canvas.scale(2,2, redCircle.x, redCircle.y);

will work. There is no need for extra translation.

like image 60
you786 Avatar answered Nov 09 '22 23:11

you786