Sandah Aung
Sandah Aung

Reputation: 6188

Android animation: bounce upward

I am not good at Android animation yet but I need to implement a bounce animation in my new project. The following an animation file that I use to accomplish it.

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

    <scale
        android:duration="600"
        android:fromXScale="1"
        android:fromYScale="0.5"
        android:pivotX="50%"
        android:pivotY="0%"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:startOffset="5000"
        android:toXScale="1.0"
        android:toYScale="1.0" />

    <alpha
        android:duration="600"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

</set>

This animation will cause the object to be animated to bounce in downward direction. What can I do to make it bounce upward?

(It will be much better if you could provide the explanation as well so that I can learn about Android animations.)

Upvotes: 1

Views: 2117

Answers (2)

Rahul Raina
Rahul Raina

Reputation: 3450

Here have xml animation code that makes object fall down with bounce effects:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromYDelta="-300%"
        android:toYDelta="100%"
        android:duration="200"
        android:repeatMode="reverse"
        android:interpolator="@android:anim/accelerate_interpolator"
    />

    <scale 
        android:fromXScale="1"
        android:toXScale="1.08"
        android:fromYScale="1"
        android:toYScale="0.8"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="1"
        android:duration="30"
        android:repeatMode="reverse"
        android:startOffset="200"
        android:interpolator="@android:anim/bounce_interpolator"
        />

   <translate
        android:fromYDelta="100%"
        android:toYDelta="-200%"
        android:duration="120"
        android:startOffset="260"
        android:interpolator="@android:anim/decelerate_interpolator"
        />
   <translate
        android:fromYDelta="-200%"
        android:toYDelta="100%"
        android:duration="120"
        android:startOffset="380"
        android:interpolator="@android:anim/accelerate_interpolator"
        />

    <scale 
        android:fromXScale="1"
        android:toXScale="1.08"
        android:fromYScale="1"
        android:toYScale="0.8"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="1"
        android:duration="30"
        android:repeatMode="reverse"
        android:startOffset="500"
        android:interpolator="@android:anim/bounce_interpolator"
        />

    <translate
        android:fromYDelta="100%"
        android:toYDelta="-100%"
        android:duration="70"
        android:startOffset="560"
         android:interpolator="@android:anim/decelerate_interpolator"

        />
    <translate
        android:fromYDelta="-100%"
        android:toYDelta="100%"
        android:duration="70"
        android:startOffset="640"
        android:interpolator="@android:anim/accelerate_interpolator"
        />  

    <scale 
        android:fromXScale="1"
        android:toXScale="1.08"
        android:fromYScale="1"
        android:toYScale="0.8"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="1"
        android:duration="20"
        android:repeatMode="reverse"
        android:startOffset="710"
        android:interpolator="@android:anim/bounce_interpolator"
        />

</set>

Upvotes: 0

Daniel Liu
Daniel Liu

Reputation: 499

In this case, you just should to adjust android:pivotY. According to the official document, android:pivotY means the Y coordinate to remain fixed when the object is scaled, specified as an absolute number where 0 is the top edge. If you want it grows upward, you should set android:pivotY to "100%". Likes:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/bounce_interpolator" >
    <scale
        android:duration="600"
        android:fromXScale="1"
        android:fromYScale="0.5"
        android:pivotX="50%"
        android:pivotY="100%"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:startOffset="5000"
        android:toXScale="1.0"
        android:toYScale="1.0" />

    <alpha
       android:duration="600"
       android:fromAlpha="0.0"
       android:toAlpha="1.0" />

</set>

Upvotes: 3

Related Questions