鸿蒙开发之帧动画ImageAnimator

鸿蒙帧动画需要借助组件ImageAnimator。使用的时候想要让动画动起来需要设置state为AnimationStatus.Running

@Entry
@Component
struct OfficialImageAnimatorPage {
  build() {
      Column() {
        ImageAnimator()
          //动画数组
          .images([
            {src:$r('app.media.icon')},
            {src:$rawfile('111.png')},
            {src:$rawfile('222.png')},
            {src:$rawfile('333.png')},
          ])
          //动画时长
          .duration(2000)
          .margin({top:30})
          .backgroundColor(Color.Green)
          //开始
          .state(AnimationStatus.Running)
          //正序还是倒序
          .reverse(false)
          .fillMode(FillMode.Forwards)
          //重复次数,-1为无限
          .iterations(-1)
          .width(340)
          .height(240)
          //动画开始回调函数
          .onStart(() => {
            console.info(' ImageAnimator onStart')
          })
          .onPause(() => {
            console.info('ImageAnimator onPause')
          })
          .onRepeat(() => {
            console.info('ImageAnimator onRepeat')
          })
          .onCancel(() => {
            console.info('ImageAnimator onCancel')
          })
          .onFinish(() => {
            console.info('ImageAnimator onFinish')
          })
      }
      .width('100%')
      .height('100%')
  }
}

如果,帧动画的图片大小不一样,并且间隔时间也不同。就需要设置参数fixedSize(false)。但是,需要注意的是,设置fixedSize为false后,需要设置每个图片的大小,当然也可以搭配上动画时长,如下

@Entry
@Component
struct OfficialImageAnimatorPage {
  build() {
      Column() {
        ImageAnimator()
          //动画数组
          .images([
            {src:$r('app.media.icon'),
              width:100,
              height:100,
              top:40,
              left:40,
              duration:1000},
            {src:$rawfile('111.png'),
              width:200,
              height:200,
              top:30,
              left:50,
              duration:200},
            {src:$rawfile('222.png'),
              width:300,
              height:200,
              top:10,
              left:50,
              duration:500},
            {src:$rawfile('333.png'),
              width:100,
              height:100,
              top:10,
              left:150,
              duration:300},
          ])
          //动画时长
          .duration(2000)
          .margin({top:30})
          .backgroundColor(Color.Green)
          //开始
          .state(AnimationStatus.Running)
          //正序还是倒序
          .reverse(false)
          .fillMode(FillMode.Forwards)
          //图片自己设置宽高
          .fixedSize(false)
          //重复次数,-1为无限
          .iterations(-1)
          .width(340)
          .height(240)
          //动画开始回调函数
          .onStart(() => {
            console.info(' ImageAnimator onStart')
          })
          .onPause(() => {
            console.info('ImageAnimator onPause')
          })
          .onRepeat(() => {
            console.info('ImageAnimator onRepeat')
          })
          .onCancel(() => {
            console.info('ImageAnimator onCancel')
          })
          .onFinish(() => {
            console.info('ImageAnimator onFinish')
          })
      }
      .width('100%')
      .height('100%')
  }
}

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Winform 中播放 GIF 可以通过使用 PictureBox 控件来实现。以下是实现的步骤: 1. 首先,在 Visual Studio 的 Winform 项目中添加一个 PictureBox 控件。可以通过拖拽方式从工具箱中添加。 2. 在代码中,指定 GIF 文件的路径。可以在项目的资源文件夹中添加 GIF 文件,并通过路径访问。 3. 使用 Image 对象加载 GIF 文件,并将其赋值给 PictureBox 控件的 Image 属性。 示例代码如下: ```c# // 指定 GIF 文件的路径 string gifPath = "路径/至/您的/GIF/文件.gif"; // 使用 Image 对象加载 GIF 文件 Image gifImage = Image.FromFile(gifPath); // 将 Image 对象赋值给 PictureBox 控件的 Image 属性 pictureBox1.Image = gifImage; // 设置 PictureBox 控件的 SizeMode 属性为 AutoSize,让控件自适应图像的大小 pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize; // 设置 PictureBox 控件的 Visible 属性为 true,使其可见 pictureBox1.Visible = true; // 播放 GIF pictureBox1.Image = gifImage; ``` 通过以上步骤,即可在 Winform 中播放 GIF 图片。运行程序时,PictureBox 控件会根据 GIF 图片的数自动播放动画。 ### 回答2: Winform是一个用于开发Windows桌面应用程序的框架,它提供了丰富的控件和功能,可以轻松创建各种功能丰富的用户界面。 要在Winform中播放GIF,有几种方法可以实现。以下是其中的一种常见方法: 首先,需要在Winform中添加一个PictureBox控件,并将其大小设置为适当的大小以容纳所需的GIF图像。 然后,在代码中,我们需要使用.NET Framework提供的System.Drawing命名空间的Image对象来加载GIF图像。可以使用Image类的FromStream方法来加载GIF图像的文件流。 接下来,我们需要将加载的GIF图像分解成一系列的,然后将每一按照指定的时间间隔依次显示在PictureBox控件上。可以使用Image类的SelectActiveFrame方法来选择GIF图像的每一。 最后,在Winform的事件处理程序中,可以使用Timer控件来控制每一的显示时间,以实现动画的效果。可以在Timer的Tick事件中使用PictureBox控件的Image属性来实现不同之间的切换。 总的来说,要在Winform中播放GIF,需要加载GIF图像、分解、控制显示时间等步骤。通过使用PictureBox控件和Timer控件,结合Image对象的相关方法,可以实现在Winform应用程序中播放GIF图像的效果。 ### 回答3: 在WinForms应用程序中播放GIF动画相对简单,可以通过PictureBox控件完成。下面是一个示例的步骤: 1. 首先,在设计窗体时将一个PictureBox控件拖放到窗体上,用于显示GIF动画。 2. 确保已经将GIF文件添加到项目中,并设置其"Build Action"属性为"Embedded Resource"。 3. 在窗体的构造函数或Load事件中添加以下代码: ```csharp private void Form1_Load(object sender, EventArgs e) { // 读取嵌入式资源 Assembly assembly = Assembly.GetExecutingAssembly(); Stream stream = assembly.GetManifestResourceStream("YourNamespace.YourGif.gif"); // 创建GIF动画对象 Image gif = Image.FromStream(stream); // 将GIF动画设置为PictureBox的图像 pictureBox1.Image = gif; // 开始播放GIF动画 ImageAnimator.Animate(gif, OnFrameChanged); } // 处理改变事件 private void OnFrameChanged(Object sender, EventArgs e) { pictureBox1.Refresh(); // 刷新PictureBox控件以显示下一 } ``` 注意要把 "YourNamespace.YourGif.gif" 替换为你项目中的GIF文件的完整资源路径。 通过这些步骤,你的WinForms应用程序就可以播放GIF动画了。这段代码首先读取嵌入式资源中的GIF文件并创建动画对象,然后将其设置为PictureBox的图像,并用ImageAnimator.Animate()方法开始播放动画改变事件OnFrameChanged()将触发PictureBox的Refresh()方法,从而实现动画的播放。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值