如何在缩放动画后获取View的新宽度和高度

3

我使用了 ScaleAnimation 来调整我的 ImageView 的大小。但是在动画完成后,我应该如何获取我的 ImageView 或其中的位图的尺寸。

这是我的代码:

ScaleAnimation animation = new ScaleAnimation(m_oldZoomRatio, m_currentZoomRatio, m_oldZoomRatio, m_currentZoomRatio);

        animation.setFillAfter(true);
        animation.setFillEnabled(true);
        animation.setAnimationListener(new AnimationListener() {

            public void onAnimationStart(Animation animation) {
            }

            public void onAnimationRepeat(Animation animation) {
            }

            public void onAnimationEnd(Animation animation) {
            }
        });
        m_child.startAnimation(animation);

m_oldZoomRatio != m_currentZoomRatio 时。

我可以看到我的视图在运行时会改变其布局。但是当获取它的宽度和高度时,我收到的是创建视图时的原始值。


如果你有解决方案,如果可能,请与我分享。 - Prince Dholakiya
1个回答

1
尝试使用动画监听器,在动画结束时更改宽度和高度。
  <animation-object>.setAnimationListener(new AnimationListener() 
     {

        @Override
        public void onAnimationEnd(Animation arg0) {
            img.getWidth();
                img.getHeight();
                  //OR 
                 int h=img.getLayoutParams().height;
                 int w=img.getLayoutParams().width;
                // For changing actual height and width
             view.getLayoutParams().width = newWidth; 
               view.getLayoutParams().height =    newHeight; 
view.requestLayout();

        }

        @Override
        public void onAnimationRepeat(Animation arg0) {


        }

        @Override
        public void onAnimationStart(Animation arg0) {


        }

     });

谢谢您的回复。但我的问题是如何获取尺寸(而不是按照您的建议设置它)。 - ndphu
要获取尺寸,您可以使用getHeight(),getWidth()函数...只需在答案中进行编辑。 - Maneesh
谢谢。但是它们两个都返回了ScaleAnimation之前视图的原始尺寸(但在屏幕上,我的ImageView已经改变了大小)。 - ndphu
好的,我尝试了一下,发现它实际上并没有改变高度和宽度,它只是填充屏幕的动画效果,实际的高度和宽度在之前和之后都是相同的。如果你想要改变实际的高度和宽度,你需要在animation_end函数中指定相应的数值。 - Maneesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接