Android scale image view with animation

13,660

Solution 1

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();

Solution 2

Try with this code, it will not reset the image size after animation, here view is the imageview you want to animate.

ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(view, "scaleX", 0.5f);
ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(view, "scaleY", 0.5f);
scaleDownX.setDuration(1000);
scaleDownY.setDuration(1000);

AnimatorSet scaleDown = new AnimatorSet();
scaleDown.play(scaleDownX).with(scaleDownY);

scaleDown.start();

Solution 3

A simplier answer is below;

imageview.animate().translationY(-200F).duration = 500
imageview.animate().scaleY(0.4F).duration = 500
imageview.animate().scaleX(0.4F).duration = 500
Share:
13,660
Ghost108
Author by

Ghost108

Updated on June 16, 2022

Comments

  • Ghost108
    Ghost108 about 2 years

    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?

  • Ghost108
    Ghost108 over 8 years
    NEW QUESTION: How looks the code, if i would to scale down AND move position up?
  • Nigam Patro
    Nigam Patro over 8 years
    means, can you explain?
  • Ghost108
    Ghost108 over 8 years
    something like this: Animation slideUp = new TranslateAnimation(0, 0, 0, -100); slideUp.setDuration(1500);
  • Nigam Patro
    Nigam Patro over 8 years
    ObjectAnimator slideUp = ObjectAnimator.ofFloat(view, "translationX", 0, -100);