qwertz qwertz - 3 months ago 51
Android Question

Android pop in animation

I mave a floating action button in my app which I want to sort of pop in, where it is invisible and starts to grow to a size like 60dp and then resizes back to the normal size of 56dp. How can that be done? I know how to do a normal fade in animation, but not a pop in.

Answer

I would create a animation file in res/anim and use a sequential scale animation like so:

expand_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale 
        android:fromXScale="0.0" 
        android:fromYScale="0.0"
        android:toXScale="1.1" <!--set the scale value here-->
        android:toYScale="1.1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="400"/> <!--length of first animation-->


    <scale 
        android:fromXScale="1.1" <!--From whatever scale you set in the first animation-->
        android:fromYScale="1.1"
        android:toXScale="1.0" <!--Back to normal size-->
        android:toYScale="1.0"
        android:pivotX="50%" 
        android:pivotY="50%"
        android:startOffset="400" <!--start 2nd animation when first one ends-->
        android:duration="400"/>
</set>

Then in your activity:

Animation expandIn = AnimationUtils.loadAnimation(this, R.anim.expand_in);
actionButton.startAnimation(expandIn);