I wanted to animate a button, so I used this answer:
https://stackoverflow.com/a/4852468/559525
and got it animating on the first attempt. But then when I tried to stop the animation from various places in my application, I found that it will ONLY stop the animation if I call view.clearAnimation() from inside the onClick() callback.
My first guess is this has to do with multi threading synchronize issues, but I know about the UI thread and I was pretty sure I was calling the clearAnimation() method from approved places in the main UI thread.
My other thought was that the UI needed to be invalidated or refreshed? But I tried putting this call right before doing an invalidate on my main layout and that didn't help. I know my invalidate is working properly because it works to update other button attributes like color.
Thanks for any advice!
Related
From Activity lifecycle we know, that in onResume the UI is visible. But if I set a breakpoint on onResume, I still don't see it, only after that. I heard there's some method which can check it, but I can't remind how it calls. We used it to make better animation. So how can I be fully sured that UI is ready?
Add global layout listener on view you want to check. Also please make sure that you remove listener once your work is done else it will keep getting called multiple times.
#onik shared a good link that should solve your problem
This ensures lay-outing. Drawing happens continuously and frame are refreshed according to sys clock.
I recommend watching: https://www.youtube.com/watch?v=Q8m9sHdyXnE
This will give you good idea on android drawing and layouting
I am starting off some animations as soon as my Activity is created.
However, by the time the Activity is fully visible the animation has already half completed.
I originally had it in onCreate but have now moved in into onWindowFocusChanged and only start the activity once I know onResume has also been called (I'm setting a boolean in onResume)
Is there anyway of knowing when an Activity is fully visible? Or am I going to have to set a 1 second delay? (This seems extremely hacky and potentially still won't work on slower phones/tablets)
If your intention is to display your animations to the user, one way you could guarantee they see the entire animation is by triggering it with an onClickListener() for the whole screen, and just wait for them to touch it?
for layout animations you can use LayoutAnimation, here is the link here
As of API level 21, you can implement the Activity#onEnterAnimationComplete() callback. Unfortunately, it seems like there's no AppCompat equivalent at this point.
I am making an interactive walkthrough for one of my apps, and in a couple of spots an alpha animation and some various other UI changes (such as checkbox pressed states) would be really great.
The walkthrough has a back and next button. The basic idea is that the next button would start the animation (maybe as a thread?), and if at any point in the animation the back button was pressed, that the animation would stop.
I have looked into the built in Android animation library, but have sort of seen a lot left to be desired. My next thought was a thread, but I know I can't change the UI from an outside thread. Also I want to leave the UI thread open for my back button listener.
Any thoughts on how these simple animations could be achieved?
You're right you can't update UI elements directly from another thread. But you can do this indirectly with Handlers. Handlers are basically a type of IPC that allows you to queue up messages to the UI for proceessing. So what you do is basically create a Handler in your Activity and pass this handler to your process thread. When there's something you want to update in the UI from the process thread just do mHandler.sendEmptyMessage(UPDATE_SOME_VIEW).
Take a look at handlers here.
So, I finally got my Animation sorted out (thanks to all that helped!).
However, I still have the problem that the animation is only displayed AFTER the other updates to the layout are.
What I am doing is dealing out a card to a user.
The animation shows the card sliding across the table.
However, the animation itself shows after the updates to the player's hand and the cards on the table are shown.
I tried making the update a separate thread and waiting on it but then I can't update the UI directly from there.
Sending a message to the UI thread doesn't help because the separate thread then ends after sending the message and I am back where I started.
I guess I am probably doing something wrong or missing something simple.
Any help would be appreciated.
Finally got it sorted out.
I built my own animation class which implements AnimationListener.
I then use onAnimationEnd to make the changes in the so that they happen after the animation.
I am changing the value of several RatingBars upon completion of a child activity (inside the onActivityResult() callback). My problem is that the parent activity has not finished drawing before my RatingBars value-changing code is executed, so I get some funky lag and a half-way completed "animation" before the parent layout has even been displayed.
I'm familiar with the document.ready() function in jQuery, which waits until the DOM is completely ready to commence any script therein.
Is there any way to achieve the same result with Android? In other words, I need a way to wait until an activity has completely finished drawing itself to the user's screen before some code is executed.
This might be a simple thing in Android, but I'm pretty noob. Thanks for your time and help.
-Steve
Could you simply put the code in the onResume method which will be called after the views have been set up?? (Not sure if this include getting drawn)
Another possibility is to create a handler and dispatch a method to it at the end of the onCreate method, this will get run on the UI thread but I imagine this won't get processed until the UI thread has finished the more important stuff (i.e. drawing the views)
This is largely just me putting down possible ideas, I know there is a way of achieving this I just can't remember how.