iT邦幫忙

2023 iThome 鐵人賽

DAY 29
0
SideProject30

拿到一支Garmin錶,要怎麼開發app?系列 第 29

[Day-29] 開發新的錶面-10

  • 分享至 

  • xImage
  •  

延續昨天的動作,再到MyFirstWatchFaceView.mc
增加以下程式碼,把圖片load到View試試看,

   var myJpg1 = null;
    var myJpg2 = null;
    var myJpg3 = null;
    var myJpg4 = null;
    var myJpg5 = null;
    var myJpg6 = null;
    var myJpg7 = null;
    var myJpg8 = null;
    function onLayout(dc as Dc) as Void {
        myfonts=WatchUi.loadResource(Rez.Fonts.myfonts);
        myfontsSmall=WatchUi.loadResource(Rez.Fonts.myfonts_small);
        myJpg1=WatchUi.loadResource(Rez.Drawables.clown1);
        myJpg2=WatchUi.loadResource(Rez.Drawables.clown2);
        myJpg3=WatchUi.loadResource(Rez.Drawables.clown3);
        myJpg4=WatchUi.loadResource(Rez.Drawables.clown4);
        myJpg5=WatchUi.loadResource(Rez.Drawables.clown5);
        myJpg6=WatchUi.loadResource(Rez.Drawables.clown6);
        myJpg7=WatchUi.loadResource(Rez.Drawables.clown7);
        myJpg8=WatchUi.loadResource(Rez.Drawables.clown8);
    }

沒想到竟然發生Out of Memory
https://ithelp.ithome.com.tw/upload/images/20231014/20128973Smg4qexMVb.png
從debug console可以看到以下訊息:

Error: Out Of Memory Error
Details: Failed invoking
Stack:

  • onLayout() at D:\2023ironman_MyFirstWatchFace\source\MyFirstWatchFaceView.mc:34 0x10000190

Encountered app crash.

可以在模擬器選單選擇 File > View Memory,
https://ithelp.ithome.com.tw/upload/images/20231014/20128973kfhFeYRNQQ.png
開啟介面觀察是哪個部分是用memory用得那麼兇

https://ithelp.ithome.com.tw/upload/images/20231014/20128973konsPGl5GB.png
發現圖片明明都不到5kb,卻用memory用得很兇...

參考這篇https://developer.garmin.com/connect-iq/connect-iq-faq/how-do-i-optimize-bitmaps/
將drawables.xml改成

<drawables>
    <bitmap id="LauncherIcon" filename="launcher_icon.png" /bitmap>
    <bitmap id="clown1" filename="clown/he-0016.jpg">
        <palette disableTransparency="true">
            <color>FFFFFF</color>
            <color>AAAAAA</color>
            <color>555555</color>
            <color>000000</color>
        </palette>
    </bitmap>
    <bitmap id="clown2" filename="clown/he-0018.jpg">
        <palette disableTransparency="true">
            <color>FFFFFF</color>
            <color>AAAAAA</color>
            <color>555555</color>
            <color>000000</color>
        </palette>
    </bitmap>
    <bitmap id="clown3" filename="clown/he-0020.jpg">
        <palette disableTransparency="true">
            <color>FFFFFF</color>
            <color>AAAAAA</color>
            <color>555555</color>
            <color>000000</color>
        </palette>
    </bitmap>
    <bitmap id="clown4" filename="clown/he-0020.jpg">
        <palette disableTransparency="true">
            <color>FFFFFF</color>
            <color>AAAAAA</color>
            <color>555555</color>
            <color>000000</color>
        </palette>
    </bitmap>
    <bitmap id="clown5" filename="clown/he-0025.jpg">
        <palette disableTransparency="true">
            <color>FFFFFF</color>
            <color>AAAAAA</color>
            <color>555555</color>
            <color>000000</color>
        </palette>
    </bitmap>
    <bitmap id="clown6" filename="clown/he-0027.jpg">
        <palette disableTransparency="true">
            <color>FFFFFF</color>
            <color>AAAAAA</color>
            <color>555555</color>
            <color>000000</color>
        </palette>
    </bitmap>
    <bitmap id="clown7" filename="clown/he-0029.jpg">
        <palette disableTransparency="true">
            <color>FFFFFF</color>
            <color>AAAAAA</color>
            <color>555555</color>
            <color>000000</color>
        </palette>
    </bitmap>
    <bitmap id="clown8" filename="clown/he-0033.jpg">
        <palette disableTransparency="true">
            <color>FFFFFF</color>
            <color>AAAAAA</color>
            <color>555555</color>
            <color>000000</color>
        </palette>
    </bitmap>
</drawables>

然後也把圖片改成黑白,

https://ithelp.ithome.com.tw/upload/images/20231014/20128973JQTmCGuVPp.png
勉強壓線過關

接下來就簡單寫一段switch case,
在整點的時候開始切換圖片,
反覆嘗試之後,我決定切4張圖就好

        //draw clown
        if(currentMin.equals("00")){
            switch (currentSec) {
            case 1:
                dc.clear();
                dc.drawBitmap((dc.getWidth()-myJpg1.getWidth())/2, (dc.getHeight()-myJpg1.getHeight())/2, myJpg1);
                break;
            case 2:
                dc.clear();
                dc.drawBitmap((dc.getWidth()-myJpg1.getWidth())/2, (dc.getHeight()-myJpg1.getHeight())/2, myJpg2);
                break;
            case 3:
                dc.clear();
                dc.drawBitmap((dc.getWidth()-myJpg1.getWidth())/2, (dc.getHeight()-myJpg1.getHeight())/2, myJpg3);
                break;
            case 4:
                dc.clear();
                dc.drawBitmap((dc.getWidth()-myJpg1.getWidth())/2, (dc.getHeight()-myJpg1.getHeight())/2, myJpg4);
                break;
            case 5:
                dc.clear();
                dc.drawBitmap((dc.getWidth()-myJpg1.getWidth())/2, (dc.getHeight()-myJpg1.getHeight())/2, myJpg4);
                break;
            default:
                break;
            }
        }

效果如下:


上一篇
[Day-28] 開發新的錶面-9
下一篇
[Day-30] 將我的錶面上架到Connect IQ
系列文
拿到一支Garmin錶,要怎麼開發app?30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言