Reputation: 1889
I've seen the issue many times, where people need the view to stay put after translating it in an animation. The fix is simple: just add fillAfter="true" and fillEnabled="true" to the XML.
This works, and the view does stay put after the animation. However, upon calling the animation again, the view resets to the original point and simply moves to the same spot.
For instance, say my view is at (0,0) and I call this animation by clicking a button:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillEnabled="true" >
<translate
android:duration="100"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="20"
android:toYDelta="20" >
</translate>
</set>
It moves from (0,0) to (20,20) as expected. However, if I hit the button again and call the same animation, it magically jumps back to (0,0) and translates to (20,20) all over again.
What if I want it to do the same translation, but originating from the new spot? So I hit the button once, it goes from (0,0) to (20,20). I hit it again, it goes to (40,40), then (60,60), and so on.
Is there a simple way to do that in the XML, or am I going to need to create some variable that updates itself constantly upon each motion (something like START_LOCATION?)
At that point, would it just make sense for me to do this all right in the Java and forget the XML? I actually quite like how compact and simple the XML is compared to the Java, but I'll begrudgingly switch over if I have to.
EDIT I just had a smart idea, and removed fromXDelta="0" (and YDelta as well), since I imagined if you only give it the "toXDelta" then it won't care where it starts from. It is a "delta," after all, which we all know means "change," regardless of origin. But that did nothing. Sadface.
Upvotes: 1
Views: 1873
Reputation: 134664
So, when you animate a View
using the normal view animations, you're only animating the position at which it is drawn. You're not affecting its actual location in the layout. Assuming you intend to make the View
change position permanently, you'll need to update the layout parameters of the View
after the animation is complete to match the movement you make with the animation.
Upvotes: 1