云网站建设017年青,深圳网站建设制作营销,seo技术是什么,请网络推广外包公司有用吗前言今天介绍一篇使用json格式在wpf中播放动画效果#xff1b;正文话说在上古#xff08;1987#xff09;时代#xff0c;Gif因其体积小成像相对清晰和非常强的兼容性#xff0c;而大受欢迎;Gif也因为当时的技术限制导致很多缺陷 这包括对电脑的内存和性能占用非常大;同时… 前言今天介绍一篇使用json格式在wpf中播放动画效果正文话说在上古1987时代Gif因其体积小成像相对清晰和非常强的兼容性而大受欢迎;Gif也因为当时的技术限制导致很多缺陷 这包括对电脑的内存和性能占用非常大;同时Gif还是一个有损文件格式 对半透明和颜色都有一定程度的限制;随着技术的进步衍生出了 apng和webp格式相对技术色彩范围更广效果也更清晰也占用更低的内存;apng和webp这两种格式需要复杂的开发环境来支持还是不太友好这时就需要另外一种格式了 序列帧序列帧它是一个无损且低内存的格式不过只能在客户端使用因为帧数多想要在web环境中使用 就需要转换为雪碧图Lottie动画是由airbnb公司推出的Lottie的原理是把各种矢量素材以及效果 打包成一个体积很小的json文件然后交给开发人员就好了经常在APP所见到的动态图标都是由Lottie来实现的下面我们如何开源项目LottieSharp[1]进行展现json文件动画1Nuget 搜索 LottieSharp 点击安装2使用方式很简单如下ws:Window x:ClassLottieSharp.Sample.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:LottieSharp.Samplexmlns:lottieSharpclr-namespace:LottieSharp;assemblyLottieSharpxmlns:wshttps://github.com/WPFDevelopersOrg.WPFDevelopers.Minimalmc:IgnorabledTitle{Binding PathImageDrawable.Fps, StringFormat{}LottieSharp{0}, ElementNameLottieAnimationView} Height450 Width800GridGrid.ColumnDefinitionsColumnDefinition WidthAuto/ColumnDefinition //Grid.ColumnDefinitionsExpander ExpandDirectionLeft Grid.Column0 Style{DynamicResource ExpanderStyle1} IsExpandedTrueBorder BorderBrush{StaticResource PrimaryPressedSolidColorBrush}BorderThickness0,0,1,0ListBox x:NamemyListBoxSelectionChangedmyListBox_SelectionChanged//Border/ExpanderGrid Grid.Column1Grid.RowDefinitionsRowDefinition Height* /RowDefinition Heightauto /RowDefinition Heightauto /RowDefinition Heightauto /RowDefinition Heightauto //Grid.RowDefinitionslottieSharp:LottieAnimationView x:NameLottieAnimationView DefaultCacheStrategyNone FileNameAssets/moody-dog.json AutoPlayTrue VerticalAlignmentCenter HorizontalAlignmentCenter/Slider Grid.Row1 Maximum10 Value1 Minimum0.1 SmallChange0.1 LargeChange0.1 ValueChangedSlider_ValueChanged_1 /Slider Grid.Row2 Minimum0 Maximum1000 SmallChange1 ValueChangedSlider_ValueChanged /DockPanel Grid.Row3 Margin5Button DockPanel.DockLeft ContentPause Animation ClickPauseAnimation_Click HorizontalAlignmentLeft /Button DockPanel.DockLeft Margin10,0,0,0 ContentStart Animation ClickStartAnimation_Click HorizontalAlignmentLeft/!--Button DockPanel.DockLeft ContentLoad Animation Margin10,0,0,0 ClickLoadAnimation_Click HorizontalAlignmentLeft/--StackPanel DockPanel.DockRight OrientationHorizontal HorizontalAlignmentRight Margin10,0 Width158TextBlock TextFps: FontSize16 VerticalAlignmentCenter/TextBlockTextBox Text{Binding FrameRate, ElementNameLottieAnimationView, ModeTwoWay} Width60//StackPanel/DockPanelGrid Grid.Row4 Margin5Grid.ColumnDefinitionsColumnDefinition WidthAuto /ColumnDefinition Width* /ColumnDefinition WidthAuto /ColumnDefinition WidthAuto //Grid.ColumnDefinitionsTextBlock Grid.Column0 TextImageAssetsFolder (optional): VerticalAlignmentCenter/TextBox Grid.Column1 NameImageAssetsFolderTextBox Margin10,0,0,0 TextChangedImageAssetsFolderTextBox_TextChanged/Button Grid.Column2 Margin10,0,0,0 Content... ClickLoadImageAssetsFolder_Click /Button Grid.Column3 Margin10,0,0,0 ContentX ToolTipDelete path ClickDeleteImageAssetsFolder_Click //Grid/Grid/Grid
/ws:Window3后台逻辑代码using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;namespace LottieSharp.Sample
{/// summary/// Interaction logic for MainWindow.xaml/// /summarypublic partial class MainWindow{public MainWindow(){InitializeComponent();Loaded MainWindow_Loaded;LottieAnimationView.UseHardwareAcceleration(true);}private void MainWindow_Loaded(object sender, RoutedEventArgs e){var path System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Assets);var root new DirectoryInfo(path);var array new Liststring(); foreach (var item in root.GetFiles()){array.Add(item.Name);}myListBox.ItemsSource array;}protected override void OnClosed(EventArgs e){base.OnClosed(e);LottieAnimationView.Dispose();DataContext null;}private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgsdouble e){LottieAnimationView.PauseAnimation();LottieAnimationView.Progress (float)(e.NewValue / 1000);}private void LoadAnimation_Click(object sender, RoutedEventArgs e){OpenFileDialog openFileDialog new OpenFileDialog();openFileDialog.DefaultExt .json;openFileDialog.Filter Json files|*.json|All files|*.*;if (openFileDialog.ShowDialog() true){LottieAnimationView.PauseAnimation();LottieAnimationView.FileName openFileDialog.FileName;LottieAnimationView.PlayAnimation();}}private void StartAnimation_Click(object sender, RoutedEventArgs e){LottieAnimationView.PlayAnimation();}private void PauseAnimation_Click(object sender, RoutedEventArgs e){LottieAnimationView.PauseAnimation();}private void LoadImageAssetsFolder_Click(object sender, RoutedEventArgs e){using (var dialog new System.Windows.Forms.FolderBrowserDialog()){if (dialog.ShowDialog() System.Windows.Forms.DialogResult.OK)ImageAssetsFolderTextBox.Text dialog.SelectedPath;}}private void DeleteImageAssetsFolder_Click(object sender, RoutedEventArgs e){ImageAssetsFolderTextBox.Text ;}private void ImageAssetsFolderTextBox_TextChanged(object sender, TextChangedEventArgs e){LottieAnimationView.PauseAnimation();LottieAnimationView.ImageAssetsFolder ImageAssetsFolderTextBox.Text;}private void Slider_ValueChanged_1(object sender, RoutedPropertyChangedEventArgsdouble e){if (!double.IsNaN(e.NewValue))LottieAnimationView.Scale (float)e.NewValue;}private void myListBox_SelectionChanged(object sender, SelectionChangedEventArgs e){var path System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Assets, myListBox.SelectedItem.ToString());LottieAnimationView.PauseAnimation();LottieAnimationView.FileName path;LottieAnimationView.PlayAnimation();}}
}案例中只是少数的json文件可以去官网[2]下载更多json文件;源码[3]参考资料[1]LottieSharp: https://github.com/ascora/LottieSharp[2]官网: https://lottiefiles.com/featured[3]源码: https://gitee.com/yanjinhua/LottieSharp