Reputation: 1366
What I am attempting to do is scale an ImageView from it's normal size, to 20% larger and then back. This should be looped. I have done this with the following code but it doesn't quite work right. More below.
anim/scale.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="1000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.2"
android:toYScale="1.2"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false" />
<scale
android:startOffset="1000"
android:duration="1000"
android:fromXScale="1.2"
android:fromYScale="1.2"
android:toXScale="1.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false" />
</set>
Then in my code I do
private void scaleAnimation() {
final Animation scaler = AnimationUtils.loadAnimation(this, R.anim.scale);
scaler.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
animation.reset();
animation.startNow();
}
});
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
scale.startAnimation(scaler);
}
}, 2000);
}
I have the handler there so I could see what was happening at the start.
The problem is kind of difficult to explain.. My image appears to get scaled as soon as the animation starts AND THEN it performs the animation. So the image appears, as it should, for 2 seconds. Just as the animation starts it suddenly gets bigger by what appears to be 25% - 50% and then the animation performs properly. Once the animation is over, the image resizes to it's original size really quickly and then gets scaled up again and then the animation starts over.
I've tried with a couple of different images without difference. It is my understanding that fromXScale=1.0 starts the animation with no scaling, i.e. the image as is. Hopefully someone can shed light on what I've done wrong or where I've misunderstood the concepts.
Cheers
Upvotes: 1
Views: 3337
Reputation: 51
In my case, I need to animate my view one time (1.0f -> 0.6f -> 1.0f)
The solution of cheezy works for me changing repeatCount = 1 instead of infinite.
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<scale
android:duration="600"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="reverse"
android:repeatCount="1"
android:toXScale="0.6"
android:toYScale="0.6" />
</set>
Upvotes: 0
Reputation: 456
If what you're trying to accomplish is an view that scales from 1.0 to 1.2 back to 1.0 constantly, you could make your animation simpler.
You would do the following: Only define one scale animation (from 1.0 to 1.2) Set the repeat count to INFINITE Set the repeat mode to REVERSE
For more info, you can check out the document on the Animation class
Upvotes: 4