Gagan
Gagan

Reputation: 755

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.

Upvotes: 3

Views: 3452

Answers (1)

A Honey Bustard
A Honey Bustard

Reputation: 3493

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

Upvotes: 7

Related Questions