繁体   English   中英

如何使用Android的加速度计

[英]How to use Android's Accelerometer

我将此加速度计指南用于Android屏幕移动。 我对所有计算和x,y,z值的含义感到困惑。 az =-。60表示什么? 或ay = 8.4253?

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

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

您的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

暂无
暂无

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

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