繁体   English   中英

Android canvas缩放,翻译和放置

[英]Android canvas scale, translate and positing

我正在尝试使用画布比例缩放后的移动限制,但是新坐标不匹配。
我的宽度480,在进行1.5f缩放后,我的宽度将为720 ...但是当我将其设置为-480时,我会在右边看到更多空间。

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);

对不起,我的英语不好和解释不好,但我想总结一下。
缩放移动画布后平移的真正宽度/高度限制是多少?

我如下解决了这个问题;

(屏幕分辨率:800x480 [横向])

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

float zoom = 1.0;

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

(对y / h进行相同操作)
我希望你能够明白。

您可以保存当前的缩放比例,然后按以下方式进行多次翻译:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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