Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate an icon for expand/collapse view?

I have this icon:

enter image description here

And when i press my item in a listview i want it to rotate 180°. When i click again i want it to rotate another 180° so it get's to it's original position.

First i tried:

view.animate().rotation(180).setDuration(500).start();

But it only fires once. After that i tried:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true">

    <rotate
        android:duration="500"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="180" />
</set>

But with that the animation always starts with the arrow showing to the buttom and rotating to the top even if the arrow already shows to the top.

So how can i make that working?

like image 401
Mulgard Avatar asked Jul 27 '15 12:07

Mulgard


1 Answers

User following code to click event of image:

ObjectAnimator anim = ObjectAnimator.ofFloat(v, "rotation",rotationAngle, rotationAngle + 180);
            anim.setDuration(500);
            anim.start();
            rotationAngle += 180;
            rotationAngle = rotationAngle%360;

and make rotationAngle a global variable:

int rotationAngle = 0;
like image 94
chandil03 Avatar answered Sep 22 '22 00:09

chandil03