我怎样才能获得移动使用加速度计的方向? [英] How can I get the direction of movement using an accelerometer?

查看:221
本文介绍了我怎样才能获得移动使用加速度计的方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序,我想知道是否可以检测运动的方向与一个轴固定。例如,我想放在桌子上我的电话和检测方向,当我移动(左,右,上,下​​)。距离是没有必要的,我只是想知道正确的方向。

I'm developing a Android application and I would like to know if is possible detect the direction of movement with one axis fixed. For example, I want put my phone on the table and detect the direction when I move it (left, right, up and down). The distance is not necessary, I just want know the accurate direction.

谢谢!

推荐答案

是的。

使用 SensorEventListener.onSensorChanged(SensorEvent事件)你能确定沿X和放大器提供的值; Y轴。您需要记录这些值,然后把它们比作任何收到的后续调用 onSensorChanged 方法得到一个差值的新值。如果在一个轴上的增量值是正的,那么该装置正在移动的一种方式,如果它的负其移动相反的方向。

Using the SensorEventListener.onSensorChanged(SensorEvent event) you can determine the values provided along the X & Y axis. You would need to record these values and then compare them to any new values that you receive on subsequent calls to the onSensorChanged method to get a delta value. If the delta value on one axis is positive then the device is moving one way, if its negative its moving the opposite way.

您可能需要微调无论是在您收到上,你认为一个delta值指示方向的改变加速度计的事件和阈值的速度。

You will probably need to fine tune both the rate at which you receive accelerometer events and the threshold at which you consider a delta value to indicate a change in direction.

这里有一个快速的code的例子就是我讲的:

Here's a quick code example of what I'm talking about:

public class AccelerometerExample extends Activity implements SensorEventListener {
    TextView textView;
    StringBuilder builder = new StringBuilder();

    float [] history = new float[2];
    String [] direction = {"NONE","NONE"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        textView = new TextView(this);
        setContentView(textView);

        SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        float xChange = history[0] - event.values[0];
        float yChange = history[1] - event.values[1];

        history[0] = event.values[0];
        history[1] = event.values[1];

        if (xChange > 2){
          direction[0] = "LEFT";
        }
        else if (xChange < -2){
          direction[0] = "RIGHT";
        }

        if (yChange > 2){
          direction[1] = "DOWN";
        }
        else if (yChange < -2){
          direction[1] = "UP";
        }

        builder.setLength(0);
        builder.append("x: ");
        builder.append(direction[0]);
        builder.append(" y: ");
        builder.append(direction[1]);

        textView.setText(builder.toString());
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // nothing to do here
    }
}

这code将只为您提供的X和Y轴的手机已经在移动的大方向。为了提供更细粒度确定方向(如试图模仿计算机鼠标的移动),你可能会发现,手机的加速度计是不适合的目的。

This code will only provide you with the general direction on the X and Y axis that the phone has moved in. To provide a more fine grained determination of direction (e.g. to attempt to mimic the movement of a computer mouse) you might find that a phone's accelerometer is not fit for purpose.

要尝试这个问题,我会先设置传感器延迟 SensorManager.SENSOR_DELAY_FAST 并创建多个历史事件的列表,这样我就可以检测出运动时间和不通过轻微移动在相反的方向,服用加速计测量在这样的精细程度时经常发生的影响。你还需要测量的已经通过,以帮助计算如在你的意见提出运动的精确测量的时间量。

To attempt this, I would first set the sensor delay to SensorManager.SENSOR_DELAY_FAST and create a list of multiple history events so that I could detect movement over time and not be influenced by slight movements in the opposite direction that often happen when taking accelerometer measurements at such a fine level. You would also need to measure the amount of time that has passed to help calculate the accurate measure of movement as suggested in your comments.

这篇关于我怎样才能获得移动使用加速度计的方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