如何在使用ViewPropertyAnimator缩放视图后将其恢复到原始大小?

7
这是我的代码。在动画之后,图像大小会减小。当我再次点击ImageView时,我只想让ImageView恢复到原始大小。作为一个初学者,我需要一些帮助。我尝试过类似以下的代码:
football.animate().scaleX(1f).scaleY(1f).setDuration(1000).start();

我尝试在setonclicklistener的开头,但是没有起作用。

提前致谢。

football.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
                            // values from 0 to 1
                            animator.setDuration(1000); // 5 seconds duration from 0 to 1
                            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
                            {
                                @Override
                                public void onAnimationUpdate(ValueAnimator animation) {
                                    float value = ((Float) (animation.getAnimatedValue()))
                                            .floatValue();
                                    // Set translation of view here. Position can be calculated
                                    // out of value. This code should move the view in a half circle.
                                    football.setTranslationX((float)(100.0 * Math.sin(value*Math.PI)));
                                    football.setTranslationY((float)(400.0 * Math.cos(value*Math.PI)));
                                }
                            });

                            animator.start();
football.setScaleType(ImageView.ScaleType.CENTER);

                            //here the scaling is performed
        football.animate().scaleX(0.4f).scaleY(0.4f).setDuration(1000).start();
                }
                    });
1个回答

3
你可以检查当前的 View 缩放值(无论是 scaleX 还是 scaleY,在这种情况下都没有关系,因为你会等比例缩放两者),然后根据该值增加或减小大小。
例如:
// if the current scale is lesser than 1.0f, increase it to 1.0f
// otherwise decrease it to 0.4f
float scaleValue = football.getScaleX() < 1.0f ? 1.0f : 0.4f;
football.animate().scaleX(scaleValue).scaleY(scaleValue).setDuration(1000).start();

编辑(回应你下面的评论):如果你想让你的View每次点击时都能从其原始大小缩小,那么你只需要在每次动画之前“重置”它:

// resetting the scale to its original value
football.setScaleX(1.0f);
football.setScaleY(1.0f);

// shrinking
football.animate().scaleX(0.4f).scaleY(0.4f).setDuration(1000).start();

每次点击时,我需要视图从原始大小变为小尺寸。因此,在开始时,我希望保留其原始大小。 - MarGin

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