Search code examples
androidandroid-animationandroid-imageview

Android scale image view with animation


i have an ImageView and would like to scale it smaller with animation. i use

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <scale
        android:fromXScale="1.0"
        android:toXScale="0.7"
        android:fromYScale="1.0"
        android:toYScale="0.7"
        android:pivotX="50%"
        android:pivotY="10%"
        android:duration="1500" />
</set>

This works good. But after the animation is finished the image gets back to its original size. Any ideas why?


Solution

  • Ok, i found a solution. is this correct like this? or it is quick and dirty? ^^

        ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(image, "scaleX", 0.7f);
        ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(image, "scaleY", 0.7f);
        scaleDownX.setDuration(1500);
        scaleDownY.setDuration(1500);
    
        ObjectAnimator moveUpY = ObjectAnimator.ofFloat(image, "translationY", -100);
        moveUpY.setDuration(1500);
    
        AnimatorSet scaleDown = new AnimatorSet();
        AnimatorSet moveUp = new AnimatorSet();
    
        scaleDown.play(scaleDownX).with(scaleDownY);
        moveUp.play(moveUpY);
    
        scaleDown.start();
        moveUp.start();