Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect Upside down Orientation in android?

In my android app i have panorama image and i am rotating this image according to phones motion using TYPE_ORIENTATION sensor it is working fine for both landscape and potrait. Here is the code for 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 device's orientation changes upside down image will rotate to wrong direction(exactly opposite direction). How can i fix this issue and what actually happens when devices orientation change upside down ?
Thanks.

like image 859
Dinesh Anuruddha Avatar asked Sep 28 '12 10:09

Dinesh Anuruddha


1 Answers

Use the device current 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

like image 135
yoah Avatar answered Oct 27 '22 09:10

yoah