这个加速度传感器监听器背后使用了什么逻辑

发布于 2024-11-28 07:04:40 字数 3632 浏览 2 评论 0原文

这是我用来捕获手机震动的代码:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够钟 2024-12-05 07:04:40

将其添加到您的活动类中

/* put this into your activity class */
private SensorManager mSensorManager;
private float mAccel; // acceleration apart from gravity
private float mAccelCurrent; // current acceleration including gravity
private float mAccelLast; // last acceleration including gravity

private final SensorEventListener mSensorListener = new SensorEventListener() {

 public void onSensorChanged(SensorEvent se) {
  float x = se.values[0];
  float y = se.values[1];
  float z = se.values[2];
  mAccelLast = mAccelCurrent;
  mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
  float delta = mAccelCurrent - mAccelLast;
  mAccel = mAccel * 0.9f + delta; // perform low-cut filter
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
 };

 @Override
 protected void onResume() {
  super.onResume();
 mSensorManager.registerListener(mSensorListener,   mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),   SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onStop() {
mSensorManager.unregisterListener(mSensorListener);
super.onStop();
}

并将其添加到您的 onCreate 方法中:

 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener,    mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
mAccel = 0.00f;
mAccelCurrent = SensorManager.GRAVITY_EARTH;
mAccelLast = SensorManager.GRAVITY_EARTH;

我希望这会有所帮助

add this in your activity class

/* put this into your activity class */
private SensorManager mSensorManager;
private float mAccel; // acceleration apart from gravity
private float mAccelCurrent; // current acceleration including gravity
private float mAccelLast; // last acceleration including gravity

private final SensorEventListener mSensorListener = new SensorEventListener() {

 public void onSensorChanged(SensorEvent se) {
  float x = se.values[0];
  float y = se.values[1];
  float z = se.values[2];
  mAccelLast = mAccelCurrent;
  mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
  float delta = mAccelCurrent - mAccelLast;
  mAccel = mAccel * 0.9f + delta; // perform low-cut filter
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
 };

 @Override
 protected void onResume() {
  super.onResume();
 mSensorManager.registerListener(mSensorListener,   mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),   SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onStop() {
mSensorManager.unregisterListener(mSensorListener);
super.onStop();
}

And add this to your onCreate method:

 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener,    mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
mAccel = 0.00f;
mAccelCurrent = SensorManager.GRAVITY_EARTH;
mAccelLast = SensorManager.GRAVITY_EARTH;

i hope this will help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文