You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按比例旋转图片,实现正确的时钟指针旋转效果?

时钟指针旋转效果修正问题

我想要旋转一张代表时钟指针的长图片,当前使用的代码如下:

val hand = Image(handBitmap).apply {
    scaledHeight = 50.0
    scaledWidth = 400.0
    anchor(.0, 0.5)
    addUpdater {
        rotation =  Angle.fromDegrees( rotation.degrees + 1)
    }
}

效果对比

  • 预期效果:时钟指针以靠近时钟圆心的端点为旋转轴,像真实时钟指针一样绕圆心做圆周旋转。
  • 实际效果:指针旋转时整体偏移,未以时钟圆心为轴转动,无法呈现正常的时钟指针效果。

修改方案

  1. 对齐旋转锚点与时钟圆心
    当前anchor(.0, 0.5)已经将旋转锚点设置在图片的左边缘垂直中点(符合时钟指针根部的位置),但缺少将该锚点定位到时钟圆心的步骤。需要添加位置设置,将锚点对齐到时钟圆心坐标:

    val hand = Image(handBitmap).apply {
        scaledHeight = 50.0
        scaledWidth = 400.0
        anchor(.0, 0.5)
        // 将锚点(左边缘中点)定位到时钟圆心,示例坐标为(200.0, 200.0),请替换为实际圆心坐标
        position = Point(200.0, 200.0)
        addUpdater {
            rotation = Angle.fromDegrees(rotation.degrees + 1)
        }
    }
    
  2. 优化旋转逻辑(可选)
    原代码中每次累加角度的方式容易产生误差,建议基于时间计算精准角度,模拟真实时钟的转动:

    addUpdater {
        // 以秒针为例,每秒转动6度(360度/60秒)
        val currentMillis = System.currentTimeMillis()
        val seconds = (currentMillis / 1000) % 60
        rotation = Angle.fromDegrees(seconds * 6.0)
    }
    

内容的提问来源于stack exchange,提问作者aQuu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 08:15:35