检测手机​​方位传感器事件 [英] Detecting phone orientation with sensor events

查看:116
本文介绍了检测手机​​方位传感器事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,它需要它的方向被锁定

I have an Activity, which needs its orientation to be locked with

setRequestedOrientation(screenOrientation);

不过,我想的方向更新,这样我可以做调整UI(想象一下HTC摄像头的应用程序,当只有按钮的图标改变方向)。 所以,我发现此类。它提供方向值,从0到360豪我过滤此值,即完善间隔 [A,B] ,如果 A< X - LT ; b 则取向研究是横向或纵向?计算是什么意思?任何提示?

But I want to get orientation updates, so that I can do adjustments to the UI(imagine HTC camera app, when only button's icons change orientation). So I found this class. It delivers orientation values, between 0 and 360. Ho do I filter this values, i.e. perfect interval [a, b], and if a<x<b then orientaion is landscape or portrait? Compute mean? Any hints?

推荐答案

这听起来像你需要code当设备的方向改变为4正常的方向,而不是在每一个角度一个才反应过来。这将过滤的取向为0,90,180和270度只值:

It sounds like you are needing code to only react when the device's orientation has changed to one of the 4 normal orientations instead of at every angle. This will filter the orientation to only values of 0, 90, 180 and 270 degrees:

OrientationEventListener myOrientationEventListener;
    int iOrientation;

    myOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL)
    {

        @Override
        public void onOrientationChanged(int iAngle)
        {                         // 0  15 30 45 60 75, 90 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345
            final int iLookup[] = {0,   0, 0,90,90, 90,90, 90,  90, 180, 180, 180, 180, 180, 180, 270, 270, 270, 270, 270, 270, 0, 0, 0}; // 15-degree increments 
            if (iAngle != ORIENTATION_UNKNOWN)
            {
                int iNewOrientation = iLookup[iAngle / 15];
                if (iOrientation != iNewOrientation)
                {
                    iOrientation = iNewOrientation;
                    // take action on new orientation here
                }
            }
        }
    };

    // To display if orientation detection will work and enable it
    if (myOrientationEventListener.canDetectOrientation())
    {
    Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG).show();
        myOrientationEventListener.enable();
    }
    else
    {
    Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG).show();
    }

这篇关于检测手机​​方位传感器事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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