50

As in the title of my question what are the methods setFillBefore() and setFillAfter() supposed to do?

I was hoping setFillAfter() would make the change to the View permanent after an animation has completed, but this is incorrect?

CaptJak
  • 3,542
  • 1
  • 25
  • 48
D-Dᴙum
  • 7,278
  • 8
  • 50
  • 90

1 Answers1

125

The answer is yes, they do work, just probably not for what you expect - for instance, the description for setFillAfter(boolean fillAfter) says

If fillAfter is true, the transformation that this animation performed will persist when it is finished.

and when set to true it does do this.

However, unintuitively an animation on Android does not actually animate the View itself, rather it animates a bitmap representation of the View.

The issue you are probably having is that after an animation the View goes back to being how it was before the animation - setFillAfter and setFillBefore cannot help you with that, because in that situation what you really want to do is set the properties of the View to be same as the animated representation (they are separate things), and setFillAfter and setFillBefore only apply to animation properties, not View properties.

The reason they exist is for chaining animations. Suppose you had a translate animation followed by a fade out. If you did not set setFillAfter(true) on the translate animation, then the View would translate, jump back to it's original position and then fade out. With setFillAfter(true) set on the translate animation, the view will move and then fade out at it's current spot.

Joseph Earl
  • 22,863
  • 9
  • 74
  • 88
  • 12
    Thanks Joseph! Your post has helped me a great deal in understanding the animation API. The android docs are certainly lacking in areas! – D-Dᴙum May 06 '11 at 19:56
  • 19
    Note that the new animation system in 3.0 addresses exactly this aspect of the previous animation system (among other things); we've added properties to View (alpha, translationX, etc.) and you can create animations (e.g., ObjectAnimator) that act on those properties. So not only is the View drawn differently because of the animation ... it *is* different, because it's properties are actually altered by the animation's use of these properties. So hopefully this will get less confusing as people adopt the new system. – Chet Haase May 06 '11 at 22:16
  • 1
    @ChetHaase Yes it does, and thank you for introducing the property animation system. For those targeting a lower minimum API level please see the NineOldAndroids library (http://nineoldandroids.com/) developed by the legend Jake Wharton which backports the new animation system to Android 1.0 and above. – Joseph Earl Jul 24 '12 at 20:24
  • @JosephEarl I have used a animation set of fade, scale and translation on `Button` view and I am not able to click on Button after the animation completes. I have `setFillEnabled()` and `setFillAfter()` to true. Mu Button zooms out and remain in its position but when I click on it nothing happens. But when I click on button's original position i.e position before the animation it is working. It seems like original buttons lies there and buttons are invisible. – Nishant Sep 10 '12 at 10:11
  • Yes, the animation does not actually move the view itself, just a bitmap representation of the view. – Joseph Earl Sep 11 '12 at 11:15
  • 4
    By the way, it seemed worth documenting the mysterious behavior of these fill flags a bit more closely, so I wrote up this article to help explain what can be very confusing: http://graphics-geek.blogspot.com/2011/08/mysterious-behavior-of-fillbefore.html – Chet Haase Oct 17 '12 at 00:46
  • @JosephEarl: Can I use these functions to create kind of a "panorama viewer"? I mean, if the image fits in the screen vertically but not horizontally, and I slowly translate horizontally the image with an animation, how can I begin viewing the part of the image that was hidden? I tried to create a panorama with an animation, but the old image is slowly being substituted by black, not with the hidden part of the image... – Luis A. Florit Jan 04 '13 at 19:54
  • They need to deprecate the old Animation APIs because they are so confusing and less effective. Why would you animate something only to have it revert to it's previous values? makes no sense. – Bron Davies Dec 17 '15 at 03:59