检测设备方向 [英] Detecting device orientation

查看:31
本文介绍了检测设备方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在不手动播放传感器数据的情况下检测 android 设备方向变化,同时保持活动方向坚持某个方向

I need to detect android device orientation change without playing manually with sensor data, while keeping activity orientation stick to some orientation

onConfigurationChange 将无法工作,因为我的 Activity 不会旋转.

onConfigurationChange will not work as will stick my activity to not rotate.

使用传感器数据来检测方向变化我认为这是轮的发明,因为 android 已经嵌入了算法的实现来检测设备方向变化.而从另一方面来说,方向变化的检测并不是像这样简单的检查.

Playing around with sensor data to detect the orientation change I consider that as an invention of wheel, as android already does have embedded implementation of the algorithm to detect device orientation change. And from another side the detection of orientation change is not a simple checks like this.

    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER || event.values.length < 3) {
            return;
        }
        //getResources().getConfiguration().orientation;
        if (event.values[1] < 6.5 && event.values[1] > -6.5) {
            if (orientation!=1) {
                Log.d("Sensor", "Protrait");
            }
            orientation=1;
        } else {
            if (orientation!=0) {
                Log.d("Sensor", "Landscape");
            }
            orientation=0;
        }
   }

它确实需要真正的数据处理,例如过滤噪音和突然的短暂震动/旋转.

It does really require real data processing like filtration from noise and sudden short shakes/rotations.

那么有什么想法可以使用传统服务检测设备方向吗?(再一次,我会将我的活动坚持到某个方向,这样 onConfigurationChange 将不起作用)

So any ideas how the device orientation can be detected using legacy services ? (Once again, I'll stick my activity to some orientation so onConfigurationChange will not work)

推荐答案

Orientation-Event 有一个监听器.

There is a Listener for Orientation-Event.

在此处查看文档.

SO 问题提及该监听器的实现.

本博客中相同的代码示例

希望对你有帮助

这篇关于检测设备方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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