move by SensorManager - Android Hardware

Android examples for Hardware:Sensor

Description

move by SensorManager

Demo Code


//package com.java2s;
import android.hardware.SensorManager;
import android.util.Log;

public class Main {
    static double dif = 1.5;

    public static boolean move(float[] values,
            float[] preAccelerometerValues) {
        float difx = Math.abs(values[SensorManager.DATA_X]);
        float dify = Math.abs(values[SensorManager.DATA_Y]);
        float difz = Math.abs(values[SensorManager.DATA_Z]);
        Log.e("log.e val = ", " " + difx + " " + dify + " " + difz);

        float difx1 = Math
                .abs(preAccelerometerValues[SensorManager.DATA_X]);
        float dify1 = Math
                .abs(preAccelerometerValues[SensorManager.DATA_Y]);
        float difz1 = Math
                .abs(preAccelerometerValues[SensorManager.DATA_Z]);
        Log.e("log.e pre = ", " " + difx1 + " " + dify1 + " " + difz1);

        float difx2 = Math.abs(values[SensorManager.DATA_X]
                - preAccelerometerValues[SensorManager.DATA_X]);
        float dify2 = Math.abs(values[SensorManager.DATA_Y]
                - preAccelerometerValues[SensorManager.DATA_Y]);
        float difz2 = Math.abs(values[SensorManager.DATA_Z]
                - preAccelerometerValues[SensorManager.DATA_Z]);
        Log.e("log.e  dif = ", " " + difx2 + " " + dify2 + " " + difz2);

        if (checkMove(values[SensorManager.DATA_X],
                preAccelerometerValues[SensorManager.DATA_X])
                || checkMove(values[SensorManager.DATA_Y],
                        preAccelerometerValues[SensorManager.DATA_Y])
                || checkMove(values[SensorManager.DATA_Z],
                        preAccelerometerValues[SensorManager.DATA_Z])) {
            return true;
        }//w  w  w .  jav a  2 s  . c o  m
        return false;
    }

    private static boolean checkMove(float f, float g) {
        double thisdif = f - g;
        if (Math.abs(thisdif) > dif) {
            return true;
        }
        return false;

    }
}

Related Tutorials