文档开发者中心
我的
|

ArkUI (方舟UI框架)

ArkWeb

HarmonyOS 5

ArkTS

Ability (程序框架)

HarmonyOS 两个叠加形状组件做切换动画,如何实现绕中心旋转切换?(API12+)

将方形rect改为圆形,修改了borderRadius,并修改其大小、增加旋转。整体使用动画animation动画过渡。过渡效果与预期不一致。

预期结果:中心点不变进行旋转、大小、方形变圆形的动画过渡。

页面代码:

收起
自动换行
深色代码主题
复制
import { matrix4 } from '@kit.ArkUI';

@Entry
@Component
export default struct Riders {
  @State opacityValue: number = 1
  @State recording: boolean = false
  build() {
    Column() {
      Button() {
        Stack({ alignContent: Alignment.Center }) {
          // 外环(动态颜色)
          Circle()
            .width(40)
            .height(40)
            .stroke(this.recording ? Color.Red : '#6964FF')
            .strokeWidth(1.5)
            .fill(Color.Transparent)

          // 内部形(动态切换)
          Rect()
            .width(this.recording ? 18 : 28)
            .height(this.recording ? 18 : 28)
            .radius(this.recording ? 6 : 14)
            .fill(this.recording ? Color.Red : '#6964FF')
            .opacity(this.opacityValue)
            .rotate({
              angle: this.recording ? 90 : 0,
              centerX: (this.recording ? 18 : 28) / 2,
              centerY: (this.recording ? 18 : 28) / 2
            })// 旋转角度
            .animation({
              duration: 500,
              curve: Curve.EaseOut,
            })
        }
        .width('100%')
        .height('100%')
      }
      .width(50)
      .height(50)
      .backgroundColor(Color.Transparent)
      .onClick(() => {
        try {
          this.recording = !this.recording
        } catch (e) {
          console.error(JSON.stringify(e), "createFloatWindowERROR")
        }
      })
    }
    .width('100%')
    .height('100%')
    .backgroundColor(Color.Black)
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }
}
点赞
收藏
回复
1
分享
举报
浏览48 发布于2026-01-05 09:31未知归属地
全部评论
最多点赞
最新发布
最早发布
|
技术支持

【背景知识】

图形变换用于对组件进行旋转、平移、缩放、矩阵变换等操作。

【解决方案】

为达到丝滑的切换效果,建议通过Stack叠加两个组件,然后结合形状组件绕z轴旋转(默认参数)一定角度并结合scale的缩放效果,达到完美的切换。

代码如下:

收起
自动换行
深色代码主题
复制
@Entry
@Component
export default struct Riders {
  @State recording: boolean = false
  @State angleSize: number = 0
  @State opacityValue: number = 1
  @State scaleSize: number = 1
  @State z: number = 1

  build() {
    Column() {
      Button() {
        Stack({ alignContent: Alignment.Center }) {
          // 外环(动态颜色)
          Circle()
            .width(40)
            .height(40)
            .stroke(this.recording ? Color.Red : '#6964FF')
            .strokeWidth(1.5)
            .fill(Color.Transparent)

          // 内部形(动态切换)
          Rect()
            .width(18)
            .height(18)
            .radius(6)
            .fill(Color.Red)
            .rotate({ angle: this.angleSize })
            .opacity(1 - this.opacityValue)

          Rect()
            .width(28)
            .height(28)
            .radius(14)
            .fill('#6964FF')
            .rotate({ z: 1, angle: this.angleSize })
            .scale({ x: this.scaleSize, y: this.scaleSize })
            .opacity(this.opacityValue)
        }
        .width('100%')
        .height('100%')
      }
      .width(50)
      .height(50)
      .backgroundColor(Color.Transparent)
      .onClick(() => {
        this.recording = !this.recording
        this.getUIContext().animateTo({
          duration: 250,
          curve: Curve.Linear
        }, () => {
          if (this.recording) {
            this.opacityValue = 0
            this.angleSize = 90
            this.scaleSize = 0.5
          } else {
            this.opacityValue = 1
            this.angleSize = 0
            this.scaleSize = 1
          }
        })
      })
    }
    .width('100%')
    .height('100%')
    .backgroundColor(Color.Black)
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }
}
展开全部
1楼回复于2026-01-05 09:31 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。