Android的天然传感器定位帮助 [英] Android natural sensor orientation help

查看:217
本文介绍了Android的天然传感器定位帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图完成雷托•迈耶的推荐方式进行更改保持屏幕的方向。从在谷歌IO他谈话的幻灯片(见#23)可以在 Android的Protips:凡下载幻灯片和code片段

我已经经历了code踩并将其设置的值,但屏幕方向仍改变。仅供参考,我在申请注册该监听器。

下面是我的code:

 最后的SensorManager SM =(的SensorManager)getSystemService(Context.SENSOR_SERVICE);
    sm.registerListener(
        新SensorEventListener(){
            @覆盖
            公共无效onSensorChanged(SensorEvent sensorEvent){
                如果(sensorEvent.sensor.getType()== Sensor.TYPE_ORIENTATION){
                    最后的窗口管理器,WM =(窗口管理器)getApplicationContext()
                            .getSystemService(Context.WINDOW_SERVICE);
                    最终显示显示= wm.getDefaultDisplay();

                    INT X = SensorManager.AXIS_X;
                    INT Y = SensorManager.AXIS_Y;

                    开关(display.getRotation()){
                    案例Surface.ROTATION_90:
                        X = SensorManager.AXIS_Y;
                        Y = SensorManager.AXIS_MINUS_X;

                        打破;
                    案例Surface.ROTATION_180:
                        Y = SensorManager.AXIS_MINUS_Y;

                        打破;
                    案例Surface.ROTATION_270:
                        X = SensorManager.AXIS_MINUS_Y;
                        Y = SensorManager.AXIS_MINUS_X;

                        打破;
                    案例Surface.ROTATION_0:
                    默认:
                        打破;
                    }

                    SensorManager.remapCoordinateSystem(sensorEvent.values​​,X,Y,新的浮动[] {});
                }
            }

            @覆盖
            公共无效onAccuracyChanged(传感器传感器,诠释精度){

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

解决方案

所有30+线code,目前没有工作可以与某些XML值来代替你的的Andr​​oidManifest.xml 文件。

我们都看到

 <活动机器人:名称=机器人YourActivity。:screenOrientation =画像和GT;< /活性GT;
 

和我们都知道,不会有景观的默认方向片很好地工作。但是,你们有多少人看到了这?

 <活动机器人:名称=机器人YourActivity。:screenOrientation =nosensor>< /活性GT;
 

基本上,它使得它使得该装置的方向不会对传感器作出反应。所以,如果您的默认设置为横向或纵向不会改变。我已经在我的的Droid&NBSP对其进行了测试,X 并在的 Xoom的和它的作品就像我早就预料到。

我希望这可以帮助别人。

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);

解决方案

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.

这篇关于Android的天然传感器定位帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