animation 动画如何应用在canvas 内容里面?

在文档里面看到动画是可以用在canvas 内容里面的,但是没有找到对应实现的demo。想问一下如何实现。

HarmonyOS
2024-10-11 11:29:03
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

animation 动画应用在canvas 内容里面可以参考这个demo:

import curves from '@ohos.curves'  
  
@Entry  
@Component  
struct AnimationDemo {  
  @State animate: boolean = false;  
  // 第一步: 声明相关状态变量  
  @State rotateValue: number = 0; // 组件一旋转角度  
  @State translateX: number = 0; // 组件二偏移量  
  @State opacityValue: number = 1; // 组件二透明度  
  
  private settings: RenderingContextSettings = new RenderingContextSettings(true)  
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)  
  
  
  // 第二步:将状态变量设置到相关可动画属性接口  
  build() {  
    Row() {  
      // 组件一  
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {  
        Canvas(this.context)  
          .onReady(() => {  
            this.context.fillRect(0, 0, 100, 100)  
          })  
      }  
      .opacity(this.opacityValue)  
      .rotate({ angle: this.rotateValue })  
      // 第三步:通过属性动画接口开启属性动画  
      .animation({ curve: curves.springMotion() })  
      .backgroundColor('#317AF7')  
      .width(100)  
      .height(100)  
      .borderRadius(30)  
      .onClick(() => {  
        this.animate = !this.animate;  
        // 第四步:闭包内通过状态变量改变UI界面  
        // 这里可以写任何能改变UI的逻辑比如数组添加,显隐控制,系统会检测改变后的UI界面与之前的UI界面的差异,对有差异的部分添加动画  
        // 组件一的rotate属性发生变化,所以会给组件一添加rotate旋转动画  
        this.rotateValue = this.animate ? 90 : 0;  
        // 组件二的offset属性发生变化,所以会给组件二添加offset偏移动画  
        this.translateX = this.animate ? 50 : 0;  
        // 父组件column的opacity属性有变化,会导致其子节点的透明度也变化,所以这里会给column和其子节点的透明度属性都加动画  
        this.opacityValue = this.animate ? 0.6 : 1;  
      })  
  
      // 组件二  
      Column() {  
      }  
      .justifyContent(FlexAlign.Center)  
      .width(100)  
      .height(100)  
      .backgroundColor('#D94838')  
      .borderRadius(30)  
      .opacity(this.opacityValue)  
      .translate({ x: this.translateX })  
      .animation({ curve: curves.springMotion() })  
    }  
    .width('100%')  
    .height('100%')  
    .justifyContent(FlexAlign.Center)  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
分享
微博
QQ
微信
回复
2024-10-11 17:32:26
相关问题
HarmonyOS 应用在投屏期间如何保活
984浏览 • 1回复 待解决
HarmonyOS 如何保证应用在后台不被挂起
1220浏览 • 1回复 待解决
应用在CPU的占用情况如何线上分析
2362浏览 • 1回复 待解决
应用在后台时发送请求失败问题
1465浏览 • 1回复 待解决
HarmonyOS animateTo或animation动画如何取消
1174浏览 • 1回复 待解决
HarmonyOS animation动画无法生效
754浏览 • 1回复 待解决
基础类型通知主要应用在哪些方面?
1161浏览 • 1回复 待解决
HarmonyOS animation动画停止位置不对
710浏览 • 1回复 待解决