How to return a view to its original size after scaling ViewPropertyAnimator?

This is my code. Here the size of the image decreases after the animation. When I click again ImageView, I just want ImageViewin my original size. I am starting, so I need help. I tried something like:

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

at the beginning setonclicklistener, but it does not 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();
                }
                    });
+4
source share
1 answer

You can check the current scale value View(either scaleX, or scaleY), in this case it does not matter, since you scale both of them equally) and increase / decrease the size based on this value.

For instance:

// 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 , , "reset" it :

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

Source: https://habr.com/ru/post/1694529/


All Articles