How to animate an icon for expanding / minimizing a view?

I have this icon:

enter image description here

And when I click on my item in the list, I want it to rotate 180 °. When I press again, I want him to turn another 180 ° so that he returns to its original position.

First I tried:

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

But it only works 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 at the same time, the animation always starts with an arrow pointing down and rotating up, even if the arrow is already pointing up.

So how can I make this work?

+7
source share
3 answers

Custom the following code to click an image event:

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;
+20
source

, , 180 ° , ,

rotationAngle = rotationAngle == 0 ? 180 : 0;  //toggle
mExpandArrow.animate().rotation(rotationAngle).setDuration(500).start();

rotationAngle ,

int rotationAngle = 0;
+11

You can try this on your adapter list

 btnOpen.setOnClickListener(v -> {

        if (btnOpen.getTag()!=null && btnOpen.getTag().toString().equals("180")){
            ObjectAnimator anim = ObjectAnimator.ofFloat(v, "rotation",180, 0);
            anim.setDuration(time);
            anim.start();
            btnOpen.setTag("");


        }  else {
            ObjectAnimator anim = ObjectAnimator.ofFloat(v, "rotation",0,  180);
            anim.setDuration(time);
            anim.start();
            btnOpen.setTag(180+"");

        }

    });

This work with a tag in your list

0
source

All Articles