Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
                }
                    });
like image 836
MarGin Avatar asked Mar 06 '18 12:03

MarGin


People also ask

How do I reset my ValueAnimator?

For ValueAnimator and ObjectAnimator can be like this a try: animator. addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { animation. removeListener(this); animation.

How do you use Viewpropertyanimator?

You create the Animator , set any optional properties such as the duration or repetition attributes, and start it. For example, to fade an object called myView out, you would animate the alpha property like this: ObjectAnimator. ofFloat(myView, "alpha", 0f).

What is Property animation in Android?

A property animation changes a property's (a field in an object) value over a specified length of time. To animate something, you specify the object property that you want to animate, such as an object's position on the screen, how long you want to animate it for, and what values you want to animate between.


1 Answers

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();
like image 103
earthw0rmjim Avatar answered Sep 19 '22 19:09

earthw0rmjim