如何使用 Android 的加速度计
How to use Android's Accelerometer
我使用此 Accelerometer 指南进行 android 屏幕移动。我对所有计算和 x、y、z 值的意义感到困惑。 z=-.60 表示什么?或者 y=8.4253?
最终,我想知道如何获取一个值以查看他们从左到右或沿 X 轴移动屏幕的程度,因为我想在 bitmap/image如果屏幕向左tilted/moved,则屏幕向左移动;如果屏幕向右倾斜,则屏幕向右移动。
我不知道该算法,也不知道这些值的含义,因此对此信息的任何反馈或指导都是最有益的。
您的 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");
}
}
更多详情,请参阅我的完整 post 网址:http://www.devexchanges.info/2015/05/detecting-tilt-device-by-using-sensor.html
我使用此 Accelerometer 指南进行 android 屏幕移动。我对所有计算和 x、y、z 值的意义感到困惑。 z=-.60 表示什么?或者 y=8.4253?
最终,我想知道如何获取一个值以查看他们从左到右或沿 X 轴移动屏幕的程度,因为我想在 bitmap/image如果屏幕向左tilted/moved,则屏幕向左移动;如果屏幕向右倾斜,则屏幕向右移动。
我不知道该算法,也不知道这些值的含义,因此对此信息的任何反馈或指导都是最有益的。
您的 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");
}
}
更多详情,请参阅我的完整 post 网址:http://www.devexchanges.info/2015/05/detecting-tilt-device-by-using-sensor.html