使用震动动作从加速度计获取方向

使用震动动作从加速度计获取方向

本文介绍了使用震动动作从加速度计获取方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个Android应用,可以使用Accelerometer一次摇动时播放下一首歌曲。

I have made android app that plays next song when shake once using Accelerometer.

现在,我想要在应用程序中显示的内容是:如果在右侧摇动手机应该播放下一首歌曲,或者在向左摇动手机之前播放,可以我使用摇动动作时发现的轴逻辑进行操作(如果可能的话)
我如何将轴置于运动检测方法中以了解移动台摇动的方向。如果有人对此有逻辑,请帮助我,将我困在这里。

Now what i want my in app if a shake the phone in right side it should play the next song in line or if shake in left play previous, can i do it with the logic of axis found at time of shake action if that is possiblehow can I put axis to the methods of motion detection to know the direction in which the mobile is shaked. If any one have a logic for this please help i am stuck here.

推荐答案

我做了一些深入的研究,并能够自己解决。但是在发布答案时,我,这比我的实现更好地解释了。因此,我发现它更有帮助。

I did some digging into this and was able to resolve it myself. but while posting the answer I found this answer by Basil which explains better than my implementation. So, i found it more helpful.

onSensorChanged 每当设备收到新值时,都会在延迟一段时间后将值存储到变量中。之后,将新值与以前的值进行比较。这导致计算出设备抖动的方向。如果答案仍然不清楚,请告诉我。

In the onSensorChanged whenever the device receives new value it store the values into variables after some delay. After that compares the new value with previous values beautifully. Which leads to calculate into direction the device has shakes. If the answer is still unclear let me know.

public class ShakeActivity extends Activity implements SensorListener {
// For shake motion detection.
private SensorManager sensorMgr;
private long lastUpdate = -1;
private float x, y, z;
private float last_x, last_y, last_z;
private static final int SHAKE_THRESHOLD = 800;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
// start motion detection
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
boolean accelSupported = sensorMgr.registerListener(this,
    SensorManager.SENSOR_ACCELEROMETER,
    SensorManager.SENSOR_DELAY_GAME);

    if (!accelSupported) {
        // on accelerometer on this device
        sensorMgr.unregisterListener(this,
                SensorManager.SENSOR_ACCELEROMETER);
    }
}

protected void onPause() {
    if (sensorMgr != null) {
        sensorMgr.unregisterListener(this,
                SensorManager.SENSOR_ACCELEROMETER);
        sensorMgr = null;
    }
    super.onPause();
}

public void onAccuracyChanged(int arg0, int arg1) {
// TODO Auto-generated method stub
}

public void onSensorChanged(int sensor, float[] values) {
    if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
        long curTime = System.currentTimeMillis();
        // only allow one update every 100ms.
        if ((curTime - lastUpdate) > 100) {
        long diffTime = (curTime - lastUpdate);
        lastUpdate = curTime;

        x = values[SensorManager.DATA_X];
        y = values[SensorManager.DATA_Y];
        z = values[SensorManager.DATA_Z];

        if(Round(x,4)>10.0000){
            Log.d("sensor", "X Right axis: " + x);
            Toast.makeText(this, "Right shake detected", Toast.LENGTH_SHORT).show();
        }
        else if(Round(x,4)<-10.0000){
            Log.d("sensor", "X Left axis: " + x);
            Toast.makeText(this, "Left shake detected", Toast.LENGTH_SHORT).show();
        }

        float speed = Math.abs(x+y+z - last_x - last_y - last_z) / diffTime * 10000;

        // Log.d("sensor", "diff: " + diffTime + " - speed: " + speed);
        if (speed > SHAKE_THRESHOLD) {
            //Log.d("sensor", "shake detected w/ speed: " + speed);
            //Toast.makeText(this, "shake detected w/ speed: " + speed, Toast.LENGTH_SHORT).show();
        }
        last_x = x;
        last_y = y;
        last_z = z;
        }
    }
}

public static float Round(float Rval, int Rpl) {
float p = (float)Math.pow(10,Rpl);
Rval = Rval * p;
float tmp = Math.round(Rval);
return (float)tmp/p;
}
}

这篇关于使用震动动作从加速度计获取方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:28