Hi,
I was trying to draw a dynamic rendered battery.
I saw a function from crystal-watch-face. It does works on some devices. But on fenix6xpro and fenix7x which both have 280*280 resolution, it displays quite differently. On f7x, the rectangle will have some pixels drifting.
code is here. You can just run it in a watch face project.
Maybe there is something to do with pixel density? So weird
function drawBatteryMeter(dc) {
var width = 32;
var height = 16;
var x = 140;
var y = 140;
var BATTERY_HEAD_HEIGHT = 4;
var BATTERY_MARGIN = 1;
dc.setColor(0xffffff, Graphics.COLOR_TRANSPARENT);
dc.setPenWidth(/* BATTERY_LINE_WIDTH */ 2);
// Body.
// drawRoundedRectangle's x and y are top-left corner of middle of stroke.
// Bottom-right corner of middle of stroke will be (x + width - 1, y + height - 1).
dc.drawRoundedRectangle(
x - width / 2 + /* (BATTERY_LINE_WIDTH / 2) */ 1,
y - height / 2 + /* (BATTERY_LINE_WIDTH / 2) */ 1,
width - /* BATTERY_LINE_WIDTH + 1 */ 1,
height - /* BATTERY_LINE_WIDTH + 1 */ 1,
/* BATTERY_CORNER_RADIUS */ 2 * layout[14]
);
// Head.
// fillRectangle() works as expected.
dc.fillRectangle(
x + width / 2 + BATTERY_MARGIN,
y - BATTERY_HEAD_HEIGHT / 2,
/* BATTERY_HEAD_WIDTH */ 2,
BATTERY_HEAD_HEIGHT
);
// Fill.
// #8: battery returned as float. Use floor() to match native. Must match getValueForFieldType().
var batteryLevel = Math.floor(Sys.getSystemStats().battery);
dc.setColor(Graphics.COLOR_GREEN, Graphics.COLOR_TRANSPARENT);
var lineWidthPlusMargin = /* BATTERY_LINE_WIDTH */ 2 + BATTERY_MARGIN;
var fillWidth = width - 2 * lineWidthPlusMargin;
dc.fillRectangle(
x - width / 2 + lineWidthPlusMargin,
y - height / 2 + lineWidthPlusMargin,
Math.ceil(fillWidth * (batteryLevel / 100)),
height - 2 * lineWidthPlusMargin
);
}