这个加速度传感器监听器背后使用了什么逻辑
这是我用来捕获手机震动的代码:
public void onSensorChanged(SensorEvent event) {
Sensor mySensor = event.sensor;
if (mySensor.getType() == SensorManager.SENSOR_ACCELEROMETER) {
long curTime = System.currentTimeMillis();
// only allow one update every 100ms.
if ((curTime - lastUpdate) > 100) {
long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;
x = event.values[SensorManager.DATA_X];
y = event.values[SensorManager.DATA_Y];
z = event.values[SensorManager.DATA_Z];
float speed = Math.abs(x+y+z - last_x - last_y - last_z)/ diffTime * 10000;
if (speed > SHAKE_THRESHOLD_HIGH) {
// yes, this is a shake action! Do something about it!
handshake_score.setText(String.valueOf(speed));
new AlertDialog.Builder(Handshake.this)
.setCancelable(true)
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("noo")
.setMessage("too much")
.setPositiveButton("OK!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
})
.show();
last_x = x;
last_y = y;
last_z = z;
} else if (speed > SHAKE_THRESHOLD_PASS) {
// yes, this is a shake action! Do something about it!
handshake_score.setText(String.valueOf(speed));
sensorMgr.unregisterListener(this);
new AlertDialog.Builder(Handshake.this)
.setCancelable(true)
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("HAHAA!")
.setMessage("good")
.setPositiveButton("OK!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
})
.show();
finish();
} else if (speed > SHAKE_THRESHOLD_SMALL) {
// yes, this is a shake action! Do something about it!
handshake_score.setText(String.valueOf(speed));
new AlertDialog.Builder(Handshake.this)
.setCancelable(true)
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("boo")
.setMessage("not enough")
.setPositiveButton("OK!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
})
.show();
last_x = x;
last_y = y;
last_z = z;
}
last_x = x;
last_y = y;
last_z = z;
}
}
}
我想做的是模仿握手,这样它就会在水平握住手机时上下抬起手机做出响应,就好像您面对横向屏幕并移动一样它上下。
虽然此代码响应扭转手机,就像您绕其长度轴旋转手机一样。
有人可以解释一下这段代码中使用的坐标 x、y 和 z 背后的逻辑吗?这些坐标在手机中的位置在哪里,以及我如何让 phoen 响应“握手”而不是在手中扭转它。
对于这样的代码有什么想法吗?
This is the code i am using to capture the shake of the phone:
public void onSensorChanged(SensorEvent event) {
Sensor mySensor = event.sensor;
if (mySensor.getType() == SensorManager.SENSOR_ACCELEROMETER) {
long curTime = System.currentTimeMillis();
// only allow one update every 100ms.
if ((curTime - lastUpdate) > 100) {
long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;
x = event.values[SensorManager.DATA_X];
y = event.values[SensorManager.DATA_Y];
z = event.values[SensorManager.DATA_Z];
float speed = Math.abs(x+y+z - last_x - last_y - last_z)/ diffTime * 10000;
if (speed > SHAKE_THRESHOLD_HIGH) {
// yes, this is a shake action! Do something about it!
handshake_score.setText(String.valueOf(speed));
new AlertDialog.Builder(Handshake.this)
.setCancelable(true)
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("noo")
.setMessage("too much")
.setPositiveButton("OK!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
})
.show();
last_x = x;
last_y = y;
last_z = z;
} else if (speed > SHAKE_THRESHOLD_PASS) {
// yes, this is a shake action! Do something about it!
handshake_score.setText(String.valueOf(speed));
sensorMgr.unregisterListener(this);
new AlertDialog.Builder(Handshake.this)
.setCancelable(true)
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("HAHAA!")
.setMessage("good")
.setPositiveButton("OK!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
})
.show();
finish();
} else if (speed > SHAKE_THRESHOLD_SMALL) {
// yes, this is a shake action! Do something about it!
handshake_score.setText(String.valueOf(speed));
new AlertDialog.Builder(Handshake.this)
.setCancelable(true)
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("boo")
.setMessage("not enough")
.setPositiveButton("OK!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
})
.show();
last_x = x;
last_y = y;
last_z = z;
}
last_x = x;
last_y = y;
last_z = z;
}
}
}
What i want to do is imitate a handshake so it would respond to lifting the phone up and down when holding it horizontally as if you`re facing the landscape screen and moving it up and down.
While this code resopnds to twisting the phone as if you spin the phone around its length axis.
Can someone explain the logics behind coordinates x, y and z used in this code, where are these in terms of place in the phone and how i could have the phoen responding to "handshakes" rather then twisting it in your hand.
any ideas for such code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其添加到您的活动类中
并将其添加到您的 onCreate 方法中:
我希望这会有所帮助
add this in your activity class
And add this to your onCreate method:
i hope this will help