Skip to content Skip to sidebar Skip to footer

How To Rotate A View From Different Angles?

I am trying to rotate an ImageButton by 180 degrees so it matches the reverse portrait orientation. When I did this the same way as the other orientation changes, the result was pe

Solution 1:

Answer :

OrientationEventListenerOrientationEventListener=newOrientationEventListener(this, SensorManager.SENSOR_DELAY_UI) {
        staticfinalintDELTA=20;

        @OverridepublicvoidonOrientationChanged(int Angle) {
            floatAngleDestination= ButtonDestination;
            if (angle > 90 - DELTA && angle < 90 + DELTA) {
                AngleDestination = -90;
            } elseif (Angle > 180 - DELTA && Angle < 180 + DELTA) {
                AngleDestination = 180;
            } elseif (Angle > 270 - DELTA && Angle < 270 + DELTA) {
                AngleDestination = 90;
            } elseif (Angle > 360 - DELTA || Angle < DELTA) {
                AngleDestination = 0;
            } 
            if (AngleDestination != ButtonDestination) {
                ButtonDestination = AngleDestination;
                MyButton.animate().rotation(ButtonDestination).setDuration(100).start(); 
            } 
        } 
    }; 
    orientationEventListener.enable();
}

Solution 2:

If you found that this code was running (either by break-point debugging or a Logcat statement) then try running the animation on the UI thread.

YourActivity.runOnUiThread(new Runnable() {
    publicvoidrun() 
        MyButton.animate().rotationBy(90 - MyButtonCurrentAngle).setDuration(100).start();
    }
});

You might also need to call start on that.

Post a Comment for "How To Rotate A View From Different Angles?"