android: fix inverted device-orientation mapping (real accelerometer reads world-up; emulator uses gravity convention)

This commit is contained in:
ZXCLI
2026-09-07 19:09:57 +08:00
parent b1a3c38a69
commit fabea4c664
8 changed files with 41 additions and 5 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -13,8 +13,15 @@ import kotlinx.coroutines.flow.StateFlow
* Physical orientation of the phone relative to its portrait grip, from the
* accelerometer. [deg] is the CLOCKWISE rotation of the device as seen by
* the user: 0 = upright portrait, 90 = turned clockwise (portrait top edge
* points to the user's right, gravity along +X device), 180 = upside down,
* 270 = turned counter-clockwise.
* points to the user's right, sensor reads -g on X), 180 = upside down
* (sensor reads -g on Y), 270 = turned counter-clockwise (sensor reads +g
* on X).
*
* Android accelerometer convention (REAL devices): at rest the reading is
* "acceleration minus gravity", i.e. it points to world UP in device coords
* — flat on a table screen-up -> z=+9.81, upright portrait -> y=+9.81.
* (Note the emulator's virtual sensor uses the opposite, gravity-vector
* convention; do not "fix" this mapping to match emulator defaults.)
*
* The activity is portrait-locked (composition glued to the phone frame, so
* the thermal image region always matches the lens direction). UI layers use
@@ -45,11 +52,12 @@ object DeviceOrientation : SensorEventListener {
override fun onSensorChanged(event: SensorEvent) {
val gx = event.values[0]
val gy = event.values[1]
// world-up in device coords: (0,+g)=0 (-g,0)=90 (0,-g)=180 (+g,0)=270.
// hysteresis: only switch pose when the dominant axis clearly wins,
// so ~45 deg in-between holds keep the previous reading
val next = when {
abs(gx) > abs(gy) + 2.5f -> if (gx > 0) 90 else 270
abs(gy) > abs(gx) + 2.5f -> if (gy < 0) 0 else 180
abs(gx) > abs(gy) + 2.5f -> if (gx < 0) 90 else 270
abs(gy) > abs(gx) + 2.5f -> if (gy > 0) 0 else 180
else -> _deg.value
}
if (next != _deg.value) _deg.value = next
Binary file not shown.
+4
View File
@@ -111,6 +111,10 @@ res/drawable/*.xml 自绘矢量图标(双弧圆等可靠几何图形
Display.rotation 恒 0 不可用)。顶栏/底部导航条目 `graphicsLayer rotationZ=-φ`
原位预旋转;渲染器 OSD 文字 `canvas.rotate(-φ)` 绕锚点旋转(标记圆点、色标条
几何仍钉死在图像上)。对话框与其他页签暂不补偿。
- **加速度计符号约定(第七轮教训,勿改回)**:真机 TYPE_ACCELEROMETER 静止读数
指向世界上方(竖屏正持 y=+9.81);模拟器虚拟传感器是反的重力约定(y=-9.81)。
DeviceOrientation 映射按**真机约定**写,模拟器测试须用反号值驱动
(φ=0→`adb emu sensor set acceleration 0:9.81:0`)。
- **图像恒 90°CW 绘制为 3:4 竖向**,填满可用区域(顶栏下~底导航上)。
- **文字/图标恒屏幕水平**(可读);标记文字位置自动跟随(probeToScreen 固定 90° 映射:
`fx=1-sy/120, fy=sx/160`)。
+12
View File
@@ -154,6 +154,18 @@
- [x] 单元测试全过;APK 已更新。
- 注:对话框(调色板等)与其他页签内容未做补偿旋转(保持竖屏可读),如需再加。
## 用户反馈修复 第七轮(2026-09-07,真机朝向全反修复)
- [x] **用户反馈**:真机正持竖屏时屏幕内容倒立,其他方向也全反。
- [x] **根因**:第六轮把加速度计符号约定写反。Android 真机 TYPE_ACCELEROMETER
静止读数 = "加速度减重力",指向世界上方向(平放屏幕朝上 z=+9.81、竖屏正持
y=+9.81);模拟器虚拟传感器却是重力向量约定(正持 y=-9.81),故模拟器测试
"通过"而真机全反(所有姿态差 180°)。
- [x] **修复**DeviceOrientation 映射改为 (0,+g)=0、(-g,0)=90、(0,-g)=180、(+g,0)=270
代码注释明确标注"勿按模拟器默认值改回"。模拟器用反号值复测:姿态 0 恢复正常
正持显示,90/180/270 补偿几何不变(preview_fix_pose*_v7.png)。
- [x] 单元测试全过;APK 已更新。
## 待办
- 真机USB实测(温度绝对值标定、FFC/录像/MDT保存端到端)