如何检测设备的上下(乒乓)运动?

时间:2012-01-11 06:49:32

标签: android device sensor

Android游戏中有一个骰子游戏,当你像乒乓球拍一样上下移动你的设备时,它会在屏幕上抛出骰子。

我想检测设备的这种移动。我怎么能这样做?

谢谢,

1 个答案:

答案 0 :(得分:1)

package org.mabna.order.utils;

public interface OnDeviceShakeListener {
    public abstract void onDeviceShaked();
}

import java.util.ArrayList;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;

public class SensorListener implements SensorEventListener {

    float[] xVals = new float[] { 0, 0, 0 };
    float[] yVals = new float[] { 0, 0, 0 };
    long prevTime = 0;

    final float angle = 10;
    final long time = 100 * 1000 * 1000; // in nanosecond

    public SensorListener()
    {

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        synchronized (this) {

            if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
                if (event.timestamp - prevTime > time)
                {
                    prevTime = event.timestamp;

                    xVals[0] = xVals[1];
                    xVals[1] = xVals[2];
                    xVals[2] = event.values[1];

                    yVals[0] = yVals[1];
                    yVals[1] = yVals[2];
                    yVals[2] = event.values[2];

                    if (degreeChanged(xVals) ||
                            degreeChanged(yVals))
                    {
                        OnDeviceShaked();
                    }
                }

            }

        }
    }

    private boolean degreeChanged(float[] V) {

        float v0 = Math.abs(V[0]);
        float v1 = Math.abs(V[1]);
        float v2 = Math.abs(V[2]);

        boolean hasAngle = ((v1 - v0) > angle && (v1 - v2) > angle);

        return hasAngle;
    }

    // DeviceShakeListener ----------------------------------------------------

    ArrayList<OnDeviceShakeListener> arrOnDeviceShakeListener =
            new ArrayList<OnDeviceShakeListener>();

    /**
     * Listens if device is shaken like a Ping-pong racket.
     * 
     * @param listener
     */
    public void setOnDeviceShakeListener(
            OnDeviceShakeListener listener) {
        arrOnDeviceShakeListener.add(listener);
    }

    public void clearOnDeviceShakeListener(
            OnDeviceShakeListener listener) {
        arrOnDeviceShakeListener.remove(listener);
    }

    // This function is called after the new point received
    private void OnDeviceShaked() {
        // Check if the Listener was set, otherwise we'll get an Exception when
        // we try to call it
        if (arrOnDeviceShakeListener != null) {
            // Only trigger the event, when we have any listener

            for (int i = arrOnDeviceShakeListener.size() - 1; i >= 0; i--) {
                arrOnDeviceShakeListener.get(i).onDeviceShaked();
            }
        }
    }

}

活动:

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
            sensorHandler = new SensorListener();
            sensorHandler.setOnDeviceShakeListener(new OnDeviceShakeListener() {
                @Override
                public void onDeviceShaked() {
                    //....
                }
            });