Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ImageView Zoom-in and Zoom-out Continuously

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

like image 354
sree127 Avatar asked Aug 14 '13 12:08

sree127


People also ask

How to zoom in and zoom Out in Android?

Pinch 2 or more fingers together or apart to adjust zoom. To zoom temporarily, quickly tap the screen 3 times and hold down your finger on the third tap. Drag your finger to move around the screen. Lift your finger to zoom out.

How to zoom in and Out image in Android studio?

This example demonstrates how do I Zoom In and Zoom Out an android ImageView. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.

What is zoom control in Android?

In Android, Zoom Control is a class that has some set of methods that are used to control the zoom functionality. It has two buttons that are used to control the zoom functionality (ie Zoom In and Zoom Out). Zoom Control Class has been deprecated in API Version 29.


1 Answers

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); 

        }
    });
like image 146
Sanket Kachhela Avatar answered Sep 28 '22 18:09

Sanket Kachhela