Midterm: Moon

Get the code: GitHub

My midterm Android application is Moon, an app that shows the moon phase for any date.


I have been wanting to explore the phases of the moon for another potential project. The phases are extremely regular and could be used to control something when the moon is treated like a meter oscillating between 0 (dark) and 1 (light). This app is meant to be just for fun but it also allowed me to take the first step towards this and other possible projects involving the moon, sun, and other natural bodies which have consistent cycles.


Moon is a simplification of the many moon phase Android apps (Moon Phases Lite, Simple Moon Phase Widget, Phases of the Moon Free, etc.) which feature realistic and/or cheesy graphics. I am not against these apps but I wanted to make something pure and simple that really lets you focus on the cycle.  Moon is no better or worse than the other available apps - it definitely has less functionality, but I will say that it does one thing and it does it well. And the reduced functionality allowed me to focus on smaller, but no less important, UI design features.

How I did it

The most important part of the app is the data that drives the moon phases for a given date. This data is available in tabular form but I wanted to do the calculations programmatically. I tried three different code sources, the first being a PHP snippet that I translated to Java. It turned out that the calculations were wrong, so in the end I found MoonPhase, a J2ME program that calculates and displays moon phases on older mobile phones.  From this I used only the Java class which did the math.

Keeping simplicity in mind, I opted for a single Activity. I then created a custom View that draws the graphics of the phases based on the calculations from MoonPhase. In the custom View I developed a way to form accurate representations of moon phases using simple shapes. This can be seen below, with the shapes given colors to let them stand out.

There are only four cases needed to draw all possible moon phases. In each the color and location of the oval and rectangle are switched to make it appear that the moon face is seamless. In this case, the rectangle is black and the oval is white, but this changes throughout the cycle.

Once able draw the moon phase for a given date, I added a few TextViews and Buttons to step back and forth between dates. I also incorporated an instance of DatePickerDialog for easy date selection, so that tapping the date lets you select any date you want.

I spent extra time on the arrow buttons and enabled them to handle a press-and-hold rather than just a single touch. This lets you more quickly increment/decrement the date. A very useful Stack Overflow thread explains this feature.

What works and what doesn't

Technically there isn't anything that doesn't work, but there is at least one thing that could be improved on. The MoonPhase calculations are so accurate that you can get the exact phase of the moon down to the millisecond, which means that my app greatly diminishes the potential accuracy of what's going on underneath. An enhancement would be to update the moon phase in real time (or closer to real time) when the current date is set. Also it would be nice to accommodate users in the Southern Hemisphere, since this only displays the moon as we see it here in the North.

I don't know what the future holds for Moon but I may attempt to use it as a UI for throttling bandwidth based on the percent of the moon illuminated. Other than that, it was a great exploration of some new Android programming conventions and features.

No comments:

Post a Comment

Speak now...