Download and display a PNG icon

Hi all,

I would like to download and display a small PNG image, as this is dynamic it cannot be included in the resource file. Downloading the file is relatively easy, but I have not found an example of how to display an image or if this is even possible?

Thanks in advance,

Chris
  • The documentation for Comm.makeImageRequest() says that the responseCallback parameter will receive a WatchUi.BitmapResource as the second parameter on success. A BitmapResource is the same thing as is returned from Ui.loadResource(), so anything that you can do with that object, you can do with the one you get in the callback.

    When you want to draw a bitmap, you typically call Dc.drawBitmap(), which is documented to take a BitmapResource as a parameter. So, you could do something like this (note, untested code below)...

    using Toybox.WatchUi as Ui;
    using Toybox.System as Sys;
    using Toybox.Communications as Comm;

    class TestDelegate extends Ui.BehaviorDelegate
    {
    hidden var _view;

    function initialize(view) {
    BehaviorDelegate.initialize();
    _view = view;
    }

    function onSelect() {

    var deviceSettings = Sys.getDeviceSettings();
    if (!deviceSettings.phoneConnected) {
    _view.setString("Connect Phone; Try again.");
    }
    else {

    var url = "upload.wikimedia.org/.../LogoLiveCloud.png";

    var params = {
    };

    var options = {
    :palette => [
    Gfx.COLOR_WHITE,
    Gfx.COLOR_GREEN,
    Gfx.COLOR_DK_GREEN,
    Gfx.COLOR_LT_GRAY,
    Gfx.COLOR_DK_GRAY,
    Gfx.COLOR_BLACK
    ],
    :maxWidth => 127,
    :maxHeight => 127,
    :dithering => Comm.IMAGE_DITHERING_NONE
    };

    _view.setString("Requesting..");

    Comm.makeImageRequest(url, params, options, self.method(:onImageResponse));
    }

    return true;
    }

    function onImageResponse(code, bitmap) {

    if (code != 200) {
    _view.setBitmap(bitmap);
    }
    else {
    _view.setString("Failed.");
    }
    }
    }

    class TestView extend Ui.View
    {
    function initialize() {
    View.initialize();
    }

    hidden var _string;
    hidden var _bitmap;

    function onLayout(dc) {
    var deviceSettings = Sys.getDeviceSettings();

    if (!deviceSettings.phoneConnected) {
    _string = "Connect Phone";
    }
    else if (deviceSettings.isTouchScreen) {
    _string = "Tap Screen";
    }
    else {
    _string = "Press Enter/Start";
    }
    }

    function setBitmap(bitmap) {
    _bitmap = bitmap;
    _string = null;
    Ui.requestUpdate();
    }

    function setString(string) {
    _string = string;
    _bitmap = null;
    Ui.requestUpdate();
    }

    function onUpdate(dc) {
    dc.setColor(Gfx.COLOR_BLACK, Gfx.COLOR_BLACK);
    dc.clear();

    var cx = dc.getWidth() / 2;
    var cy = dc.getHeight() / 2;


    if (_bitmap != null) {
    cx -= _bitmap.getWidth() / 2;
    cy -= _bitmap.getHeight() / 2;

    dc.drawBitmap(cx, cy, _bitmap);
    }
    else (_string != null) {
    dc.drawText(cx, cy, Gfx.FONT_SMALL,
    _string,
    Gfx.TEXT_JUSTIFY_CENTER | Gfx.TEXT_JUSTIFY_VCENTER);
    }
    }
    }


    Travis
  • Thank you!

    Hi Travis,

    Thank you for the detailed response, I obviously need to read through the documentation more.

    I will let you know how I get on in due course.

    Best regards,

    Chris