簡體   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