0

I've got an ImageButton that rotates when the phone rotates. It rotates following the device orientation. Something like:

int rotation = this.getWindowManager().getDefaultDisplay().getRotation();
        int degrees = 0;
        switch (rotation) {
            case Surface.ROTATION_0: degrees = 0; break;
            case Surface.ROTATION_90: degrees = 90; break;
            case Surface.ROTATION_180: degrees = 180; break;
            case Surface.ROTATION_270: degrees = 270; break;
        }
        int relative_orientation = (current_orientation + degrees) % 360;
        int ui_rotation = (360 - relative_orientation) % 360;
        preview.setUIRotation(ui_rotation); 

and then in the ImageButton i'm doing this:

view = findViewById(R.id.button1);
            layoutParams = (RelativeLayout.LayoutParams)view.getLayoutParams();
            view.setLayoutParams(layoutParams);
            view.setRotation(ui_rotation);

It works but there is no animation.. So the rotation is very drastic and not smooth.. Is there a way to put an animation?

EDIT: I add the setUIRotation() method

void setUIRotation(int ui_rotation) {
        if( MyDebug.LOG )
            Log.d(TAG, "setUIRotation");
        this.ui_rotation = ui_rotation;
    }
Atlas91
  • 5,754
  • 17
  • 69
  • 141

1 Answers1

0

You may use something like this instead of view.setRotation():

RotateAnimation rotate = new RotateAnimation(0f, ui_rotation, 
    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  

rotate.setDuration(500);
rotate.setFillAfter(true); 

view.startAnimation(rotate);

For edge cases e.g. animation not yet ended when another rotation is triggered you may achieve better effects using setAnimationListener.

You may refer to the discussions at Rotating a view in Android for details.

Community
  • 1
  • 1
headuck
  • 2,763
  • 16
  • 19
  • Yes but how to implement in my code..see my edit, i've written the setUIRotation() method – Atlas91 Sep 13 '15 at 10:36
  • In the place you currently trigger your rotation e.g. `view.setRotation(ui_rotation)` above – headuck Sep 13 '15 at 11:06
  • As per your code above, you already have the lines `view = findViewById(R.id.button1); ... view.setRotation(ui_rotation);`. But `setRotation()` would rotate the view immediately. Now instead of calling `setRotation()`, insert the lines above in place of `setRotation()` so that the the rotation would proceed gradually in (say) 500ms. – headuck Sep 13 '15 at 15:36
  • I tried but and the animation itself works but it rotates too much degrees.. I mean..basically to works correctly should be: `RotateAnimation rotate = new RotateAnimation(0f, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);` but if i write 0 instead ui_rotation of course the animation doesn't work – Atlas91 Sep 13 '15 at 15:51
  • Did you remove the original setRotation()? Also if `ui_rotation` is an absolute orientation value you may need to convert your it to a relative value compared with the last value, say by saving the latter in a static variable and doing subtraction each time. You may also limit the rotation to range from -180 to 180 (i.e. mod it by 360 and subtract 180) – headuck Sep 13 '15 at 16:13