何时在 Android 上使用加速度计或陀螺仪
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20693547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use accelerometer or gyroscope on Android
提问by user2892857
Is it ok to assume most of the user devices will have a gyroscope? In other words will I be excluding a significant number of people by using a gyroscope in my app?
可以假设大多数用户设备都有陀螺仪吗?换句话说,我会通过在我的应用程序中使用陀螺仪来排除大量的人吗?
I'm making a children's storybook app and I'm wanting the user to be able to tilt their device around the yaw axis to move a rocking chair back and forth. It's a small part of the app and it doesn't have to be very precise. Is there one sensor I should use over the other?
我正在制作一个儿童故事书应用程序,我希望用户能够围绕偏航轴倾斜他们的设备来前后移动摇椅。它只是应用程序的一小部分,不需要非常精确。我应该在另一个传感器上使用一个传感器吗?
回答by foibs
User coverage:
用户覆盖:
Most devices have both gyroscope and accelerometer, but almost everydevice has an accelerometer. Especially the older devices and the cheaper ones do not have a gyroscope. Don't forget that since it is a kids app, maybe the parents don't want the little ones to use their superb 700$ tablet, and will probably use cheaper ones. Cheaper devices tend to not have gyroscopes. So +1 for the accelerometer.
大多数设备都有陀螺仪和加速度计,但几乎每个设备都有一个加速度计。特别是旧设备和便宜的设备没有陀螺仪。不要忘记,因为它是一个儿童应用程序,也许父母不希望小孩子使用他们出色的 700 美元平板电脑,而可能会使用更便宜的平板电脑。更便宜的设备往往没有陀螺仪。所以加速度计+1。
User experience
用户体验
The accelerometer measures acceleration in the 3-dimensional coordinate system. In most cases (when the user actually does not jump around or throw the device) the biggest part of acceleration is gravity. With gravity alone it is very easy to determine tilt and slight movements. Plus you have the Gravity software sensor to eliminate strange movements, and Linear Acceleration to eliminate Gravity. The Gyroscope detects rotation. It is more sensitive, more precise and produces events faster than the accelerometer, but it seems to be an overkill for this usecase. If you wanted to make a 3D racing game or a flight simulator It'd be a win for the gyroscope, but for a kids app it's just too much.
加速度计测量 3 维坐标系中的加速度。在大多数情况下(当用户实际上没有跳来跳去或扔设备时)加速度的最大部分是重力。仅靠重力就很容易确定倾斜和轻微的运动。此外,您还可以使用 Gravity 软件传感器来消除奇怪的运动,并使用 Linear Acceleration 来消除 Gravity。陀螺仪检测旋转。它比加速度计更敏感、更精确并且产生事件的速度更快,但对于这个用例来说似乎有点矫枉过正。如果您想制作 3D 赛车游戏或飞行模拟器,陀螺仪将是一个胜利,但对于儿童应用程序而言,这太过分了。
Battery Usage
电池使用
No contest here. The gyroscope uses 3-30 times more battery (maybe more, depending on device), while the accelerometer is very gentle on the battery. Plus, for most users the accelerometer is already active (to rotate the screen automatically) so there is no sensor battery usage here. +1 for the accelerometer
这里没有比赛。陀螺仪使用 3-30 倍的电池(可能更多,取决于设备),而加速度计对电池的消耗非常小。另外,对于大多数用户来说,加速度计已经处于活动状态(自动旋转屏幕),因此这里没有传感器电池使用。+1 为加速度计
Programming
编程
Sensors are pretty easy and straightforward to implement in Android apps. Since the gyroscope detects rotation speed, it gives all 0 values if the device is left still (and maybe a little noise in the range of +-0.01rad/s), so you only need a small if
block to dismiss very slight movements (e.g. when gyroscope values are less than 0.2 rad/s). With the accelerometer, you need some extra calculations to determine the orientation of the device and which axis is actually the one that needs more attention to determine the direction of movement. It's not hard to do, but it does add a few extra lines of code and some extra debugging to your line of work. +1 for the gyroscope.
在 Android 应用程序中实现传感器非常简单和直接。由于陀螺仪检测旋转速度,如果设备保持静止,它会给出所有 0 值(可能会有 +-0.01rad/s 范围内的一点噪音),所以你只需要一个小块if
来消除非常轻微的移动(例如当陀螺仪值小于 0.2 rad/s 时)。使用加速度计,您需要进行一些额外的计算来确定设备的方向,以及哪个轴实际上是需要更多注意来确定移动方向的轴。这并不难,但它确实为您的工作线添加了一些额外的代码行和一些额外的调试。+1 为陀螺仪。
Conclusion
结论
For a simple kids app, accelerometer is the way to go. I wouldn't think about it any more. Since you don't care much about precision, you actually eliminate the points gained by the gyroscope.
对于一个简单的儿童应用程序,加速度计是要走的路。我不会再考虑了。既然你不太在意精度,你实际上消除了陀螺仪获得的点数。
回答by NigelK
I think these sensors are the ones to consider (I'm quoting from the book "Reto Meier, Professional Android 4 Application Development" here):
我认为这些传感器是需要考虑的(我在这里引用了“Reto Meier,Professional Android 4 Application Development”一书):
Sensor.TYPE_ACCELEROMETER — A three-axis accelerometer that returns the current acceleration along three axes in m/ s2 (meters per second, per second.)
Sensor.TYPE_GYROSCOPE— A three-axis gyroscope that returns the rate of device rotation along three axes in radians/ second. You can integrate the rate of rotation over time to determine the current orientation of the device; however, it generally is better practice to use this in combination with other sensors (typically the accelerometers) to provide asmoothed and corrected orientation.
Sensor.TYPE_ROTATION_VECTOR— Returns the orientation of the device as a combination of an angle around an axis. It typically is used as an input to the getRotationMatrixFromVector method from the Sensor Manager to convert the returned rotation vector into a rotation matrix. The rotation vector Sensor typically is implemented as a virtual sensor that can combine and correct the results obtained from multiple sensors, such as the accelerometers and gyroscopes, to provide a smoother rotation matrix.
Sensor.TYPE_ACCELEROMETER — 三轴加速度计,以 m/s2(米每秒,每秒)为单位返回沿三个轴的当前加速度。
Sensor.TYPE_GYROSCOPE — 三轴陀螺仪,以弧度/秒为单位返回设备沿三个轴的旋转速率。您可以随着时间的推移对旋转速率进行积分,以确定设备的当前方向;然而,通常更好的做法是将其与其他传感器(通常是加速度计)结合使用以提供平滑和校正的方向。
Sensor.TYPE_ROTATION_VECTOR——以围绕轴的角度组合的形式返回设备的方向。它通常用作来自传感器管理器的 getRotationMatrixFromVector 方法的输入,以将返回的旋转矢量转换为旋转矩阵。旋转矢量传感器通常作为虚拟传感器实现,它可以组合和校正从多个传感器(例如加速度计和陀螺仪)获得的结果,以提供更平滑的旋转矩阵。
End of quoting... You can find the default Sensor implementation for a given type using the getDefaultSensor method, e.g.:
引用结束...您可以使用 getDefaultSensor 方法找到给定类型的默认 Sensor 实现,例如:
Sensor defaultGyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
If that returns null, the device doesn't have the sensor so you know not to try and use it. I expect the majority of devices do have these sensors (most likely the accelerometer) since they are the ones used to detect screen rotation.
如果返回 null,则设备没有传感器,因此您知道不要尝试使用它。我希望大多数设备都有这些传感器(很可能是加速度计),因为它们是用于检测屏幕旋转的传感器。
回答by baash05
I believe most phones have the ability to tilt. I have a cheep 2 year old phone that was 45$ when it was new, and is running android 2.1, it tilts and changes perspective.
I also have a tablet that's three years old, and it too tilts.
I'd say you're quite safe assuming the tilt will be there.
我相信大多数手机都有倾斜的能力。我有一部便宜的 2 年旧手机,它的新手机售价为 45 美元,并且运行的是 android 2.1,它会倾斜并改变视角。
我也有一台使用了三年的平板电脑,它也倾斜了。
我会说你很安全,假设倾斜会在那里。
回答by Basile Perrenoud
Almost all (or even all) phone will have an accelerometer. But not all of them have a gyroscope. Someone made a non exhaustive list of phone with gyrosope here: Which Android phones out there do have a gyroscope?
几乎所有(甚至所有)手机都会有一个加速度计。但并非所有人都有陀螺仪。有人在这里列出了带有陀螺仪的手机的非详尽列表:哪些 Android 手机确实有陀螺仪?
If you can manage to make it work using only the accelerometer, its certain more devices will support your app, but I can't really tell you how big this difference is
如果您可以设法仅使用加速度计使其工作,那么它的某些设备将支持您的应用程序,但我无法真正告诉您这种差异有多大
回答by RBerteig
Over time, the number and types of sensors available in a "typical" android device has changed fairly dramatically. But some means of measuring the orientation of the device is among the older capabilities. It would appear that the correct approach is to test at run time for the newer sensors, and fall back to older ones, while gracefully handling the case where the information is simply not available.
随着时间的推移,“典型”Android 设备中可用的传感器数量和类型发生了相当大的变化。但是某些测量设备方向的方法属于较旧的功能。似乎正确的方法是在运行时测试较新的传感器,然后回退到较旧的传感器,同时优雅地处理信息根本不可用的情况。
The Sensor Overiewdeveloper document has a table showing when specific sensor types were first made available by Android API level. But do note that this just reflects the capability of that API level to talk about that data type; a specific device might still be missing the required hardware.
该传感器Overiew显影剂文档具有示出当特定类型的传感器由Android API级首先提供的表。但请注意,这仅反映了该 API 级别谈论该数据类型的能力;特定设备可能仍然缺少所需的硬件。
It does appear that accelerometer and gyroscope are among the oldest sensors and are likely present in most handsets.
加速度计和陀螺仪似乎是最古老的传感器之一,并且可能存在于大多数手机中。
Right now (December, 2013) the developer dashboardis showing that all but about 1.6% of android devices accessing the Play Store are version 2.3 or newer (API 10 or newer). That data is based solely on version 2.2 or newer devices using the store, devices older than 2.2 were only about 1% of all devices checking in to any Google server in August of 2013. That page includes information about device display resolution, but does not appear to cover other features of devices. I could not find a dashboard that specifically covered what sensors are available in the wild.
现在(2013 年 12 月),开发者仪表板显示,访问 Play 商店的 Android 设备中,只有大约 1.6% 是 2.3 或更高版本(API 10 或更高版本)。该数据仅基于使用该商店的 2.2 版或更新版本的设备,2013 年 8 月,2.2 版之前的设备仅占所有登录到任何 Google 服务器的设备的约 1%。该页面包含有关设备显示分辨率的信息,但不包含似乎涵盖了设备的其他功能。我找不到专门涵盖野外可用传感器的仪表板。