Pause after animation - android

I have an AnimationSet that does 3 different animations.
After the animations end, I would like to pause the app for 3 secs, before everything snaps back to start position.
How do I do that ?

Well, there is a animation.hasEnded() function you could use to test if the animation has finished. When that returns true, you could use a java timer to schedule a task after however many milliseconds you want to wait. AnimationSet also has a function to get the duration of the Animations.
Take a look at...
Animation
and AnimationSet

I think you could probably achieve this effect somehow with an AnimationListener Set one to get a callback when your animation ends and add a delayed runnable that will move everything back to the proper position for you. However I think it might get somewhat convoluted to do it this way since it would require setting fillAfter to true, then manually placing your Views in their original position inside your Runnable thats gets posted with a delay.
I think a more straightforward solution to get the same effect is to simply add a 4th animation to your set that has a delay to make it start after all of the others are complete. Make this 4th animation last for 3 seconds, and make it have no visible effect (i.e. grow by 0%). That should build in a 3 second pause for you and still handle moving all of your Views back to their original position (without the need to do it manually). With this solution you'd leave fillAfter set to false, and that would cause your Views to "snap back". By adding another animation that lasts for 3 seconds but has no visible effect it will seem like a pause to the user.

Related

Animation at the beginning of activity jumps

I am working on animating custom Views for my Android app. I have accomplished this via Property Animations and calling invalidate() on the View in the onAnimationUpdate() callback, as per https://developer.android.com/guide/topics/graphics/prop-animation.html:
Depending on what property or object you are animating, you might need to call the invalidate() method on a View to force the screen to redraw itself with the updated animated values. You do this in the onAnimationUpdate() callback.
My problem is that when these animations are run at the beginning of a newly launched Activity, they skip frames at the beginning, causing them to jump very noticeably. I have tried both:
Starting the animation immediately from the Activity's onCreate() method
Starting the animation upon the OnGlobalLayout() callback using the ViewTreeObserver of the Activity's root view.
I did the latter since I thought maybe the animation was invoked before the layout had been finalized, but the result is the same.
With logs, I determined that the onAnimationUpdate() callback is called consistently throughout the animation (that is, every 10-20 ms or so, from start to end). onAnimationUpdate() simply calls invalidate(), which should force the View to redraw itself, ideally immediately (but the documentation only claims this happens "at some point in the future"). That seems to be precisely the problem: onDraw() is only called once or twice at the very start before not being called for about 250 ms. After this, it resumes being called every 10-20 ms, as it should have the whole time. But that block of time causes very noticeable lag in the animation.
To be clear, this problem only happens at the beginning of the Activity. If I simply set a 300 ms delay before starting the animation, it runs smoothly all the way through. But I don't like that solution, since it's hacky. It seems the problem is that onDraw() is not called immediately upon invalidate() near the beginning of the Activity. But, I can't figure out why this is, what's blocking onDraw(), or how to fix it at all.
I found only this StackOverFlow thread: Animation at the beginning of activity skips frames where the poster has the identical problem. The basic code is there and the videos make the problem clear. I can post my code too, but I think that the fact that the problem appears in the most basic test app shows that there's something else going on.
Since there are no codes attached, I'm assuming you have some transition animation to the activity. If this is the case, it might cause the problem.Since there are two animations running simultaneously. Disable the transition and give it try.
startActivity(intent);
getActivity().overridePendingTransition(0, 0);
It sounds like your draw loop UI thread is getting starved.
I'd use traceview to be sure that there aren't any methods blocking your draw calls. http://tools.android.com/tips/traceview
This should help you determine what's being invoked instead of the onDraw method.

Android Lollipop's BeginDelayedTransition not always working

I have an activity which starts with a number of invisible views. I then use handler.postDelayed to make every view visible again with a slide animation and the BeginDelayedTransition method. Normally all the views should slide in, one after another. Anyway i noticed that depending on the time interval between each runnable and even depending on the device i test the app on, some of the animations are totally skipped and the view just appears without any transition. I think it may be related to the time it takes to beginDelayedTransition to calculate the changes in the visibility thus instantiating the correct animation.
Is anyone experiencing the same problem? Is there any workaround for this, other than calling a TranslateAnimation every time?
Thanks in advance!

