Drawing a straight line in layouts.xml

In my first attempt at a data field, I used direct dc calls to render my whole UI, and although that worked, it was ugly code and not easy to support multiple devices. I think that approach was also part of the reason I was struggling to stay even under the 28.6KB memory limit, nevermind the 16KB limit, so I'm going back to the drawing board and this time intending to use layouts. I was surprised to see there is no documented way of defining a straight line in the layouts.xml file - I can do circles, arcs, rectangles etc. but not lines. I don't really want a hybrid where I use dc.drawLine in combination with the layout, but the only other way I can think to do it is to define a rectangle in the layout with a height (or width) of 1px. Is that sensible, or have I missed something obvious here?

  • Not sure about that but if your aim is to reduce your memory footprint, using layouts is NOT the way... It will increase memory usage instead of decreasing it.

    I went from direct drawing to layouts for my Forerunner 645 watchface, only to see memory usage increasing and users complaining the watchface became slower. Going back to direct drawing fixed these issues...
  • As FlipStone said, if your goal is to reduce memory, layouts probably won't help and may increase it. That was one of the tricks when all DFs were only 16k - remove the layouts as you can save some memory.

    If your goal is to clean up the code, maybe it's more a case of how you do your code. For example, do the basic setup for stuff in onLayout(), and remove all checks for a device/DF size/screen location from onUpdate.

    In onLayout, figure out the x/y of things, maybe set flags for something that may or may not be displayed in the current DF, etc, then in onUpdate, just let it use those values, and don't do any extra checks there. Doing all the checks you can in onLayout() should also make the code more efficient, as on a device, onLayout() isn't called that often, unlike onUpdate() that's called every second when the DF is visible.
  • Thanks guys, that's really interesting. I've never worked in an environment like this where memory management is such a problem (my usual development target is a web server with 16GB of RAM).

    @jim: I was just looking to see where I got the idea that I should switch to layouts instead of direct drawing, and it turns out it was you, although I had completely misread what you had said in that recent thread about preserving memory :)

    I'll stick with dc calls in that case. I'll also start using onLayout() more - I was pretty much doing everything drawing-related in onUpdate() before and whilst I hadn't observed any performance issues, I had overshot the 28.6KB limit so something definitely needed to change. I just hadn't been paying that much attention to how I was using memory.