gpt4 book ai didi

android - 在不扩展 Activity 的情况下检测 Android 上的屏幕方向变化

转载 作者:行者123 更新时间:2023-11-30 00:39:24 28 4
gpt4 key购买 nike

我有这个适用于 Unity 的 Android 插件。目前它每帧重新计算一次相机的投影矩阵,虽然这有效,但显然非常浪费。我希望仅在屏幕方向发生变化时才能够重新计算矩阵,因为这是唯一需要重新计算的时间。

但是,当我在网上查看时,我似乎能找到的所有答案总是实现 onConfigurationChanged 或类似的东西,最终都要求你做同样的事情:扩展 Activity 类,这是我做不到的事情,因为没有 Activity 。它只是一个构建的库,而不是一个完整的应用程序。

我知道有 OrientationEventListener,但每次设备的方向改变哪怕一度时都会触发,所以这种方法仍然非常浪费。

我想知道的是,有没有一种方法可以检测屏幕方向变化,例如某种不需要扩展 Activity 即可使用的 onConfigurationChanged 事件?

基本上我想要结束的是这样的:

// Event fired only when the screen orientation changes
void onScreenOrientationChanged (ScreenOrientation orientation)
{
recalculateMatrix(orientation);
}

而不是我现在拥有的更像是:

// Method called once per frame
void Draw ()
{
// Draw code
ScreenOrientation orientation = getOrientation();
recalculateMatrix(orientation);
}

最佳答案

正如您提到的,我认为您可以使用 OrientationEventListener 实现:

void onOrientationChanged (int orientation) {
if (orientationIsChanged(orientation)) {
// recalculate matrix
}
}

你必须实现

boolean orientationIsChanged(int angle)

考虑到: “当设备以其自然位置定向时,方向为 0 度,当其左侧位于顶部时为 90 度,当设备倒置时为 180 度,当其右侧位于顶部时为 270 度。ORIENTATION_UNKNOWN 返回时设备接近平面,无法确定方向。”

关于android - 在不扩展 Activity 的情况下检测 Android 上的屏幕方向变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42787605/

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