How can I detect the up-down (Ping-pong) movement of device? [closed]

蹲街弑〆低调 提交于 2019-12-25 03:36:49

问题


There is a dice game in android games that when you move up and down your device like a Ping-pong racket, it throws dices in the screen.

I want to detect this movement of device. How can I do that?

Thanks,


回答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();
            }
        }
    }

}

In Activity:

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


来源:https://stackoverflow.com/questions/8815259/how-can-i-detect-the-up-down-ping-pong-movement-of-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!