Lottie 動畫是一種以 json 檔案儲存的動畫檔案。相較於 gif,在檔案體積和動畫流暢度上有很大的優勢,而且圖形是向量的,放大不會糊。
Lottie 的官方提供了 web、android、iOS 的實作,微軟提供了 UWP 的實作 Lottie-Windows。WPF 則沒有找到可用的實作。下有 https://github.com/ascora/LottieSharp,不過我測試了幾輪,發現其依賴的 sharpDx 經常報錯,而且我完全不熟悉 sharpDx,不知道如何解決。
那麼身為 WPF 開發者,想要使用 Lottie 動畫看來還是要自己動手。
想要在 WPF 上使用 Lottie 動畫,主要需要攻克以下問題:
- 解析 Lottie 的 json 檔案,了解其中所有資料的意義,也就是得到需要繪製的圖形物件,包括各種圓形、矩形、顏色、模糊效果、遮罩、緩動、軌跡、關鍵影格等。
- 將上面得到的資料轉換為具體的繪圖參數。
- 在 WPF 中進行高幀率的繪製。
截至 2022-02-10,覺得可行的方案為:
- 微軟提供的 UWP 實作中,有現成的解析模組,可直接將 Json 檔案讀取為一個資料類別,且該模組並沒有依賴 UWP,所以可以直接拿來用。雖然還有一些 Bug(測試了幾十個動畫檔案,有 3 個無法正常播放),但當前該庫還算活躍,可以期待其改進。
- SkiaSharp 的功能非常全面,涵蓋 Lottie 動畫所需的功能應該不成問題,而且其跨平台,所以按照資料要求使用 SkiaSharp 進行繪製即可。
- SkiaSharp 官方提供的 WPF 畫布即使開啟 GPU 繪圖模式,其速度依然無法達到 1080p 60 幀,不過之前進行其他專案時對 GLWpfControl 進行了改進,實測 2k 144 幀佔用 RTX 2070 23%,達到可用程度,雖然有少量記憶體洩漏,但問題不大。
如果成功實作的話,應該可以同時支援 WinForm、WPF、UnoPlatform(GTK)、Avalonia、Blazor(Wasm) 以及所有 SkiaSharp 和 .NET Core 3.0+ 可以運行的平台上。
當前仍然處於探索驗證階段,無實用價值。
當前處於預覽狀態,範例:https://github.com/xiejiang2014/Xiejiang.SKLottie.Samples
本文發佈於https://www.cnblogs.com/8u7tgyjire7890/p/15881159.html
以下會持續更新成功實作的動畫,每次更新都說明支援了更多的 lottie 特性
2022-02-10 選了幾個簡單的動畫進行測試。
https://lottiefiles.com/93494-step-loader

https://lottiefiles.com/87474-duo-cubes-loader-2

混合模式
https://lottiefiles.com/89023-loading-circles

2022-02-12 稍微複雜一點的例子:
https://lottiefiles.com/84848-polar-bear
Lottie-Windows 沒有實作 PolyStar 的資料解析,所以缺少黃色的星星,只能等待 Lottie-Windows 繼續實作

路徑動畫。
https://lottiefiles.com/88348-blob

路徑偏移
https://lottiefiles.com/86095-frequencies-fork

2022-02-16:
層級關係,旋轉
https://lottiefiles.com/85410-wifi

2022-02-18
處理 PreCompLayer,LayerCollectionAsset
https://lottiefiles.com/88964-snowman

更複雜的路徑動畫。對比 Lottie Viewer 提示需要 UAP11,我已經升級到 Windows10 21H2 了依然無法播放,不清楚具體原因。
https://lottiefiles.com/87491-liquid-blobby-loader-green

2022-02-19
https://lottiefiles.com/93748-camera-roll

ImageAsset 和 ImageLayer
https://lottiefiles.com/93795-dog-car-ride

Opacity
https://lottiefiles.com/94910-greenish-arrow-down

2022-02-26
https://lottiefiles.com/87670-satisfied-bear

https://lottiefiles.com/96051-tta-certificate

更複雜的例子
https://lottiefiles.com/96388-tiger

2022-02-27
路徑的 FillType
https://lottiefiles.com/96511-listening-animation

2022-03-02
https://lottiefiles.com/97409-404-page-animation

蒙版(+模式)
https://lottiefiles.com/88967-food-delivery-service

蒙版(-模式)
https://lottiefiles.com/83667-arty-chat

https://lottiefiles.com/99804-backend-blue

遮罩(+模式)
https://lottiefiles.com/96957-lock

https://lottiefiles.com/89881-smiling-star

2020-04-01
https://lottiefiles.com/83395-addland-heatfav-v2

https://lottiefiles.com/96939-files

2022-04-17
https://lottiefiles.com/96245-success

https://lottiefiles.com/88282-rocket

https://github.com/CommunityToolkit/Lottie-Windows/files/8182417/Lottie.Animation.zip

https://lottiefiles.com/102516-growth
