Android 自然传感器方向帮助

发布于 2024-11-18 17:44:55 字数 1994 浏览 2 评论 0 原文

我正在尝试实现 Reto Meier 推荐的保持屏幕方向不变的方法。他在 Google IO 期间演讲的幻灯片(参见 #23)可以在 Android 专业提示:在哪里下载幻灯片和代码片段

我已单步执行代码并设置值,但屏幕方向仍然发生变化。仅供参考,我在应用程序中注册了这个监听器。

这是我的代码:

final SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    sm.registerListener(
        new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent sensorEvent) {
                if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
                    final WindowManager wm = (WindowManager) getApplicationContext()
                            .getSystemService(Context.WINDOW_SERVICE);
                    final Display display = wm.getDefaultDisplay();

                    int x = SensorManager.AXIS_X;
                    int y = SensorManager.AXIS_Y;

                    switch (display.getRotation()) {
                    case Surface.ROTATION_90:
                        x = SensorManager.AXIS_Y;
                        y = SensorManager.AXIS_MINUS_X;

                        break;
                    case Surface.ROTATION_180:
                        y = SensorManager.AXIS_MINUS_Y;

                        break;
                    case Surface.ROTATION_270:
                        x = SensorManager.AXIS_MINUS_Y;
                        y = SensorManager.AXIS_MINUS_X;

                        break;
                    case Surface.ROTATION_0:
                    default:
                        break;
                    }

                    SensorManager.remapCoordinateSystem(sensorEvent.values, x, y, new float[] {});
                }
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        }, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),
        SensorManager.SENSOR_DELAY_NORMAL);

I am trying to accomplish Reto Meier's recommended way for keeping the screen orientation from changing. The slides from his talk during Google IO (see #23) can be found in Android Protips: Where to Download the Slides and Code Snippets.

I have stepped through the code and it setting the values, but the screen orientation still changes. FYI, I register this listener in the Application.

Here is my code:

final SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    sm.registerListener(
        new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent sensorEvent) {
                if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
                    final WindowManager wm = (WindowManager) getApplicationContext()
                            .getSystemService(Context.WINDOW_SERVICE);
                    final Display display = wm.getDefaultDisplay();

                    int x = SensorManager.AXIS_X;
                    int y = SensorManager.AXIS_Y;

                    switch (display.getRotation()) {
                    case Surface.ROTATION_90:
                        x = SensorManager.AXIS_Y;
                        y = SensorManager.AXIS_MINUS_X;

                        break;
                    case Surface.ROTATION_180:
                        y = SensorManager.AXIS_MINUS_Y;

                        break;
                    case Surface.ROTATION_270:
                        x = SensorManager.AXIS_MINUS_Y;
                        y = SensorManager.AXIS_MINUS_X;

                        break;
                    case Surface.ROTATION_0:
                    default:
                        break;
                    }

                    SensorManager.remapCoordinateSystem(sensorEvent.values, x, y, new float[] {});
                }
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        }, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),
        SensorManager.SENSOR_DELAY_NORMAL);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2024-11-25 17:44:55

当前不起作用的所有 30 多行代码都可以替换为 AndroidManifest.xml 文件中的一些 XML 值。

我们都见过

<activity android:name=".YourActivity" android:screenOrientation="portrait" ></activity>

并且都知道这对于具有默认横向方向的平板电脑来说效果不佳。但你们有多少人见过这个?

<activity android:name=".YourActivity" android:screenOrientation="nosensor" ></activity>

基本上它使得设备的方向不会对传感器做出响应。因此,如果您的默认设置是横向或纵向,则它不会改变。我已经在我的 Droid XXoom 它的工作原理就像我所期望的那样。

我希望这对其他人有帮助。

All 30+ lines of code that currently are not working can be replaced with some XML values in your AndroidManifest.xml file.

We have all seen

<activity android:name=".YourActivity" android:screenOrientation="portrait" ></activity>

and we all know that doesn't work well with tablets that have a default orientation of landscape. But how many of you have seen this?

<activity android:name=".YourActivity" android:screenOrientation="nosensor" ></activity>

Basically it makes it so that the device's orientation doesn't respond to the sensor. So if your default is landscape or portrait it won't change. I have tested it on my Droid X and on a Xoom and it works like I would have expected it to.

I hope this helps others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文