Android Animate view from off screen not working
Asked Answered
B

2

11

I have a view that is positioned totally off screen and I am trying to animate it onto the screen.

When I call:

view.startAnimation(tA);

nothing happens, tA.initialize and tA.applyTransformation never get called.

If I move the view so that any part of it is visible before I start the animation, then the animation works correctly.

What is preventing a view from being animated when it is positioned off the parent View?

Belfort answered 12/2, 2012 at 18:15 Comment(0)
D
3

It's my understanding from researching the same problem that Android Animations do not do well when provided with offscreen coordinates for their start or finish.

There is some dialog on the Android forums about this bug having been addressed but I'm still experiencing problems on 4.2.

Edit:

On second thought, I just ran across this answer and it provides a working alternative if you can use the newer APIs (ObjectAnimator).

View view = this;
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "y", 0, 100);
anim.setDuration(super.animationDuration());
anim.start();

Where the properties of ObjectAnimator.ofFloat(view, "y", 0, 100); are

ObjectAnimator.ofFloat(Object objBeingAnimated, String propertyBeingAnimated, float startValue, float endValue)
Diffusivity answered 3/10, 2013 at 15:43 Comment(0)
F
0

I found this answer using ValueAnimator to modify the MarginLayoutParams.topMargin (in my case) in onAnimationUpdate(), which fixed the issue. My View starts out with its margin set so that the View is off screen.

The ObjectAnimator approach was promising but did not work for me, it had the same cutoff issue for off-screen views that I got with TranslateAnimation.

Flense answered 18/7, 2014 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.