Buzz Burrowes
Buzz Burrowes

Reputation: 21

Struggling with view animations

I've got an absolute layout. In that layout is a custom view that takes up the left 3rd of the screen. I put a button in the layout that I want to cause the custom view to slide on and off of the screen. I've tried using animation resources (translates... "slidein" and "slideout") and the function startAnimation on the custom view, but I can't get the behavior I am looking for.

OK... I start with the custom view visible and in onCreate I find the view and animate it off screen using my slideout animation. That works fine. I figured out that I need to set "fillAfter" in the animation so that the custom view stays off screen.

Now, when I press my button I want to cause the custom view to slide back on the screen, so I trigger my slidein animation using startAnimation again but with slidein. BUT... that causes the view to first jump back to its original position AND THEN slide to the right... causing it to finish in the middle of the screen.

How do I get the animation to use the view's current position as the animation starting position, not its original position?

Thanks

Upvotes: 2

Views: 855

Answers (2)

Theo
Theo

Reputation: 6083

I also experienced the flicker described in this question. My solution was to use the improved Honeycomb animation APIs. There is a convenient library that ports these all the way back to Android 1.0:

http://nineoldandroids.com/

For more on Honeycomb Animation APIs see: http://android-developers.blogspot.com/2011/02/animation-in-honeycomb.html

In my case I had 2 overlapped LinearLayouts inside a RelativeLayout. I wanted to slide the top LinearLayout off the screen and reveal the bottom LinearLayout below. Then I wanted to slide to top LinearLayout back on screen to its original position so the top layout covered the bottom layout again. Using the old animation APIs I was seeing a flicker before the second animation (offscreen -> onscreen) was starting.

With the new APIs this task turned out to be trivial:

// Slide out (add to button handler)
ObjectAnimator.ofFloat(mTopLayout, "translationY", mTopLayout.getHeight()).start();

// Slide back in  (add to button handler)
ObjectAnimator.ofFloat(mTopLayout, "translationY", 0).start();

The Honeycomb Animation APIs actually move objects around on the screen (rather than pretending to move them like the older animation APIs), so there is no need to fool around with filleAfter, fillBefore, etc.

Upvotes: 2

Eric Novins
Eric Novins

Reputation: 431

Look into setting the fillAfter property to keep the end animation state

Upvotes: 1

Related Questions