Why ObjectAnimator.cancel() doesn't always work?

So there are a few questions about animations not ending when using cancel() on them. You also need to call clearAnimation on the view. Can someone explain why is that? It's also very strange to me, that the View needs to know about what animates it.
You call clear animation to reset the transformation matrix that the view is using to transform its canvas during the animation. The main problem is that old Android animation system is a crap since it relied on animating a "snapshot" of the actual view.
As for the cancel, when you invoke cancel what happens is that you stop the runnable that is applying the steps of your Animation. Of course you cannot be sure in which state your animation is getting stopped
To be clear:
The View Animation Framework , a.k.a "old animation framework", only animates the translations of the view, and not the actual properties. So lets say you animate the view's location on the X dimention: the value of the view's X property remains the same.
In relevance to your question, this means that canceling the animation draws the view back in its original position. So it doesnt matter when did the animation was canceled.
Object animation, introduced in Android 3.0, animates the actual properties. This means that when you cancel the animation, the view remains in its relevant position. So you might not know exactly WHEN was the animation canceled, but you can clearly find out WHERE is it drawn right now.
If you need object animation for older versions of Android, and frankly why go any other way, you can use the 9OldAndroids library.
Mor on the difference between the animation frameworks here.

Android Animation - Button stays clickable

I am making a game in which I have 5 buttons, looking like clouds, falling from the "sky".
That means that when my activity starts, 'clouds' cannot be seen, since the marginTop is set to -100dp.
From that position they start falling down untill they get lost on the bottom side of the screen.
The thing is, I need those buttons to be clickable, during the process of animation.
So far, I found some documentation about how I can make the buttons clickable AFTER the animation ends. But I don't need that. I need to be able to click on the buttons through the animation time itself.
NOTE: I need something that works with versions before 3.0.
Anybody has any link to documentation or some example or anything ?
After doing some research, I found out that there are two types of animations:
View Animation and Property Animation.
The view animation can only animate View objects. It also lack a variety of animations, since it can do only stuff as scale, rotate, move... It cannot change background color, for example.
Also, the disadvantage of the View Animation is that it only change the position of where the View object is DRAWN. Physically, it still stays in the same position.
That's why the button is un-clickable, after the View Animation is finished upon it.
Property Animation, in the other hand, can animate both View and non-View objects and it doesn't have constraints as the View Animation.
When objects are moved, for example, with the property animation, they are not just drawn on some other position on the screen, but they are actually MOVED there.
Now, Property Animation is a lot more complex to write than the View Animation, so if you don't really need all the advantages of the Property Animation, it is suggested to use View Animation.
Source:
Property vs View Animation
Tutorial and SupportLybrary up to API 1:
nineoldandroids
You can change the buttons to imageViews and then do
imageView.setOnClickListener(myListener)
then set myListener to do whatever you previously wanted to happen on the buttons onClick. Your activity will have to implement OnClickListener
Added bonus: you can make the images look like clouds :)

Android AnimatorSet animation + setStartDelay VS AnimatorListenener.onAnimationStart?

I have a question about the Android AnimatorSet object. I'm trying to create a TextView dynamically and set it's visibility to GONE and make it appear when my animation starts after the start delay. To accomplish this, I've setup an onAnimationStart listener to tell me when the animation is starting so that I can set the TextView to visible. I add that TextView into an AnimatorSet to perform some animations on the alpha and translateY but I also set the setStartDelay to a value so that the animation starts at 2500 milliseconds. My problem is that I want the TextView to become visible when the animation actually starts at the 2500 milli mark, but onAnimationStart is only being called when my AnimatorSet.start() function is being called, and not the requested 2500 milliseconds after. This is resulting in my TextView's becoming visible before their animation actually starts (after the setStartDelay period). How do I overcome this and get the TextView objects to go visible only after the setStartDelay period???? Thank you very very much, you are the best StackOverflow!!!! :) :) :)
I have been having the same issue. I am animating 3 ValueAnimators in an AnimatorSet. I was doing a "playTogether()" in my set like so:
set.playTogether(alpha,animScale,transY);
set.start();
And found that the animation delay caused issues. Instead I tried the following:
set.play(animScale);
set.play(transY);
set.play(alpha);
set.start();
It seems to work!

Categories

Resources