Android:确定手部移动的方向,即曲线。

时间:2016-03-18 13:18:40

标签: java android android-sensors

我正在开发一个Android项目,我希望在其中检测用户的手部动作。我查了很多例子,但是所有的例子都只检测出线性的变化,比如手向上或向下,向右或向左移动......我没有找到弯曲检测。我现在有基本代码,我将传感器数据发送到我们的系统,但我想直接发送一些对象或其他解释中的曲线运动。对此不熟悉,不知道如何继续。你能帮忙的话,我会很高兴。非常感谢。 : - )

代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    sensorMan = (SensorManager)getSystemService(SENSOR_SERVICE);
    accelerometer = sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mAccel = 0.00f;
    mAccelCurrent = SensorManager.GRAVITY_EARTH;
    mAccelLast = SensorManager.GRAVITY_EARTH;

}

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
        mGravity = event.values.clone();
        // Shake detection
        float x = mGravity[0];
        float y = mGravity[1];
        float z = mGravity[2];
        mAccelLast = mAccelCurrent;
        mAccelCurrent = (float)Math.sqrt(x * x + y * y + z * z);
        float delta = mAccelCurrent - mAccelLast;
        mAccel = mAccel * 0.9f + delta;
        // Make this higher or lower according to how much
        // motion you want to detect
        if(mAccel > 2){
            SensorModel sensorModel = new SensorModel(x,y,z);
            new sendDataToServer().execute(sensorModel);
        }
    }
}

public class sendDataToServer extends AsyncTask<SensorModel,Void,Void>{

    @Override
    protected Void doInBackground(SensorModel... params) {
        try {
            SensorModel sensorModel = params[0];
            RestTemplate restTemplate = StaticRestTemplate.getRest();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
            restTemplate.postForObject(transmitUrl,sensorModel,Boolean.class);
        }catch (Exception ignored){}

        return null;
    }
}

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

@Override
public void onResume() {
    super.onResume();
    sensorMan.registerListener(this, accelerometer,
            SensorManager.SENSOR_DELAY_UI);
}

@Override
protected void onPause() {
    super.onPause();
    sensorMan.unregisterListener(this);
}

谢谢。 : - )

0 个答案:

没有答案