【问题标题】:Scaling animation doesn't change the ImageView size缩放动画不会改变 ImageView 的大小
【发布时间】:2019-07-08 09:04:04
【问题描述】:

我正在创建一个应用程序,从资源中加载可绘制对象并将其显示在占位符 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) {}
                    });

【问题讨论】:

  • 这是动画的完整代码吗?
  • 是的 - 在那之后我什么都不做

标签: android android-animation scale


【解决方案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) {}
                    });

【讨论】:

  • @Itamar 如果有效,请接受答案(如果无效,请发表评论)。干杯。
  • 我没有意识到缩放并不能真正缩放图像,而只是对其进行操作。
  • 是的,这相当令人惊讶。如果我不得不冒险猜测,那可能是为了提高性能。通过保持宽度和高度不变,布局更改无需在 View 缩放时计算。
猜你喜欢
  • 1970-01-01
  • 2015-01-26
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 2017-12-02
  • 2012-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多