Konstantinos Natsios
Konstantinos Natsios

Reputation: 2924

Android button animate

I have these buttons here and i want them both to have some animation onclick. To grow up a little bit and then return to normal.

Here are the buttons xml codes.

<ImageButton
        android:layout_width="165dp"
        android:layout_height="60dp"
        android:text="next"
        android:id="@+id/next"
        android:src="@drawable/next"
        android:background="@android:color/transparent"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/linearLayout"
        android:layout_alignEnd="@+id/linearLayout" />

    <ImageButton
        android:layout_width="170dp"
        android:layout_height="60dp"
        android:text="confirm"
        android:src="@drawable/confirm"
        android:background="@android:color/transparent"
        android:id="@+id/confirm"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
         />

These buttons are on one activity_main.xml file.

Can someone help me showing me some stuff of what should i do to make it happen?

Thanks a lot for your time.

Upvotes: 3

Views: 3214

Answers (2)

shhp
shhp

Reputation: 3693

Use Animator can achieve the goal easily:

findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Animator scale = ObjectAnimator.ofPropertyValuesHolder(v,
                    PropertyValuesHolder.ofFloat(View.SCALE_X, 1, 1.5f, 1),
                    PropertyValuesHolder.ofFloat(View.SCALE_Y, 1, 1.5f, 1)
                    );
            scale.setDuration(1000);
            scale.start();
        }
    });

Upvotes: 4

divinemaniac
divinemaniac

Reputation: 277

In the button's onclick, we start the animation we want to use using the view.startAnimation() method. We first load the required animation using AnimationUtils.loadAnimation()

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        view.startAnimation(AnimationUtils.loadAnimation(context, R.anim.button_click));            
    }
}

Now create a file under the anim folder called button_click.xml or whatever you want to name it.

anim/button_click.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
    android:fromXScale = "1"
    android:toXScale = "0.9"
    android:fromYScale = "1"
    android:toYScale = "0.9"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration = "50">
    </scale>
</set>

Upvotes: 1

Related Questions