缩放动画不会更改ImageView的大小

时间:2019-07-08 09:04:04

标签: android android-animation scale

我正在创建一个应用程序,用于从资源中加载可绘制对象并将其显示在占位符ImageView中。 当用户单击按钮加载下一张图像时,我创建了一个动画,该动画将旧图像移动并缩放到新位置,并将新的可绘制对象加载到占位符。

在30张图像之后,我正在运行OOM,这很有意义。

我想做的是在动画结束后对每个图像重新采样,因为我以500X500占位符开始,以1/3结束。

问题在于,完成动画后,ImageView的宽度和高度保持不变。 缩放动画是否应该更改ImageView的宽度和高度?

这是动画的代码:

    //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) {}
                    });

1 个答案:

答案 0 :(得分:1)

scaleX()使View的scaleX属性被动画化为指定值(reference)。它不会直接更改布局的宽度/高度值。 (reference

通过将scaleX / Y值乘以得到预期的宽度/高度值。

//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) {}
                    });