【问题标题】:Android canvas scale, translate and positingAndroid 画布缩放、翻译和定位
【发布时间】:2011-11-19 23:14:29
【问题描述】:

我正在尝试使用画布比例缩放后的移动限制,但新坐标不匹配。
我的宽度为 480,在 1.5f 缩放后我的宽度将是 720...但是当我将 translate 设置为 -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);

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

【问题讨论】:

  • 你解决了吗??..请分享一下..我正面临这个问题.. :(

标签: android canvas


【解决方案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/height 执行相同操作)
希望你能理解。

【讨论】:

    【解决方案2】:

    您可以保存当前的缩放,然后像这样乘以翻译:

    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
    

    【讨论】:

    • 我听不懂。 ptCentre 是什么意思?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    相关资源
    最近更新 更多