Dazza
Dazza

Reputation: 145

Rotating a view on Android issue

I have been stuck on quite an irritating problem for a number of days now.

To describe the problem; I essentially have a issue when I perform a rotation on a view which is part of a larger view, then I display a fragment on top of this view, and then hide this fragment returning to the original view.

The view maintains its rotation but is placed above and to the right of where it should be.

In the XML layout I have set the view to be centered in the parent, and sure enough on startup everything is where its supposed to be.

While the view is rotating and the main view is visible (it is essentially a compass) everything works as intended

If I do not rotate the view, then show the fragment followed by hiding it it stays where it was meant to be without issue, so clearly the act of rotating it and then displaying something over it is causing a problem.

If I continue to rotate the view after it has moved out of position, it snaps back into its centered position and all is well - how can I ensure the view to always remain where its suppose to be?!?

For reference I am calling the rotation method like so:

RotateAnimation rotate = new RotateAnimation(PREVOIUS_ROTATION_AMOUNT, NEW_ROTATION, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
rotate.setDuration(980);
rotate.setRepeatCount(0); 
rotate.setFillAfter(true);
compassView.startAnimation(rotate);

Upvotes: 0

Views: 143

Answers (1)

Gil Moshayof
Gil Moshayof

Reputation: 16771

This code should do what you're expecting:

compassView.setPivotX(compassView.getMeasuredWidth() / 2);
compassView.setPivotY(compassView.getMeasuredHeight() / 2);
compassView.animate().rotation(NEW_ROTATION).setDuration(980);

Upvotes: 1

Related Questions