简体   繁体   中英

Invert sensor values[1] in portrait to positive and flipped portrait negative on Android

I want to invert the degree values on portrait (-90° to 90°) or flipped portrait (90° to -90°). So I would solve it like this:

class OrientationListener implements SensorEventListener
{

@Override
public void onSensorChanged(SensorEvent event)
{
    if (button == true){
        angle = Math.round(event.values[1]);

        //(WindowManager)getApplication().getSystemService(WINDOW_SERVICE).getDefaultDisplay();
        Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

        int rotation = display.getRotation();
        switch(rotation)
        {
            case Surface.ROTATION_0:
                angle *= -1;
                break;

            case Surface.ROTATION_90:
                break;

            case Surface.ROTATION_180:
                angle *= 1;
                break;

            case Surface.ROTATION_270:
                break;
        }

     }
}

But it doesn't work. I get always the screen rotation 0.

You have to implement OrientationEventListener instead of SensorEventListener

@Override
public abstract void onOrientationChanged (int orientation)
{
    // Orientation is the value of the angle in from 0 to 359
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM