How to determine upward orientation in Android?

In my Android application, I have a panoramic image, and I rotate this image according to the movement of the phones using the TYPE_ORIENTATION sensor, it works great both for landscape and potrait. Here is the code for the rotation logic.

@Override public void onSensorChanged(SensorEvent event) { switch (event.sensor.getType()) { case Sensor.TYPE_ORIENTATION: float[] insideval = getValPoints(); event.values[1]=(float) (event.values[1]*2.8); event.values[2]=(float) (event.values[2]*2.8); setValPoints(event.values.clone()); if (insideval != null) { float check= insideval[0] - event.values[0]; if (-1<=check && check <=1) { //Animation stoped this.stopAnimation(); }else{ if (getdefaltDeviceOrientation() == ORENTATION_LANDSCAPE) { // Natural Orientation is landscape if (getcurrentDeviceOrientation() == 1) { //ORIENTATION_PORTRAIT startPoint = CGPoint.CGPointMake(insideval[2], insideval[1]); if (insideval[0] < event.values[0]) { endPoint = CGPoint.CGPointMake(insideval[2]+ event.values[2], insideval[1]); } else if (insideval[0] > event.values[0]) { endPoint = CGPoint.CGPointMake(insideval[2]- event.values[2], insideval[1]); } } else if (getcurrentDeviceOrientation() == 2) { //Log.i(TAG, "ORIENTATION_LANDSCAPE"); startPoint = CGPoint.CGPointMake(insideval[1], insideval[2]); if (insideval[0] < event.values[0]) { endPoint = CGPoint.CGPointMake(insideval[1]- event.values[1], insideval[2]); } else if (insideval[0] > event.values[0]) { endPoint = CGPoint.CGPointMake( insideval[1]+event.values[1] ,insideval[2]); } } } else if (getdefaltDeviceOrientation() == ORENTATION_PROTRAIT) { // Natural Orientation is portrait if (getcurrentDeviceOrientation() == 1) { //Log.i(TAG, "ORIENTATION_PORTRAIT"); startPoint = CGPoint.CGPointMake(insideval[1], insideval[2]); if (insideval[0] < event.values[0]) { endPoint = CGPoint.CGPointMake(insideval[1]+ event.values[1], insideval[2]); } else if (insideval[0] > event.values[0]) { endPoint = CGPoint.CGPointMake(insideval[1]- event.values[1], insideval[2]); } } else if (getcurrentDeviceOrientation() == 2) { //Log.i(TAG, "ORIENTATION_LANDSCAPE"); startPoint = CGPoint.CGPointMake(insideval[2], insideval[1]); if (insideval[0] < event.values[0]) { endPoint = CGPoint.CGPointMake(insideval[2]- event.values[2], insideval[1]); } else if (insideval[0] > event.values[0]) { endPoint = CGPoint.CGPointMake(insideval[2]+event.values[2],insideval[1]); } } } this.drawView(); } } break; } } 

So my problem is if the orientation of the device changes upside down. the image will rotate in the wrong direction (in the opposite direction). How can I fix this problem and what really happens when the device orientation changes upside down?
Thanks.

+6
source share
1 answer

Use current device rotation http://developer.android.com/reference/android/view/Display.html#getRotation ()

It returns one of 4 options: ROTATION_0, ROTATION_90, ROTATION_180, ROTATION_270

+6
source

Source: https://habr.com/ru/post/926492/


All Articles