1

Is there any way to animate a cropping of an ImageView?

Say for example, the ImageView is 720 x 480. I want to chop off the bottom rows of pixels with an animation until the ImageView is completely gone. I have only been able to move the image up when the onclicklistener is enabled, and make it transparent, which is ok, but not what the designer asked for.

2 Answers2

2
    ValueAnimator anim = ValueAnimator.ofInt(myImageView.getMeasuredHeight(), 0);
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            int val = (Integer) valueAnimator.getAnimatedValue();
            ViewGroup.LayoutParams layoutParams = myImageView.getLayoutParams();
            layoutParams.height = val;
            myImageView.setLayoutParams(layoutParams);
        }
    });
    anim.setDuration(1000);
    anim.start();       

I got code from here and just changed it to height: ObjectAnimator animate LinearLayout width

Community
  • 1
  • 1
0

Assuming you only want to chop off the height, using ObjectAnimator will work out to create such a custom cropped-height animation, First of all you need to specify getHeight() and setHeight() methods and then create a custom animation with ObjectAnimator.

public float getHeight() { ... }
public void setHeight(float h) { ... }

ObjectAnimator heightAnim= ObjectAnimator.ofFloat(yourImageView, "height", heightBegin, heightEnd);
Onur A.
  • 3,007
  • 3
  • 22
  • 37
  • Thanks for pointing me in the right direction! I first had to get Jake Wharton's NineOldAndroids animation library to do as you suggested because I need to support < API 11. Then I couldn't override getHeight() because height is final, so I had had to ValueAnimator. – Lucidmike78 Aug 05 '13 at 16:34
  • You're welcome, yes and also ObjectAnimator added in API 11+, but JakeWhartons' library includes it also for devices < API 11 – Onur A. Aug 05 '13 at 16:47