tomasslavicek

Efficient drawing of the background bitmap Garmin Connect IQ

Nov 12th, 2018
209
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     <bitmap id="bg1" filename="bg1.png" dithering="none">
  3.         <palette disableTransparency="true">
  4.             <color>ffffff</color><color>aaaaaa</color><color>000000</color>
  5.             <color>000055</color><color>0000aa</color><color>0000ff</color>
  6.             <color>ffff00</color><color>ffff55</color><color>ffffaa</color><color>aaaa00</color>
  7.         </palette>
  8.     </bitmap>    
  9. */
  10.  
  11. var backgroundBitmap;
  12.  
  13. function onLayout(dc) {
  14.     backgroundBitmap = null;
  15.     backgroundBitmap = new Gfx.BufferedBitmap({
  16.         :width => dc.getWidth(),
  17.         :height => dc.getHeight(),
  18.         :palette => [0xffffff, 0xaaaaaa, 0x555555, 0x000000, 0x000055, 0x0000aa, 0x0000ff, 0xffff00, 0xffff55, 0xffffaa, 0xaaaa00]
  19.     });
  20. }
  21.  
  22. function draw(dc) {                    
  23.     if (redraw) {    
  24.         // Full redraw, 1x per minute (or when he is looking on his watch)
  25.         var dc2 = backgroundBitmap.getDc();
  26.         bgBitmap = Ui.loadResource(bmp1); // Bitmap 120x120 px
  27.         dc2.drawBitmap(0, 0, bgBitmap);
  28.         bgBitmap = null;
  29.         bgBitmap = Ui.loadResource(bmp2);
  30.         dc2.drawBitmap(120, 0, bgBitmap);
  31.         bgBitmap = null;
  32.         bgBitmap = Ui.loadResource(bmp3);
  33.         dc2.drawBitmap(0, 120, bgBitmap);
  34.         bgBitmap = null;
  35.         bgBitmap = Ui.loadResource(bmp4);
  36.         dc2.drawBitmap(120, 120, bgBitmap);
  37.         bgBitmap = null;
  38.         redraw = false;
  39.     }
  40.     dc.drawBitmap(0, 0, backgroundBitmap);
  41. }
RAW Paste Data