如何获得带有传感器的手机的角度/度数? [英] How do I get the angle/degree of the phone with sensor/s?

查看:217
本文介绍了如何获得带有传感器的手机的角度/度数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用TYPE_ACCELEROMETER传感器获取电话角度. 我的目标是仅在手机倾斜后才能获得角度值. 它有效,但是问题是 当我将手机正面朝上放在桌子上时, 它仍然说isLandscape = true;

I am trying to get the phone angles by using TYPE_ACCELEROMETER sensor. My goal is to get the angle values only after the phone is tilted. It works, but the problem is when I put my phone facing up on the table, it still says isLandscape = true;

private boolean isLandscape;

mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(
                                          Sensor.TYPE_ACCELEROMETER),1000000);
private final SensorEventListener mSensorListener = new SensorEventListener() { 
    @Override
    public void onSensorChanged(SensorEvent mSensorEvent) {  
        float X_Axis = mSensorEvent.values[0];
        float Y_Axis = mSensorEvent.values[1];
        double angle = Math.atan2(X_Axis, Y_Axis)/(Math.PI/180);

        if(!isLandscape) {                  
            if(angle > 80) {
                Orientation = 90;
                isLandscape = true;
            }
        }
        else 
        {

            if(Math.abs(angle) < 10) {
                Orientation = 0;  //portrait
                isLandscape = false;
            }
        }
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
};

仅当手机倾斜后才能获得手机角度的最佳方法是什么? 对不起,英语不好,

What's the best way to get the phone angles only after the phone is tilted? I am sorry for bad English,

谢谢.

推荐答案

我不知道我是否理解您的问题,但是我认为您希望您的应用仅在手机处于纵向状态时计算倾斜角度,首先您需要取值mSensorEvent.values[0],在这种情况下,如果处于站立状态的手机将返回0,则向右倾斜将是1到9之间的负值,而向左倾斜是正值.

I don't know if i understand your question ,but i think you want your app to calculate the angel of tilt only if the phone is in portrait ,first you need to take the value of mSensorEvent.values[0] and in this case if the phone in stand state in will return 0 , tilt to right will be negative values from 1 to 9 ,and the left positive .

然后,仅当mSensorEvent.values[1]值在9到7之间时,就必须执行所有这些操作(9是完美的标准).确保设备处于纵向位置.

then you have to do all this just in case of mSensorEvent.values[1] values between 9 and 7 for example (9 is perfect stand) . to ensure the the device in portrait position .

,如果需要度角值,可以将mSensorEvent.values的浮点值乘以10.

and if you need the degree angle values you can multiply the float value of mSensorEvent.values by 10.

我希望这对您有帮助

更新***

您可以尝试以下方法:

    private boolean isLandscape;

    @Override 
    public void onSensorChanged(SensorEvent mSensorEvent) {   
        float X_Axis = mSensorEvent.values[0]; 
        float Y_Axis = mSensorEvent.values[1]; 

        if((X_Axis <= 6 && X_Axis >= -6) && Y_Axis > 5){
        isLandscape = false; 
        }
        else if(X_Axis >= 6 || X_Axis <= -6){
        isLandscape = true;
        }

    } 

这篇关于如何获得带有传感器的手机的角度/度数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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