4

I have been trying to zoom in a view and then return back to the original size with a zoom out like animation.

What i have been able to do was put zoom in and out in a set and animate it on an imageview on button click, but it drops the image size suddenly for the first time, and then animates fine on the later clicks. I I would appreciate any help to accomplish a smooth animation

my code

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    >
    <scale
        android:duration="1000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale=".5"
        android:toYScale=".5" >
    </scale>
    <scale
        android:duration="1000"
        android:fromXScale=".5"
        android:fromYScale=".5"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1"
        android:toYScale="1" >
    </scale>

</set>
final Animation ani_in = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.zoomin_out);
imageView.startAnimation(ani_in);
Giorgos Neokleous
  • 1,709
  • 1
  • 14
  • 25
Hissaan Ali
  • 2,229
  • 4
  • 25
  • 51

2 Answers2

14

Animation is outdated(Animation vs Animator). Use ValueAnimator:

        final ValueAnimator anim = ValueAnimator.ofFloat(1f, 1.5f);
        anim.setDuration(1000);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                image.setScaleX((Float) animation.getAnimatedValue());
                image.setScaleY((Float) animation.getAnimatedValue());
            }
        });
        anim.setRepeatCount(1);
        anim.setRepeatMode(ValueAnimator.REVERSE);
        anim.start();

enter image description here

3

Don't need to use two scale tags it can be done in only single scale tag

by using these two:

android:repeatCount="infinite"
 android:repeatMode="reverse"

In your anim file

<scale
        android:duration="30000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="90%"
        android:pivotY="90%"
        android:toXScale="1.5"
        android:toYScale="1.5"
        android:startOffset="1000"
        android:repeatCount="infinite"
        android:repeatMode="reverse">
    </scale>

In code file

imageView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.zoomin_out));