Smooth floating animation using translation in XML

和自甴很熟 提交于 2019-12-07 03:49:26

问题


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!


回答1:


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>



来源:https://stackoverflow.com/questions/21066509/smooth-floating-animation-using-translation-in-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!