Windows 8应用实例解析 - WinRT下创建GIF动画(Flipflop)

简介:

在Windows Store中下载了一个有意思的应用,叫做Flipflop(http://apps.microsoft.com/windows/app/flipflop/99c01512-fe4f-4d1a-872e-eb9fd6638ff4),该应用允许用户创建翻页式动画效果(Flipbook Animation), 并且可以导出动画为GIF文件。在MSDN看到一篇介绍该项目的技术文章,分享给大家。

Flipflop项目中,作者使用Windows Imaging Component(WIC)实现创建图片(MSDN查看Windows 8 WIC新特性),

在项目中引用Windows.Graphics命名空间,在该空间中包含BitmapEncoder类(MSDN),通过该类可以创建特定的图片编码,例如GifEncoder。

通过代码查看工具,可以看到作者创建一个共享类"GifMaker"实现动画帧的定义,

 


 
 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Runtime.InteropServices.WindowsRuntime; 
  4. using Windows.Foundation; 
  5. using Windows.Graphics.Imaging; 
  6. using Windows.Storage; 
  7.  
  8. namespace Utilities 
  9. { 
  10.     public sealed class GifMaker 
  11.     { 
  12.         readonly List<byte[]> frames = new List<byte[]>(); 
  13.         private readonly uint frameWidth; 
  14.         private readonly uint frameHeight; 
  15.  
  16.         public GifMaker(uint width, uint height) 
  17.         { 
  18.             frameWidth = width; 
  19.             frameHeight = height; 
  20.         } 
  21.  
  22.         public void AppendNewFrame([ReadOnlyArray]byte[] frame) 
  23.         { 
  24.             frames.Add(frame); 
  25.         } 

实现翻页式动画效果的关键是将多个单帧图片连接在一起,然后通过延时设置播放各个帧实现动画效果。

在BitmapEncoder类(MSDN)中,GoToNextFrameAsync()方法可实现在当前帧的基础上异步动态添加新的空白帧,而通过代码实现浏览各个单一图片,动态放入不同的空白帧中实现翻页动画效果。

 


 
 
  1. public IAsyncInfo GenerateAsync(StorageFile file) 
  2. { 
  3.     return AsyncInfo.Run(async ctx => 
  4.         { 
  5.             var outStream = await file.OpenAsync(FileAccessMode.ReadWrite); 
  6.  
  7.             var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.GifEncoderId, outStream); 
  8.  
  9.             for (int i = 0; i < frames.Count; i++) 
  10.             { 
  11.                 var pixels = frames[i]; 
  12.                 encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore, 
  13.                                      frameWidth, frameHeight, 
  14.                                      92.0, 92.0, 
  15.                                      pixels); 
  16.  
  17.                 if (i < frames.Count - 1) 
  18.                     await encoder.GoToNextFrameAsync(); 
  19.             } 
  20.  
  21.             await encoder.FlushAsync(); 
  22.             outStream.Dispose(); 
  23.         }); 
  24. } 

最后,需要设置播放帧延迟时间,以达到翻页动画效果。控制帧延迟的属性是encoder.BitmapProperties和“/grctlext/Delay”,代码如下:

 


 
 
  1. public IAsyncInfo GenerateAsync(StorageFile file, int delay) 
  2. { 
  3.     return AsyncInfo.Run(async ctx => 
  4.         { 
  5.             var outStream = await file.OpenAsync(FileAccessMode.ReadWrite); 
  6.  
  7.             var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.GifEncoderId, outStream); 
  8.  
  9.             for (int i = 0; i < frames.Count; i++) 
  10.             { 
  11.                 var pixels = frames[i]; 
  12.                 encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore, 
  13.                                      frameWidth, frameHeight, 
  14.                                      92.0, 92.0, 
  15.                                      pixels); 
  16.  
  17.                 if (i == 0) 
  18.                 { 
  19.                     var properties = new BitmapPropertySet 
  20.                         { 
  21.                             { 
  22.                                 "/grctlext/Delay", 
  23.                                 new BitmapTypedValue(delay / 10, PropertyType.UInt16) 
  24.                             } 
  25.                         }; 
  26.  
  27.                     await encoder.BitmapProperties.SetPropertiesAsync(properties); 
  28.                 } 
  29.  
  30.                 if (i < frames.Count - 1) 
  31.                     await encoder.GoToNextFrameAsync(); 
  32.             } 
  33.  
  34.             await encoder.FlushAsync(); 
  35.             outStream.Dispose(); 
  36.         }); 
  37. } 

如果你是使用JavaScript作为Windows store应用开发语言,可以使用以下代码实现以上相同的效果,

 


 
 
  1. var picker = new Windows.Storage.Pickers.FileSavePicker(); 
  2. picker.fileTypeChoices.insert("Gif files", [".gif"]); 
  3.  
  4. picker.pickSaveFileAsync().then(function(file) { 
  5.     if (!file) { 
  6.         return; 
  7.     } 
  8.     var gifMaker = new Utilities.GifMaker(800, 600); 
  9.  
  10.     for (var commandsIndex = 0; commandsIndex < currentFlipflop.pages.length; commandsIndex++) { 
  11.         // This code is used to retrieve canvases bytes 
  12.         var bytes = Flipflop.Tools.GetBytesfromFlipflop(currentFlipflop.pages[commandsIndex], 
  13.             800, 600); 
  14.  
  15.         gifMaker.appendNewFrame(bytes); 
  16.     } 
  17.  
  18.     gifMaker.generateAsync(file, 200).done(); 
  19. }); 

Flipflop产生GIF动画效果演示:

 

