Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

animation for zoom in and zoom out in android for imageview

How do i set zoom in and zoom out when click on imageview?I want my program to react when user click on imageview must get large to some extent and can move imageview on that screen and sometime it reduce the size along when it move on touch anywhere on the screen .when click again is go resume original size what do i do?

like image 459
Shweta Kanjia Avatar asked Mar 17 '13 06:03

Shweta Kanjia


People also ask

How do you zoom in and zoom out on 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.

What is pinch zoom in Android?

Full screen magnification: Zoom in & make everything biggerPinch with 2 fingers to adjust zoom.


1 Answers

As far as I know there are two ways.

The first way:

Make a new folder in res called 'anim'. Than make a xml file inside, for example zoomin.xml. Afterwards put the following code inside.

<scale  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromXScale="1" 
  android:toXScale="5" 
  android:fromYScale="1" 
  android:toYScale="5" 
  android:pivotX="50%" 
  android:pivotY="50%" 
  android:duration="1000" 
  android:fillAfter="true">
</scale>

Make another one for zoom out, but with reversed values.

<scale  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromXScale="5" 
  android:toXScale="1" 
  android:fromYScale="5" 
  android:toYScale="1" 
  android:pivotX="50%" 
  android:pivotY="50%" 
  android:duration="1000" 
  android:fillAfter="true">
</scale>

You can change the values according to your needs. I think that they are self-explanatory.

And now in your java code.

ImageView imageView = (imageView)findViewById(R.id.yourImageViewId);

Animation zoomin = AnimationUtils.loadAnimation(this, R.anim.zoomin);
Animation zoomout = AnimationUtils.loadAnimation(this, R.anim.zoomout);
imageView.setAnimation(zoomin);
imageView.setAnimation(zoomout);

Now you only need to keep track which is the current state. And for each state execute this lines of codes:

imageView.startAnimation(zoomin);

and

imageView.startAnimation(zoomout);

For example:

imageView.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                if(!pressed) {
                    v.startAnimation(zoomin);
                    pressed = !pressed;
                } else {
                    v.startAnimation(zoomout);
                    pressed = !pressed;
                }
            }
        });

The other way is described here : http://developer.android.com/training/animation/zoom.html.

like image 118
Ikspeto Avatar answered Oct 03 '22 18:10

Ikspeto