存在问题:

最近接手公司一个比较成熟的产品项目开发(WPF桌面端),其中,在登陆系统加载时,60张图片切换,实现loading闪烁加载,快有密集恐惧症了!!!

  代码如下:

private void LoadPics()
{
try
{
_storyboard = new Storyboard();
for (int i = ; i < ; i++)
{
ObjectAnimationUsingKeyFrames oauf = new ObjectAnimationUsingKeyFrames();
//ObjectAnimationUsingKeyFrames 可以对指定 Duration 内的一组 KeyFrames 中的 Object 属性值进行动画处理
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri("pack://application:,,,/jg.CloudCube.WPF;component/Resources/LoadingAnimation/loading" + (i + ) + ".png");
bitmap.CacheOption = BitmapCacheOption.Default;
bitmap.EndInit(); ImageBrush imgBrush = new ImageBrush(bitmap);
//读取图片文件
DiscreteObjectKeyFrame dokf = new DiscreteObjectKeyFrame();
//DiscreteObjectKeyFrame 通过使用离散内插,可以在前一个关键帧的 Object 值及其自己的 Value 之间进行动画处理。
dokf.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(i * ));
//KeyTime 获取或设置应到达关键帧的目标 Value 的时间
//这里每隔40毫秒设置一张图片,也就是每秒1000/40=25帧
dokf.Value = imgBrush;
oauf.KeyFrames.Add(dokf);
Storyboard.SetTargetProperty(oauf, new PropertyPath("(Rectangle.Fill)"));
//把动画应用到窗体的Rectangle中
Storyboard.SetTarget(oauf, RectDis);
//RectDis是Rectangle的名称
_storyboard.Children.Add(oauf);
//把ObjectAnimationUsingKeyFrames动画添加到Storyboard中
}
_storyboard.RepeatBehavior = RepeatBehavior.Forever;
}
catch (Exception ex)
{
var log = log4net.LogManager.GetLogger("Error");
log.Error(ex.Message, ex);
}
}

60张图:

这样的实现效果,实在对不住 WPF对动画那么好的支持。

解决方式:

下面使用WPF(Storyboard)只在xaml中,实现Loading加载闪烁动画。

  代码如下:

<Window x:Class="TestWpf.Window4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Window4" Height="300" Width="300"> <Window.Resources>
<Style x:Key="FlashStyle" TargetType="{x:Type FrameworkElement}">
<Style.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(FrameworkElement.Opacity)"
BeginTime="00:00:00" From="1" To="0" Duration="00:00:01.5" AutoReverse="True"
RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
<Storyboard x:Key="StoryLeftToRight" RepeatBehavior="Forever" Duration="00:00:01.5">
<DoubleAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="e1"
Storyboard.TargetProperty="(FrameworkElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.0" Value="1" />
<SplineDoubleKeyFrame KeyTime="00:00:00.5" Value="0" />
<SplineDoubleKeyFrame KeyTime="00:00:01.0" Value="0" />
<SplineDoubleKeyFrame KeyTime="00:00:01.5" Value="0" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="e2"
Storyboard.TargetProperty="(FrameworkElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.0" Value="0" />
<SplineDoubleKeyFrame KeyTime="00:00:00.5" Value="1" />
<SplineDoubleKeyFrame KeyTime="00:00:01.0" Value="0" />
<SplineDoubleKeyFrame KeyTime="00:00:01.5" Value="0" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="e3"
Storyboard.TargetProperty="(FrameworkElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.0" Value="0" />
<SplineDoubleKeyFrame KeyTime="00:00:00.5" Value="0" />
<SplineDoubleKeyFrame KeyTime="00:00:01.0" Value="1" />
<SplineDoubleKeyFrame KeyTime="00:00:01.5" Value="0" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="e4"
Storyboard.TargetProperty="(FrameworkElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.0" Value="0" />
<SplineDoubleKeyFrame KeyTime="00:00:00.5" Value="0" />
<SplineDoubleKeyFrame KeyTime="00:00:01.0" Value="0" />
<SplineDoubleKeyFrame KeyTime="00:00:01.5" Value="1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources> <Grid Background="DeepSkyBlue">
<StackPanel Orientation="Horizontal" Margin="20" VerticalAlignment="Bottom" HorizontalAlignment="Right">
<TextBlock Text="Loading " Style="{StaticResource FlashStyle}" Foreground="White" FontSize="20"/>
<StackPanel Orientation="Horizontal">
<StackPanel.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource StoryLeftToRight}" />
</EventTrigger>
</StackPanel.Triggers>
<Ellipse
Name="e1"
Width="5"
Height="5"
Margin="5,0,0,0"
HorizontalAlignment="Left"
Fill="White" />
<Ellipse
Name="e2"
Width="5"
Height="5"
Margin="5,0,0,0"
HorizontalAlignment="Left"
Fill="White" />
<Ellipse
Name="e3"
Width="5"
Height="5"
Margin="5,0,0,0"
HorizontalAlignment="Left"
Fill="White" />
<Ellipse
Name="e4"
Width="5"
Height="5"
Margin="5,0,0,0"
HorizontalAlignment="Left"
Fill="White" />
</StackPanel>
</StackPanel>
</Grid>
</Window>

  实现效果:

   技术要点:

      DoubleAnimation属性介绍:

  Storyboard.TargetName:附加属性操作到指定的对象上;
  Storyboard.TargetProperty:要操作指定对象的属性;
  From..To :上述属性值的起始范围;
  Duration: 在多少时间内完成上述属性值的变化;
  RepeatBehavior:反复次数;
  AutoReverse:完成向迭代后是否按相反的顺序播放

      System.Windows.Media.Animation提供三种动画类线性插值动画类(17个)、关键帧动画(22个)、路径动画(3个).

      参考:  http://www.cnblogs.com/libaoheng/archive/2012/04/23/2466242.html

    

