Smooth floating animation using translation in XML

10,551

Fixed it! For those interested, I finally found a fix for my problem. I had to set the repeat mode to reverse and use just the one translate movement. The solution is below:

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

<alpha android:fromAlpha="0.0" android:toAlpha="1.0" 
  android:interpolator="@android:anim/accelerate_interpolator" 
  android:duration="1000"
  android:fillAfter="true"/>

<translate
android:interpolator="@android:anim/linear_interpolator"
android:fromYDelta="0%p"
android:toYDelta="10%p"
android:duration="2000"
android:startOffset="0"
android:repeatCount="infinite"
android:repeatMode="reverse"></translate>

Share:
10,551
Admin
Author by

Admin

Updated on June 07, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm developing a game for Android in Eclipse and I'm trying to get an image to smoothly translate up and down to look like it's floating up and down infinitely. As is, it translates smoothly, but looks choppy in the transition from going down to going up and vice versa. I don't believe I have a firm grasp on how fromDelta and toDelta work as far as units. I've tried searching this site and google for information on this, but, while able to find solutions to all my other issues this way, was not able to find a solution for this.

    My code for the XML animation file is below:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillEnabled="true"
    android:fillAfter="true" >
    
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" 
          android:interpolator="@android:anim/accelerate_interpolator" 
          android:duration="1000"
          android:fillAfter="true"/>
    
    <translate
    android:interpolator="@android:anim/linear_interpolator"
    android:fromYDelta="0%p"
    android:toYDelta="10%p"
    android:duration="2000"
    android:startOffset="0"
    android:repeatCount="infinite"></translate>
    
    <translate
    android:interpolator="@android:anim/linear_interpolator"
    android:fromYDelta="10%p"
    android:toYDelta="-10%p"
    android:duration="2000"
    android:startOffset="2000"
    android:repeatCount="infinite"></translate>
    
    </set>
    

    I've tried doing from 10%p to 0%p in the second translate but that didn't work. Neither did from 10%p to 0%p. Any help would be greatly appreciated. Thanks!