gpt4 book ai didi

java - Android:围绕屏幕中心旋转 Canvas

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:04:22 29 4
gpt4 key购买 nike

我正在尝试围绕屏幕中心旋转 Canvas ,以获得右下角的 (0,0) 坐标。

我是这样做的:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();
canvas.rotate(180, width/2, height/2);

它确实旋转正确,并且在 API 级别 17 的 AVD 上一切都很完美。但是在 API 级别 8 的 AVD 上,(0,0) 坐标出现在错误的位置。在旋转之前,图片在该设备上也适合整个屏幕,但现在看起来像这样:

enter image description here

是什么导致了这个问题?

*编辑!

通过围绕 Canvas 的中点旋转 Canvas 解决了这个问题:

canvas.rotate(180,canvas.getWidth()/2,canvas.getHeight()/2);

但是,为什么以前的版本不能使用旧的 API?

最佳答案

CanvasDisplay 并不总是需要具有相同的大小。 Display.getWidth() 已弃用,建议改用 Display.getSize()。来自documentation :

Gets the size of the display, in pixels.

Note that this value should not be used for computing layouts, since a device will typically have screen decoration (such as a status bar) along the edges of the display that reduce the amount of application space available from the size returned here. Layouts should instead use the window size.

因此,在 canvas 矩阵转换的情况下,始终使用 canvas 作为引用,而不是 Display 或 Window,因为 Canvas 会给出准确的值。因此,要围绕中心旋转 Canvas ,您需要 canvas 的中心。

Canvas 的中心是(canvas.getWidth()/2 , canvas.getHeight()/2)

关于java - Android:围绕屏幕中心旋转 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18028369/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com