MarGin
MarGin

Reputation: 2558

How to return a View to its original size after it is scaled by ViewPropertyAnimator?

This is my code. Here the image size is decreased after the animation. When I click the ImageView again I just want the ImageView in its original size.I am a beginner so I need some help. I have tried something like :

football.animate().scaleX(1f).scaleY(1f).setDuration(1000).start();

at the beginning of setonclicklistener but that doesn't work.

Thanks in advance

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

Upvotes: 7

Views: 1298

Answers (1)

earthw0rmjim
earthw0rmjim

Reputation: 19427

You could check for the current scale value of the View (either scaleX or scaleY, doesn't matter in this case since you scale both of them equally) and increase/decrease the size based on that value.

For example:

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

EDIT (addressing your comment below): If you would like your View to shrink from its original size every time you click on it, then you just have to "reset" it before each animation:

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

Upvotes: 3

Related Questions