简体   繁体   English

如何使用Android的加速度计

[英]How to use Android's Accelerometer

I used this Accelerometer guide for android screen movement. 我将此加速度计指南用于Android屏幕移动。 I am confused about all the calculations and the significance of the x, y, z values. 我对所有计算和x,y,z值的含义感到困惑。 What does az=-.60 signify? az =-。60表示什么? or ay=8.4253? 或ay = 8.4253?

Ultimately, I would like to know how to get a value to see how much they are moving the screen left-to-right or in the X-axis because I want to make a bitmap/image on the screen move left if the screen is tilted/moved left and it move right if the screen is tilted right. 最终,我想知道如何获取一个值,以查看他们在屏幕上左右移动或X轴移动了多少,因为我想使屏幕上的位图/图像向左移动。向左倾斜/移动,如果屏幕向右倾斜,则向右移动。

I do not know the algorithm for that nor do I know what the values mean so any feedback or guidance upon this information would be most beneficial. 我不知道该算法,也不知道这些值的含义,因此,对此信息的任何反馈或指导将是最有益的。

Your Activity can implement SensorEventListener , override onSensorChanged(SensorEvent event) like this: 您的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");
    }
}

More details, see my full post at: http://www.devexchanges.info/2015/05/detecting-tilt-device-by-using-sensor.html 更多详细信息,请参阅我的完整文章: http : //www.devexchanges.info/2015/05/detecting-tilt-device-by-using-sensor.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM