HarmonyOS4.0系统性深入开发09卡片使用动效能力

卡片使用动效能力

ArkTS卡片开放了使用动画效果的能力,支持显式动画、属性动画、组件内转场能力。需要注意的是,ArkTS卡片使用动画效果时具有以下限制:

名称 参数说明 限制描述
duration 动画播放时长 限制最长的动效播放时长为1秒,当设置大于1秒的时间时,动效时长仍为1秒。
tempo 动画播放速度 卡片中禁止设置此参数,使用默认值1。
delay 动画延迟执行的时长 卡片中禁止设置此参数,使用默认值0。
iterations 动画播放次数 卡片中禁止设置此参数,使用默认值1。

以下示例代码实现了按钮旋转的动画效果:

typescript 复制代码
@Entry
@Component
struct AttrAnimationExample {
  @State rotateAngle: number = 0;

  build() {
    Column() {
      Button('change rotate angle')
        .onClick(() => {
          this.rotateAngle = 90;
        })
        .margin(50)
        .rotate({ angle: this.rotateAngle })
        .animation({
          curve: Curve.EaseOut,
          playMode: PlayMode.AlternateReverse
        })
    }.width('100%').margin({ top: 20 })
  }
}

卡片使用自定义绘制能力

ArkTS卡片开放了自定义绘制的能力,在卡片上可以通过Canvas组件创建一块画布,然后通过CanvasRenderingContext2D对象在画布上进行自定义图形的绘制,如下示例代码实现了在画布的中心绘制了一个笑脸。

typescript 复制代码
@Entry
@Component
struct Card {
  private canvasWidth: number = 0;
  private canvasHeight: number = 0;
  // 初始化CanvasRenderingContext2D和RenderingContextSettings
  private settings: RenderingContextSettings = new RenderingContextSettings(true);
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);

  build() {
    Column() {
      Row() {
        Canvas(this.context)
          .margin('5%')
          .width('90%')
          .height('90%')
          .onReady(() => {
            console.info('[ArkTSCard] onReady for canvas draw content');
            // 在onReady回调中获取画布的实际宽和高
            this.canvasWidth = this.context.width;
            this.canvasHeight = this.context.height;
            // 绘制画布的背景
            this.context.fillStyle = 'rgba(203, 154, 126, 1.00)';
            this.context.fillRect(0, 0, this.canvasWidth, this.canvasHeight);
            // 在画布的中心绘制一个红色的圆
            this.context.beginPath();
            let radius = this.context.width / 3
            let circleX = this.context.width / 2
            let circleY = this.context.height / 2
            this.context.moveTo(circleX - radius, circleY);
            this.context.arc(circleX, circleY, radius, 2 * Math.PI, 0, true);
            this.context.closePath();
            this.context.fillStyle = 'red';
            this.context.fill();
            // 绘制笑脸的左眼
            let leftR = radius / 4
            let leftX = circleX - (radius / 2)
            let leftY = circleY - (radius / 3.5)
            this.context.beginPath();
            this.context.arc(leftX, leftY, leftR, 0, Math.PI, true);
            this.context.strokeStyle = '#ffff00'
            this.context.lineWidth = 10
            this.context.stroke()
            // 绘制笑脸的右眼
            let rightR = radius / 4
            let rightX = circleX + (radius / 2)
            let rightY = circleY - (radius / 3.5)
            this.context.beginPath();
            this.context.arc(rightX, rightY, rightR, 0, Math.PI, true);
            this.context.strokeStyle = '#ffff00'
            this.context.lineWidth = 10
            this.context.stroke()
            // 绘制笑脸的嘴巴
            let mouthR = radius / 2.5
            let mouthX = circleX
            let mouthY = circleY + (radius / 3)
            this.context.beginPath();
            this.context.arc(mouthX, mouthY, mouthR, Math.PI, 0, true);
            this.context.strokeStyle = '#ffff00'
            this.context.lineWidth = 10
            this.context.stroke()
          })
      }
    }.height('100%').width('100%')
  }
}

运行效果如下图所示。

相关推荐
二流小码农2 小时前
鸿蒙开发:wrapBuilder传递参数
android·ios·harmonyos
别说我什么都不会2 小时前
鸿蒙(HarmonyOS)性能优化实战-应用性能分析工具CPU Profiler使用指南
性能优化·harmonyos
png3 小时前
从零开始纯血鸿蒙天气预报-主界面(1)
harmonyos·arkui
城中的雾3 小时前
鸿蒙开发者必看:如何用一行命令搞定HSP/HAP文件安装?
harmonyos
23zhgjx-zgx3 小时前
OSPF:虚链路
网络·tcp/ip·华为·智能路由器·ensp
Georgewu5 小时前
【HarmonyOS Next】鸿蒙应用弹框和提示气泡详解(一)
前端·华为·harmonyos
今阳5 小时前
鸿蒙开发笔记-11-LazyForEach 数据懒加载
android·华为·harmonyos
爱宇阳5 小时前
如何在 Windows 10 启用卓越性能模式及不同电源计划对比
windows·卓越模式·电池选项
坚果的博客6 小时前
鸿蒙版Flutter快递查询助手
flutter·华为·harmonyos
轻口味6 小时前
【每日学点HarmonyOS Next知识】状态栏控制、片段按钮点击回调、绘制组件、取消按钮与输入框对齐、父调子组件方法
pytorch·华为·harmonyos·harmonyosnext