如何在ViewPropertyAnimator缩放后将View返回到原始大小?

时间:2018-03-06 12:16:11

标签: android android-animation android-view

这是我的代码。这里动画后图像尺寸减小了。当我再次点击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 个答案:

答案 0 :(得分:2)

您可以检查View的当前比例值(scaleXscaleY,在这种情况下无关紧要,因为您同等地缩放它们)并增加/根据该值减小尺寸。

例如:

// 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();