简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM