Android Example Another Analog Clock

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.
 

Attachments

  • AnalogClockB4A.zip
    125.8 KB · Views: 1,003
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime 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.
 

Attachments

  • AnalogClockB4AV1.zip
    127.3 KB · Views: 828
Last edited:

merlin2049er

Well-Known Member
Licensed User
Longtime User
Great. How accurate are the android timer ticks? Will you loose minutes or seconds on the hour?
 

Johan Schoeman

Expert
Licensed User
Longtime 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.
 

DonManfred

Expert
Licensed User
Longtime User
Will you loose minutes or seconds on the hour?
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...
 

Johan Schoeman

Expert
Licensed User
Longtime 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...
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?
 

Johan Schoeman

Expert
Licensed User
Longtime 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
 

Attachments

  • AnalogClockB4AV2.zip
    130.4 KB · Views: 820
Last edited:

Troberg

Well-Known Member
Licensed User
Longtime 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?

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.
 
Top