juztcode
juztcode

Reputation: 1345

animating the layout's setRotation property

views can be rotated using the RotateAnimation object in android, but using this class seems to only visually rotate the views, e.g. clicking the rotate view(rotated with pivot other than center) would not actually click the view, the view is still in it's original position but is only visually somewhere else.

Hence, in order to rotate the view along with it's children to a new position, I found the solution to use layout.setRotation(angle), in my case layout is a constraintLayout which I'll have to rotate through 180.

But, the problem is, that I am not being able to animate this, I'd like to make a smooth transition from 0 to 180 degrees, but it just snaps to 180 degrees. How can I animate this property or possibly the rotation?

Upvotes: 0

Views: 318

Answers (1)

l_b
l_b

Reputation: 583

In your .xml file inside the Constraint Layout add the following code.

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"/>

MainActivity.java :

 ConstraintLayout constraintLayout = findViewById(R.id.layout);
 constraintLayout.animate().rotation(100).setDuration(2000);

The only problem was that you forgot to add .animate() before .rotation() and that's why it didn't work.

Upvotes: 2

Related Questions