Search code examples
androidimagetimercountdownstopwatch

Create stopwatch timerclock countdown timer with image flip every second android


I was looking over internet a lot, but only found examples how to text is changed every second or show even miliseconds.

I was using those examples to change picture when second was change, but timer was not accurate anymore. On image change the app respond with random delay for about 20 milisec or 200 milisec.

I was using class Runnable with postDelay and AsynTask where onPostExecute refresh UI, but both working exactly the same with lag.

What is the best practice to change picture every second? And in the future I have an idea to start animation of 5 images every second (more fancy clock timer).

I would post my code, but now is really a mess of all examples I found till now.


Solution

  • well you have not shared the code so I have to guess only,

    I would suggest to have the images as bitmap or drawable initialized already, may be in a hashtable.

    After that, start your timer and place the code to replace the image at right place. Point is you should not spend time other than replacing image in you thread.

    Edited: If you want to do it via code :

    before starting your animation create an array of Bitmaps not drawable ids (or the object of Drawble class). Why because you need to save time spent by system in creating the bitmaps from the ids you provide in array and then set to you image view.

    Also, you can use AnimateDrawbles for this which is very easy optimized and reliable.

    here is a sample. Just tune the duration variable in the xml according to your need.