Search code examples
javaandroidanimationscale

Android java scale animation bounces


This is the code:

    //These are the animations of the Menu button getting bigger and smaller
    final ScaleAnimation MenuScaleBigger = new ScaleAnimation(1f, 1.1f, 1f, 1.1f,1,0.5f,1,0.5f);
    MenuScaleBigger.setDuration(400);
    final ScaleAnimation MenuScaleSmaller = new ScaleAnimation(1.1f, 1f, 1.1f, 1f,1,0.5f,1,0.5f);
    MenuScaleSmaller.setDuration(400);

    MenuScaleBigger.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            Menu.setScaleX(1.1f);
            Menu.setScaleY(1.1f);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

    MenuScaleSmaller.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            Menu.setScaleX(1f);
            Menu.setScaleY(1f);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    //This is the click listener that starts the animation of getting bigger
    Menu.setOnClickListener(new View.OnClickListener() {
        int counter = 0;
        @Override
        public void onClick(View v) {
            if(counter == 0){
                Menu.startAnimation(MenuScaleBigger);
                counter++;
            }
            else{
                Menu.startAnimation(MenuScaleSmaller);
                counter--;
            }
        }
    });

I think that the bouncing happens because of the Menu.ScaleX(); and Menu.ScaleY(); , but I need some way of making the animation stay in the last frame.

What I want is that if I click the Menu Imageview it becomes bigger, and stays bigger, and if I press it again it becomes smaller going back to the original size.


Solution

  • Remove the AnimationListeners and add these two lines

    MenuScaleBigger.setFillAfter(true);
    MenuScaleSmaller.setFillAfter(true);
    

    setFillAfter(true); makes sure that the View stays in the position after animation and doesn't come back to it's base position