Question

I'm trying to write a live wallpaper where objects are falling down from top to bottom. The problem is that on a tablet (I'm using a nexus 7 and a galaxy note) when screen orientation changes, my objects are no more falling down but they are going to the wrong direction (if a turn the device right they are falling right, if I turn it left they falls to the left, and so on..).

I'm using gravity sensor

mGravitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);

and I calculate the square root of gravity along X and Y axis to move the objects.

How is it possible to make the objects always falling down? Thanks in advance!

I add an image to better explane this behavior.

description

Was it helpful?

Solution

I found a simple solution that in my case is working perfectly:

I've written this method to correctly map the values:

    private void mapSensors(float[] input, float[] output) {
        switch (((WindowManager) getApplicationContext().getSystemService(
                Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation()) {
        case 0:
            output[0] = input[0];
            output[1] = input[1];
            break;
        case 1:
            output[0] = -input[1];
            output[1] = input[0];
            break;
        case 2:
            output[0] = -input[0];
            output[1] = -input[1];
            break;
        case 3:
            output[0] = input[1];
            output[1] = -input[0];
            break;
        }
    }

And the method is called like this:

    @Override
    public void onSensorChanged(SensorEvent event) {
        mapSensors(event.values, gravity);
    }

It maps the values from the sensor (event.values) in to my float array (gravity).

Hope this might help someone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top