【问题标题】:How to use Android's Accelerometer如何使用 Android 的加速度计
【发布时间】:2015-10-14 01:10:46
【问题描述】:

我使用这个Accelerometer 指南来进行安卓屏幕移动。我对所有计算以及 x、y、z 值的重要性感到困惑。 z=-.60 表示什么?还是 y=8.4253?

最终,我想知道如何获取一个值,以查看它们从左到右或在 X 轴上移动了多少屏幕,因为我想让屏幕上的位图/图像向左移动,如果屏幕向左倾斜/移动,如果屏幕向右倾斜,它向右移动。

我不知道这方面的算法,也不知道这些值的含义,因此对这些信息的任何反馈或指导都是最有益的。

【问题讨论】:

  • 这些值代表手机在这些轴上的加速度,单位为 m/s^2,该页面上有一个很好的说明:cdn.tutsplus.com/mobile/uploads/2014/01/xyz.jpg
  • 因此,如果他们将手机向右倾斜,会导致 x 方向的正加速度和 y 方向的负加速度?
  • 读数通常很嘈杂。我建议你尝试一下。输出 3 个传感器读数并移动手机,看看它是如何变化的。

标签: java android accelerometer gyroscope


【解决方案1】:

您的 Activity 可以像这样实现 SensorEventListener,覆盖 onSensorChanged(SensorEvent event)

public void onSensorChanged(SensorEvent event) {
    float x = event.values[0];
    float y = event.values[1];
    if (Math.abs(x) > Math.abs(y)) {
        if (x < 0) {
            image.setImageResource(R.drawable.right);
            textView.setText("You tilt the device right");
        }
        if (x > 0) {
            image.setImageResource(R.drawable.left);
            textView.setText("You tilt the device left");
        }
    } else {
        if (y < 0) {
            image.setImageResource(R.drawable.up);
            textView.setText("You tilt the device up");
        }
        if (y > 0) {
            image.setImageResource(R.drawable.down);
            textView.setText("You tilt the device down");
        }
    }
    if (x > (-2) && x < (2) && y > (-2) && y < (2)) {
        image.setImageResource(R.drawable.center);
        textView.setText("Not tilt device");
    }
}

更多详情,请参阅我的完整帖子:http://www.devexchanges.info/2015/05/detecting-tilt-device-by-using-sensor.html

【讨论】:

    猜你喜欢
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多