WPF动画 - Loading加载动画的更多相关文章

  1. 2款不同样式的CSS3 Loading加载动画 附源码

    原文:2款不同样式的CSS3 Loading加载动画 附源码 我们经常看到的Loading加载很多都是转圈圈的那种,今天我们来换一种有创意的CSS3 Loading加载动画,一种是声波形状的动画,另一 ...

  2. HTML5 五彩圆环Loading加载动画实现教程

    原文:HTML5 五彩圆环Loading加载动画实现教程 今天我们要来介绍一款效果很特别的HTML5 Loading加载动画,不像其他的Loading动画,这款Loading动画颜色很丰富,并且在转圈 ...

  3. vue+elementUI+axios实现的全局loading加载动画

    在项目中,很多时候都需要loading加载动画来缓解用户的焦虑等待,比如说,我打开了一个页面,而这个页面有很多接口请求,但浏览器的请求并发数就那么几个,再加上如果网速不行的话,那么这时候,用户很可能就 ...

  4. QT自定义控件系列(二) --- Loading加载动画控件

    本系列主要使用Qt painter来实现一些基础控件.主要是对平时自行编写的一些自定义控件的总结. 为了简洁.低耦合,我们尽量不使用图片,qrc,ui等文件,而只使用c++的.h和.cpp文件. 由于 ...

  5. 16款纯CSS3实现的loading加载动画

    分享16款纯CSS3实现的loading加载动画.这是一款实用的可替代GIF格式图片的CSS3加载动画代码.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div clas ...

  6. 10个样式各异的CSS3 Loading加载动画

    前几天我在园子里分享过2款很酷的CSS3 Loading加载动画,今天又有10个最新的Loading动画分享给大家,这些动画的样式都不一样,实现起来也并不难,你很容易把它们应用在项目中,先来看看效果图 ...

  7. 原生JS+ CSS3创建loading加载动画;

    效果图: js创建loading show = function(){ //loading dom元素 var Div = document.createElement("div" ...

  8. CSS动画实例:Loading加载动画效果(一)

    一些网站或者APP在加载新东西的时候,往往会给出一个好看有趣的Loading图,大部分的Loading样式都可以使用CSS3制作出来,它不仅比直接使用gif图简单方便,还能节省加载时间和空间.下面介绍 ...

  9. CSS动画实例:Loading加载动画效果(三)

    3.小圆型Loading 这类Loading动画的基本思想是:在呈现容器中定义1个或多个子层,再对每个子层进行样式定义,使得其均显示为一个实心圆形,最后编写关键帧动画控制,使得各个实心圆或者大小发生改 ...

随机推荐

  1. Module in powershell

    https://docs.microsoft.com/en-us/powershell/module/powershellget/install-module?view=powershell-6 ht ...

  2. YTU 2432: C++习题 对象数组输入与输出

    2432: C++习题 对象数组输入与输出 时间限制: 1 Sec  内存限制: 128 MB 提交: 1603  解决: 1152 题目描述 建立一个对象数组,内放n(n<10)个学生的数据( ...

  3. 深入探析c# Socket

    最近浏览了几篇有关Socket发送消息的文章,发现大家对Socket Send方法理解有所偏差,现将自己在开发过程中对Socket的领悟写出来,以供大家参考. (一)架构 基于TCP协议的Socket ...

  4. BZOJ_2099_[Usaco2010 Dec]Letter 恐吓信_后缀自动机+贪心

    BZOJ_2099_[Usaco2010 Dec]Letter 恐吓信_后缀自动机 Description FJ刚刚和邻居发生了一场可怕的争吵,他咽不下这口气,决定佚名发给他的邻居 一封脏话连篇的信. ...

  5. python from import与import as 的含义

    from os import makedirs, unlink, sep #从os包中引入 makedirs.unlink,sep类 from os.path import dirname, exis ...

  6. OC:基础总结

    OC面向对象的编程语言思想 类与对象.继承与实例化.属性点语法.内存管理.字符串.可见度. 类是一组具有相同特征和行为的事物的抽象 OC的与C相比所具有的新的特点: 定义新的类.类的实例和方法.方法的 ...

  7. bzoj 3566: [SHOI2014]概率充电器【树形概率dp】

    设g[u]为这个点被儿子和自己充上电的概率,f[u]为被儿子.父亲和自己充上电的概率 然后根据贝叶斯公式(好像是叫这个),1.P(A+B)=P(A)+P(B)-P(A)*P(B),2.P(A)=(P( ...

  8. 洛谷 P2761 软件补丁问题 【spfa】

    -为什么最短路的题会出现在网络流24里?? 因为范围是15所以直接把每个状态作为一个点,向它能转移到的点连有向边即可.可以不用建图(据说建图存不下?),直接枚举m个转移方案.位运算比较麻烦注意不要写错 ...

  9. 51nod 1238 最小公倍数之和 V3 【欧拉函数+杜教筛】

    首先题目中给出的代码打错了,少了个等于号,应该是 G=0; for(i=1;i<=N;i++) for(j=1;j<=N;j++) { G = (G + lcm(i,j)) % 10000 ...

  10. 洛谷P4141消失之物(背包经典题)——Chemist

    题目地址:https://www.luogu.org/problemnew/show/P4141 分析:这题当然可以直接暴力枚举去掉哪一个物品,然后每次暴力跑一遍背包,时间复杂度为O(m*n^2),显 ...