簡體   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