Watch Face - Multiple Background Images

I thought it'd be cool to be able to have different background images based on things like "time of day."

So, I wrote a simple watch face app and ran into a snag. I think I used up too much memory when loading resources.
I've been using Ui.loadResource() to load just two images in the onLayout function.

What's odd is it works in the simulator, but not on my vivoactive watch.
By removing the loading and use of the second background image, everything runs fine. Either image, so it's not the image itself causing grief.
Is there a way to unload a resource, then add another from within the program? The total size of the app was about 121k.

Just FYI, I'm rather new to this.
Thanks!

The coded goes something like this:
class garminsimplewatchfaceView extends Ui.WatchFace {

var picOne;
var picTwo;

//! Load your resources here
function onLayout(dc) {
picOne = Ui.loadResource(Rez.Drawables.id_picOne);
picTwo = Ui.loadResource(Rez.Drawables.id_picTwo);
}

//! Restore the state of the app and prepare the view to be shown
function onShow() {
}

//! Update the view
function onUpdate(dc) {

// Get date/time strings
var moment = Time.now();

if (info.min % 2 == 0) {
dc.drawBitmap(bgX, bgY, picOne);
}else{
dc.drawBitmap(bgX, bgY, picTwo);
}
  • Former Member
    Former Member over 10 years ago
    If you're only wanting to display one image at any given time I think you have the right idea to only load one into memory at a time. I think you could include a check at the beginning of onUpdate() to see if you need to load a different image.

    function onUpdate(dc) {
    checkLoadedImage();
    dc.drawBitmap(bgX, bgY, pic);
    }

    function checkLoadedImage() {
    if ( isDaytime && !daytimeImageLoaded ) {
    pic = null; // This is included to show that memory is being cleared. You don't actually need it.
    pic = Ui.loadResource(Rez.Drawables.day_image);
    daytimeImageLoaded = true;
    } else if ( !isDaytime && daytimeImageLoaded ) {
    pic = null; // This is included to show that memory is being cleared. You don't actually need it.
    pic = Ui.loadResource(Rez.Drawables.night_image);
    daytimeImageLoaded = false;
    }
    }
  • Awesome, and thanks!
    It works!

    The key is pic = null;
    Or more specifically, reassigning the same "pic" variable to the various pictures.
    From what I gather, each time "pic" is assigned a new picture, the previous one is cleared from memory.