欢迎大家留言讨论学习Windows 8应用开发,希望能够看到更多优秀的Windows store应用。

欢迎大家加入QQ技术群,一起学习讨论Windows 8&Silverlight&WPF&Widnows Phone开发技术。 
22308706(一群) 超级群500人 
37891947(二群) 超级群500人 
100844510(三群) 高级群200人 
32679922(四群) 超级群500人 
23413513(五群) 高级群200人 
32679955(六群) 超级群500人 
88585140(八群) 超级群500人 
128043302(九群 企业应用开发推荐群) 高级群200人 
101364438(十群) 超级群500人


本文转自冷秋寒 51CTO博客,原文链接:http://blog.51cto.com/kevinfan/1148024 ,如需转载请自行联系原作者

相关文章
|
Ubuntu API C++
C++标准库、Windows API及Ubuntu API的综合应用
总之,C++标准库、Windows API和Ubuntu API的综合应用是一项挑战性较大的任务,需要开发者具备跨平台编程的深入知识和丰富经验。通过合理的架构设计和有效的工具选择,可以在不同的操作系统平台上高效地开发和部署应用程序。
527 11
|
安全 前端开发 Windows
Windows Electron 应用更新的原理是什么?揭秘 NsisUpdater
本文介绍了 Electron 应用在 Windows 中的更新原理,重点分析了 `NsisUpdater` 类的实现。该类利用 NSIS 脚本,通过初始化、检查更新、下载更新、验证签名和安装更新等步骤,确保应用的更新过程安全可靠。核心功能包括差异下载、签名验证和管理员权限处理,确保更新高效且安全。
883 4
Windows Electron 应用更新的原理是什么?揭秘 NsisUpdater
|
网络协议 定位技术 Windows
Windows Server 2019 DNS服务器搭建
Windows Server 2019 DNS服务器搭建
1199 1
|
vr&ar C# 图形学
WPF与AR/VR的激情碰撞:解锁Windows Presentation Foundation应用新维度,探索增强现实与虚拟现实技术在现代UI设计中的无限可能与实战应用详解
【8月更文挑战第31天】增强现实(AR)与虚拟现实(VR)技术正迅速改变生活和工作方式,在游戏、教育及工业等领域展现出广泛应用前景。本文探讨如何在Windows Presentation Foundation(WPF)环境中实现AR/VR功能,通过具体示例代码展示整合过程。尽管WPF本身不直接支持AR/VR,但借助第三方库如Unity、Vuforia或OpenVR,可实现沉浸式体验。例如,通过Unity和Vuforia在WPF中创建AR应用,或利用OpenVR在WPF中集成VR功能,从而提升用户体验并拓展应用功能边界。
651 1
|
数据库 Windows
超详细步骤解析:从零开始,手把手教你使用 Visual Studio 打造你的第一个 Windows Forms 应用程序,菜鸟也能轻松上手的编程入门指南来了!
【8月更文挑战第31天】创建你的第一个Windows Forms (WinForms) 应用程序是一个激动人心的过程,尤其适合编程新手。本指南将带你逐步完成一个简单WinForms 应用的开发。首先,在Visual Studio 中创建一个“Windows Forms App (.NET)”项目,命名为“我的第一个WinForms 应用”。接着,在空白窗体中添加一个按钮和一个标签控件,并设置按钮文本为“点击我”。然后,为按钮添加点击事件处理程序`button1_Click`,实现点击按钮后更新标签文本为“你好,你刚刚点击了按钮!”。
2182 1
|
XML 缓存 前端开发
Electron-builder 是如何打包 Windows 应用的?
本文首发于微信公众号“前端徐徐”,作者徐徐深入解析了 electron-builder 在 Windows 平台上的打包流程。文章详细介绍了 `winPackager.ts`、`AppxTarget.ts`、`MsiTarget.ts` 和 `NsisTarget.ts` 等核心文件,涵盖了目标创建、图标处理、代码签名、资源编辑、应用签名、性能优化等内容,并分别讲解了 AppX/MSIX、MSI 和 NSIS 安装程序的生成过程。通过这些内容,读者可以更好地理解和使用 electron-builder 进行 Windows 应用的打包和发布。
1620 0
|
域名解析 缓存 网络协议
Windows系统云服务器自定义域名解析导致网站无法访问怎么解决?
Windows系统云服务器自定义域名解析导致网站无法访问怎么解决?
|
域名解析 网络协议 安全
Windows Server 2003 DNS服务器搭建
Windows Server 2003 DNS服务器搭建
375 0
|
数据可视化 程序员 C#
C#中windows应用窗体程序的输入输出方法实例
C#中windows应用窗体程序的输入输出方法实例
545 0
|
开发者 C# Windows
WPF与游戏开发:当桌面应用遇见游戏梦想——利用Windows Presentation Foundation打造属于你的2D游戏世界,从环境搭建到代码实践全面解析新兴开发路径
【8月更文挑战第31天】随着游戏开发技术的进步,WPF作为.NET Framework的一部分,凭借其图形渲染能力和灵活的UI设计,成为桌面游戏开发的新选择。本文通过技术综述和示例代码,介绍如何利用WPF进行游戏开发。首先确保安装最新版Visual Studio并创建WPF项目。接着,通过XAML设计游戏界面,并在C#中实现游戏逻辑,如玩家控制和障碍物碰撞检测。示例展示了创建基本2D游戏的过程,包括角色移动和碰撞处理。通过本文,WPF开发者可更好地理解并应用游戏开发技术,创造吸引人的桌面游戏。
1252 0

推荐镜像

更多
  • DNS