17

Is there any way to Zoom-in and Zoom-out an ImageView continuously in Android. I tried using the below code, but only one of the Zoom function is working.

zoomin.xml

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

</set>

zoomout.xml

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

</set>

And the Activity class that I've :

Animation zoomin, zoomout; //declared as public

@Override
public void onCreate(Bundle savedInstanceState) {
   // animation
    zoomin = AnimationUtils.loadAnimation(this, R.anim.zoomin);
    zoomout = AnimationUtils.loadAnimation(this, R.anim.zoomout);
    bgImage.setAnimation(zoomin);
    bgImage.setAnimation(zoomout);
    Thread t = new Thread(new Zoom());
    t.start();
}
private class Zoom implements Runnable {
    @Override
    public void run() {
        while (true) {              
            bgImage.startAnimation(zoomin);
            try {
                Thread.sleep(8000);
            } catch (InterruptedException e) {
                                    e.printStackTrace();
            }               
            bgImage.startAnimation(zoomout);
        }
    }
}

Here the zoomin animation seems to be working fine. Is there any way to implement the zoomin and zoomout animation continuously???

Thanks

Community
  • 1
  • 1
sree127
  • 421
  • 3
  • 9
  • 25

4 Answers4

20

use this instead of thread

 zoomin.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation arg0) {
            bgImage.startAnimation(zoomout); 

        }
    });

and

 zoomout.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation arg0) {
            bgImage.startAnimation(zoomin); 

        }
    });
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
  • 3
    i suggest to add android:repeatCount="1" and android:repeatMode="reverse" to scale paramter in zoomin and zoomout and it works very beautiful :) – Arash Dec 27 '13 at 15:26
  • No need to add animation listener, can do all in one xml, see answer below. – Antonis Radz May 18 '21 at 13:25
2

simply use in your animation xml:

 android:repeatMode="restart"
    android:repeatCount="infinite"
Adeeb karim
  • 292
  • 3
  • 9
2

Easiest way is:

continuous_zoom_out_zoom_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:repeatMode="reverse"
    android:shareInterpolator="true">

    <scale
        android:duration="500"
        android:fillAfter="true"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:startOffset="0"
        android:toXScale="0.8"
        android:toYScale="0.8" />

    <scale
        android:duration="500"
        android:fillAfter="true"
        android:fromXScale="0.8"
        android:fromYScale="0.8"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:startOffset="1000"
        android:toXScale="1"
        android:toYScale="1" />
</set>

And simply use

imageView.loadAnimation(AnimationUtils.loadAnimation(root.context, R.anim.continuous_zoom_out_zoom_in))

No additional callbacks or things like that is needed.

It will create continuous zoom in zoom out animation

Antonis Radz
  • 3,036
  • 1
  • 16
  • 34
-2

You can use something like below and as Sanket mentioned

Zommin.xml

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

</set>

Zoomout.xml

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

</set>

And the code :

zoomin.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation arg0) {
                imageView.startAnimation(zoomout);

            }
        });
Mohammed Riyadh
  • 883
  • 3
  • 11
  • 34