Android Example Another Analog Clock

Discussion in 'Tutorials & Examples' started by Johan Schoeman, Apr 6, 2015.

  1. Johan Schoeman

    Johan Schoeman Expert Licensed User

    analogClock.png

    See the attached project. It uses a library that wraps a project that you can find here. There are files in the Objects/res/drawable and Objects/res/values folders that this project depends on (thus you need to use this project layout as base to work from). The library creates the visual analog clock (clock face and hour/minute hands). The clock is however powered/driven by a B4A timer. The library files (jar and xml) are in the /files folder of the attached project. Copy them to your additional library files folder.
     

    Attached Files:

    Last edited: Jul 8, 2015
  2. Johan Schoeman

    Johan Schoeman Expert Licensed User

    analogClockWithSecondsHand.png

    Added a hand for seconds to the analog clock. New lib files are in the /files folder of the attached project. The clock at top left should be the time of your device. The next 3 clocks to the right should be your device time minus 1 hour, device time minus 2 hours, device time minus 3 hour. The 4 clocks in the bottom row should be your device time +1, +2, +3 and +4 hours.
     

    Attached Files:

    Last edited: Jul 7, 2015
    ellpopeb4a likes this.
  3. merlin2049er

    merlin2049er Well-Known Member Licensed User

    Great. How accurate are the android timer ticks? Will you loose minutes or seconds on the hour?
     
  4. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Top left clock has been running just about spot on (almost to the second) with local ZA time as reported here. But then I guess the time of the device gets updated automatically according to NPT....? Perhaps one of the experts on this subject can shed some light on this topic.
     
  5. DonManfred

    DonManfred Expert Licensed User

    No. The maximum will be 1 second or so... Some events dont get fired directly due to the system is hard working for a moment... So the tick will fired a bit later...

    I just would use a timer of 300-500 ms and on each tick i would read the systemtime and set the time of the clock-view...
     
  6. Johan Schoeman

    Johan Schoeman Expert Licensed User

    My example reads the system time every 1000ms (hour, min, sec) and then call setTime. So it should be OK? Is the system time updated via the NPT?
     
  7. DonManfred

    DonManfred Expert Licensed User

    it think it is set one time using NPT... Maybe android does this regularly.
     
  8. Johan Schoeman

    Johan Schoeman Expert Licensed User

    analogClockWorld.png

    The new library files for the attached project are in the /files folder. Have added a click event to the clocks. When you click on a clock a spinner (with yellow text) will appear in the center of the screen. Select the city/timezone that you want the clock to display the time of. You can set the city/timezone of all 8 clocks individually. Each clock should show the current time for the city/timezone that you have selected (have compared it with local times as reported by http://www.timeanddate.com/worldclock). If no city/timezone is selected for a clock then that specific clock will show the time of your device.

    You will also need to enable/select libraries:
    1. JavaObject V2.01
    2. StringFunctions V1.05
     

    Attached Files:

    Last edited: Jul 8, 2015
  9. Troberg

    Troberg Well-Known Member Licensed User

    I would use a faster interval. Consider this scenario:

    First tick: The time is 12:03:04.999, displayed as 12:03:04
    Second tick: This tick is slightly delayed, so the time is 12:04:06.001, displayed as 12:03:06

    As you see, it will look like it jumped two seconds at once, and it was only a 2ms delay in the timer. A 2ms delay is quite possible, especially since it's good practice to disable the timer in the timer event, so that you won't get another timer event until you are done with the previous, should something unexpected happen to delay execution.

    A better solution is to fire the timer more often, say ever 100 or 200 ms, then, on each tick, check if the second has actually changed, and only redraw if it has. Will use negligible more CPU, but will update smoother.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice