Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zoomout animation on button press in android

When press a button,i need to animate that button....button will zoom out little on pressed and in orignal size on released..(very much like in tinder)

i use this :

<?xml version="1.0" encoding="utf-8"?>

<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="0.9"
    android:toYScale="0.9" >
</scale>

but it not work according to my need,,,i need that the button remain small till it is pressed. and become normal on released.

like image 970
Gagan Avatar asked Feb 02 '26 16:02

Gagan


1 Answers

You can create a second animation for returning to unclicked state like this:

<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fillAfter="true" 
    android:fromXScale="0.9"
    android:fromYScale="0.9"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.0"
    android:toYScale="1.0" >
</scale>

and then :

yourButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
          // start your first zoom out Animation here
                    break;

                case MotionEvent.ACTION_UP:
         // start zoom in animation which returns to original state
                 break;
            }
            return false;
        }
    });

Dont forget to set fillAfter in your Animations to true, otherwise it will snap back to the original state after the duration has ended. Or in case you dont want a second animation you can call

yourButton.startAnimation(yourAnimation);

inside MotionEvent.ACTION_DOWN and then call

yourButton.clearAnimation();

inside MotionEvent.ACTION_UP

like image 87
A Honey Bustard Avatar answered Feb 04 '26 05:02

A Honey Bustard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!