检测绕Y轴Android Java的旋转

时间:2014-05-10 15:54:45

标签: android vector rotation angle flip

如何检测手机Y轴周围的旋转?

android phone axis

我是android的新手。我想检测180度旋转。我想检测一下,例如用户是否在桌面上翻动手机,或者用户是否将手机放在口袋里。

我已经阅读了很多文章,但我真的不明白如何获得手机位置,然后计算另一个位置之间的角度。

我找到了这篇文章,但我不知道如何处理数组命名方向:

Get device angle by using getOrientation() function

谢谢!

//这是我的解决方案。不完全合乎逻辑,但效果很好:

public class FlipListener implements SensorEventListener {

    SensorManager sensorMgr;
    FlipEventReceiver receiver;

    public FlipListener(Context context, FlipEventReceiver receiver) {
        this.receiver = receiver;
        sensorMgr = (SensorManager) context.getSystemService(Activity.SENSOR_SERVICE);
        sensorMgr.registerListener(this, sensorMgr.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_UI);
    }   

    public void onResume() {
        sensorMgr.registerListener(this, sensorMgr.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_UI);
    }

    public void onPause() {
        sensorMgr.unregisterListener(this);
        clearStack();
    }

    private static final int IGNORE_FLIPS_AFTER_FLIP = 2500;
    private static final int SAMPLING_INTERVAL = 60;
    private static final int MINIMAL_STACK_SIZE_TO_FLIP = 2; // Shouldn't be lower than 2
    private static final float FLIP_RADIANS = (float)Math.toRadians(140);
    private static final int STACK_MAX_SIZE = 38;

    private List<Float> stack = new ArrayList<Float>();
    private long lastAdd = 0;
    private long lastFlip = 0;

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
            rotationRateAroundYChanged((float)event.values[1]);
        }
    }

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

    private void rotationRateAroundYChanged(float rotationRateAroundY) {
        long currentTime = System.currentTimeMillis();

        if (lastFlip != 0 && (currentTime - lastFlip) < IGNORE_FLIPS_AFTER_FLIP) {
            return;
        }

        if( (currentTime - lastAdd) >= SAMPLING_INTERVAL ) {
            if( Math.abs(rotationRateAroundY) > 0.3 ) { // Smaller values are unimportant. They can make only mess.
                addToStack(rotationRateAroundY);
                checkForFlip();
            }
        }
    }

    private void checkForFlip() {

        int stackSize = stack.size();
        if( stackSize < MINIMAL_STACK_SIZE_TO_FLIP ) return;
        float approximateAngleSummary = 0;
        float val;

        for(int i = 0; i < stackSize; i++) {
            val = Math.abs(stack.get(i).floatValue());
            // "+ Math.pow(val/4.58, 2) )" don't have a sense. Simply it works better with it.
            approximateAngleSummary += ( (val + Math.pow(val/4.58, 2) ) / 1000 ) * SAMPLING_INTERVAL;

            if( approximateAngleSummary >= FLIP_RADIANS ) {
                triggerFlipDetected();
                clearStack();
                return;
            }
        }
    }

    private void clearStack() {
        stack.clear();
    }

    private void addToStack(float val) {
        lastAdd = System.currentTimeMillis();
        int stackSize = stack.size();
        if( stackSize > 0 && ((stack.get(stackSize-1) > 0 ? 1 : -1) != (val>0?1:-1) || stackSize > STACK_MAX_SIZE) ) {
            clearStack();
        }
        stack.add(val);
    }

    private void triggerFlipDetected() {
        lastFlip = System.currentTimeMillis();

        receiver.onFlipDetected();
    }

    public interface FlipEventReceiver {
        public void onFlipDetected();
    }
}

用法:

public class FlipTestActivity extends Activity implements FlipEventReceiver {

FlipListener flipListener;
    boolean flipListenerActive = true;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flip_test);

    flipListener = new FlipListener(this, this);
}

public void onFlipDetected() {
    // What to do when flip detected
}

    @Override
protected void onResume() {
    super.onResume();
    if( !flipListenerActive ) { 
        flipListener.onResume();
        flipListenerActive = true;
    }
}

    @Override
protected void onPause() {
    super.onPause();
    if( flipListenerActive ) {
        flipListener.onPause();
        flipListenerActive = false;
    }
}

}

1 个答案:

答案 0 :(得分:0)

SensorManager是一个允许您访问设备传感器的类。查看here

Here是关于传感器的好教程