musa
musa

Reputation: 1465

Android canvas scale, translate and positing

I'm trying the moving limit after zoom with canvas scale, but new coordinates does not match.
my width 480, after 1.5f zooming my width will be 720... but when I set translate to -480, I'm seeing more space on the right.

float zoom = 0.5f;
PointF translate = new PointF(0, 0);
canvas.scale(zoom, zoom);
canvas.translate(translate.x, translate.y);
//...
canvas.drawRect(0, 0, width, height, paint);

sorry my bad English and explanation, but I want to ask in summary;
what is the true width/height limit for translate after zooming for moving the canvas?

Upvotes: 2

Views: 5109

Answers (2)

musa
musa

Reputation: 1465

I solved this problem as follows;

(screen resolution: 800x480 [landscape])

int screenWidth = 800;
int gameLimitX = 1600;
int cameraPositionX = 0;

float zoom = 1.0;

if((screenWidth / zoom) + cameraPositionX > gameLimitX) {
    cameraPositionX = (screenWidth / zoom) + cameraPositionX;
}

(do same for y/height)
I hope you can understand.

Upvotes: 1

Deepscorn
Deepscorn

Reputation: 832

You can save your current zoom and then multiply translations like this:

canvas.scale(_factorScale, _factorScale);
int wGameView = (int) (_wGameView/_factorScale);
int hGameView = (int) (_hGameView/_factorScale);
int xCam = (int) (ptCentre.x-(wGameView>>>1));
int yCam = (int) (ptCentre.y-(hGameView>>>1));
//move camera now!
canvas.translate(-xCam,-yCam);
//here goes drawing

Upvotes: 0

Related Questions