2015-10-14 60 views
0

我使用這個Accelerometer指南android屏幕移動。我對所有的計算和x,y,z值的重要性感到困惑。 z = - 。60是什麼意思?或y = 8.4253?如何使用Android的加速度計

最後,我想知道如何獲得一個值,看他們從左到右移動屏幕的多少,或者在X軸上,因爲我想讓屏幕上的位圖/圖像向左移動如果屏幕向左傾斜/向左移動,並且屏幕向右傾斜則向右移動。

我不知道算法,也不知道這些值是什麼意思,所以對這些信息的任何反饋或指導將是最有益的。

+0

這些值代表在米/秒的軸手機的加速度^ 2,在該頁面上有一個很好的例子:https://cdn.tutsplus.com/mobile/uploads/2014/01/xyz.jpg – Zarwan

+0

所以,如果他們傾斜手機的權利,這將導致一個積極的加速度在X和Y的負面? – user3483163

+0

讀數通常很嘈雜。我建議你嘗試一下。輸出3個傳感器讀數並移動電話並查看其變化情況。 – Zarwan

回答

1

你的活動可以實現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

+0

精美的工作。 – user3483163