Itamar Kerbel
Itamar Kerbel

Reputation: 2568

Scaling animation doesn't change the ImageView size

I'm creating an app the loads drawables from the resource and displays it in a placeholder ImageView. When the user clicks on a button to load the next image I created an animation that moves and scales the old image to a new location and loads a new drawable to the placeholder.

After 30 images I'm running OOM which makes sense.

What I'm trying to do is resample each image once the animation ends as I'm starting with a 500X500 place holder and ending with 1/3 of it.

The problem is that after I finish the animation the width and height of the ImageView stay the same. Shouldn't the scaling animation change the width and height of the ImageView?

Here is the code for the animation:

    //create the animation
    imageView.setPivotX(0);
    imageView.setPivotY(0);
    imageView.animate()
             .scaleX(scaleX) //about 0.3
             .scaleY(scaleY) //about 0.3
             .x(finalX) //location x of the image
             .y(finalY) //location y of the image
             .setListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animation) {
                            int reqWidth = imageView.getWidth(); //result 500 as expected
                            int reqHeight = imageView.getHeight();//result 500 as expected
                            Drawable image = imageView.getDrawable();
                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            int reqWidth = imageView.getWidth();//result 500 - why?
                            int reqHeight = imageView.getHeight();//result 500 - why?
                            Drawable image = imageView.getDrawable();
                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {}

                        @Override
                        public void onAnimationRepeat(Animator animation) {}
                    });

Upvotes: 1

Views: 1206

Answers (1)

Chrisvin Jem
Chrisvin Jem

Reputation: 4070

The scaleX() causes the View's scaleX property to be animated to the specified value (reference). It doesn't directly change the layout width/height values. (reference)

You can get the expected width/height values by multiplying with the scaleX/Y values.

//create the animation
    imageView.setPivotX(0);
    imageView.setPivotY(0);
    imageView.animate()
             .scaleX(scaleX) //about 0.3
             .scaleY(scaleY) //about 0.3
             .x(finalX) //location x of the image
             .y(finalY) //location y of the image
             .setListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animation) {
                            int reqWidth = imageView.getWidth(); //result 500 as expected
                            int reqHeight = imageView.getHeight();//result 500 as expected
                            Drawable image = imageView.getDrawable();
                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            int reqWidth = imageView.getWidth() * imageView.getScaleX;
                            int reqHeight = imageView.getHeight() * imageView.getScaleY;
                            Drawable image = imageView.getDrawable();
                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {}

                        @Override
                        public void onAnimationRepeat(Animator animation) {}
                    });

Upvotes: 1

Related Questions