user3642735 user3642735 - 2 months ago 23
Android Question

How do I reverse a running animation?

When the

Button
is touched, it starts a zoom animation.

Animation zoomIn = AnimationUtils.loadAnimation(context, R.anim.zoomin);
view.startAnimation(zoomIn);


The zoomin.xml code:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillEnabled="true"
android:fillAfter="true">
<scale
android:fromXScale="1.0"
android:toXScale="1.3"
android:fromYScale="1.0"
android:toYScale="1.3"
android:pivotX="50%"
android:pivotY="50%"
android:duration="200"
android:interpolator="@android:anim/accelerate_interpolator"/>
</set>


But if the
Button
is released during this still running animation, it should reverse the animation from this point in time.

How can i achieve that?

Answer

You could use ViewPropertyAnimator to achieve something like this.

For example:

Button button = (Button) findViewById(R.id.yourButton);

button.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        // adjust the scale and duration values according to your needs

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            v.animate().scaleX(2).scaleY(2).setDuration(2000);
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            v.animate().scaleX(1).scaleY(1).setDuration(2000);
        }
        return false;
    }
});
Comments