Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 436
Mlove Avatar asked Sep 17 '11 17:09

Mlove


1 Answers

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

like image 167
Lalit Poptani Avatar answered Oct 11 '22 12:10

Lalit Poptani