Mlove
Mlove

Reputation: 203

Animate zoom in via center of an image in Android with xml

I am trying to do some simple image zooming and panning with Android and I have two simple questions.

First, when I call the animation using scale, it zooms using the upper left-hand corner of the image as the origin but I would like it to zoom from the center of the image.
The second question is once the animation is done, it resets the image to the original state and I would like it to stay at the final state.

Here is the xml I have for the scale:

<scale android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:toXScale="2.0"
    android:toYScale="2.0"
    android:detachWallpaper="true"
    android:duration="3000"></scale>

and in my code:

a = AnimationUtils.loadAnimation(this, R.anim.set);
a.reset();
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.clearAnimation();
iv.startAnimation(a);

Upvotes: 10

Views: 14343

Answers (1)

Lalit Poptani
Lalit Poptani

Reputation: 67286

For scaling a image from center you have to set the pivotX and pivotY

try this code for scaling from center and retaining the state after scaling,

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

Thanks...

Upvotes: 30

Related Questions