Android画布缩放,翻译和定位

时间:2011-11-19 23:14:29

标签: android canvas

我在使用画布比例缩放后尝试移动限制,但新坐标不匹配 我的宽度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);
抱歉,我的英语和解释不好,但我想总结一下;
移动画布后缩放后的平移的真实宽度/高度限制是什么?

2 个答案:

答案 0 :(得分:1)

我解决了这个问题如下;

(屏幕分辨率:800x480 [风景])

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

float zoom = 1.0;

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

(对于y /身高也一样)
我希望你能理解。

答案 1 :(得分:0)

您可以保存当前缩放,然后将这样的翻译倍增:

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