先看效果图:
最直观的,这是4个圆点在移动,就用一个横向的StackPanel表示这四个点吧。
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value=""/>
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource AncestorType=UserControl, AncestorLevel=1}, Path= Foreground}"/>
<Setter Property="CornerRadius" Value=""/>
<Setter Property="Height" Value=""/>
<Setter Property="Width" Value=""/>
</Style>
</StackPanel.Resources>
<Border x:Name="border4" Margin="0,0,5,0" RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Border.RenderTransform>
</Border>
<Border x:Name="border3" Margin="0,0,5,0" RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Border.RenderTransform>
</Border>
<Border x:Name="border2" Margin="0,0,5,0" RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Border.RenderTransform>
</Border>
<Border x:Name="border1" RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Border.RenderTransform>
</Border>
</StackPanel>
圆点的颜色绑定在用户控件的Foreground上面。在调用控件的时候记得对其Foreground赋值以便显示适合的颜色。
这个动画实际上很简单,分为5个部分:
1.准备部分:准备向前移动
2.正向加速度部分:移动速度越来越快
3.匀速部分:慢速匀速移动
4.负向加速度部分:移动速度越来越慢
5.等待部分:等待所有动画完成
动画代码如下:
<Storyboard x:Key="sbKey" Completed="Storyboard_Completed" >
<!--最右边-->
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="border1">
<EasingDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.8">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:1.2"/>
<EasingDoubleKeyFrame KeyTime="0:0:2">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseIn"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:2.6"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="border2">
<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="0" x:Name="_2_1">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:0.9" Value="190" x:Name="_2_2">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:1.3" Value="210" x:Name="_2_3"/>
<EasingDoubleKeyFrame KeyTime="0:0:2.1" Value="400" x:Name="_2_4">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseIn"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:2.6" Value="400" x:Name="_2_5"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="border3">
<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0" x:Name="_3_1">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:1.0" Value="190" x:Name="_3_2">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:1.4" Value="210" x:Name="_3_3"/>
<EasingDoubleKeyFrame KeyTime="0:0:2.2" Value="400" x:Name="_3_4">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseIn"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:2.6" Value="400" x:Name="_3_5"/>
</DoubleAnimationUsingKeyFrames>
<!--最左边-->
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="border4">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0" x:Name="_4_1">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:1.1" Value="190" x:Name="_4_2">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:1.5" Value="210" x:Name="_4_3"/>
<EasingDoubleKeyFrame KeyTime="0:0:2.3" Value="400" x:Name="_4_4">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseIn"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:2.6" Value="400" x:Name="_4_5"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
由于动画需要自适应宽度,所以部分关键帧的值无法在XAML中表现出来,只有在后台进行动态处理。
当控件的宽度发生变化时需要更新动画部分关键帧:
/// <summary>
/// 当控件的大小发生变化时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
//当控件的宽度发生变化时
if(e.WidthChanged)
{
isNeedUpdateStoryboard = true;
}
}
当动画每一次执行完毕后,下一次执行之前,判断是否需要更新部分关键帧,所以响应动画的Completed事件:
/// <summary>
/// 动画执行完毕
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Storyboard_Completed(object sender, EventArgs e)
{
//每次动画执行完毕后判断是否需要更新动画
if(isNeedUpdateStoryboard)
{
UpdateStoryboard();
} isNeedUpdateStoryboard = false;
storyboard.Begin(this);
}
/// <summary>
/// 更新动画部分关键帧
/// </summary>
private void UpdateStoryboard()
{
//获取控件的实际宽度,减去4个点占用的宽度
double realWidth = this.ActualWidth - ;
//正向加速度占比47.5%
double _0Width = realWidth * 0.475;
//匀速部分占比5%
double _1Width = _0Width + realWidth * 0.05; //根据控件的实际大小动态更改动画
#region 右边第一个
var _0 = storyboard.Children[] as DoubleAnimationUsingKeyFrames;
var _0_1 = _0.KeyFrames[] as EasingDoubleKeyFrame;
var _0_2 = _0.KeyFrames[] as EasingDoubleKeyFrame;
var _0_3 = _0.KeyFrames[] as EasingDoubleKeyFrame;
var _0_4 = _0.KeyFrames[] as EasingDoubleKeyFrame; _0_1.Value = _0Width;
_0_2.Value = _1Width;
_0_3.Value = realWidth;
_0_4.Value = realWidth;
#endregion #region 右边倒数第二个
var _1 = storyboard.Children[] as DoubleAnimationUsingKeyFrames;
var _1_1 = _1.KeyFrames[] as EasingDoubleKeyFrame;
var _1_2 = _1.KeyFrames[] as EasingDoubleKeyFrame;
var _1_3 = _1.KeyFrames[] as EasingDoubleKeyFrame;
var _1_4 = _1.KeyFrames[] as EasingDoubleKeyFrame; _1_1.Value = _0Width;
_1_2.Value = _1Width;
_1_3.Value = realWidth;
_1_4.Value = realWidth;
#endregion #region 右边倒数第三个
var _2 = storyboard.Children[] as DoubleAnimationUsingKeyFrames;
var _2_1 = _2.KeyFrames[] as EasingDoubleKeyFrame;
var _2_2 = _2.KeyFrames[] as EasingDoubleKeyFrame;
var _2_3 = _2.KeyFrames[] as EasingDoubleKeyFrame;
var _2_4 = _2.KeyFrames[] as EasingDoubleKeyFrame; _2_1.Value = _0Width;
_2_2.Value = _1Width;
_2_3.Value = realWidth;
_2_4.Value = realWidth;
#endregion #region 右边倒数第四个
var _3 = storyboard.Children[] as DoubleAnimationUsingKeyFrames;
var _3_1 = _3.KeyFrames[] as EasingDoubleKeyFrame;
var _3_2 = _3.KeyFrames[] as EasingDoubleKeyFrame;
var _3_3 = _3.KeyFrames[] as EasingDoubleKeyFrame;
var _3_4 = _3.KeyFrames[] as EasingDoubleKeyFrame; _3_1.Value = _0Width;
_3_2.Value = _1Width;
_3_3.Value = realWidth;
_3_4.Value = realWidth;
#endregion
}
最后提供动画的开始和结束调用:
/// <summary>
/// 使进度条开始滚动
/// </summary>
public void Start()
{
this.Visibility = System.Windows.Visibility.Visible;
UpdateStoryboard();
storyboard.Begin(this, true);
}
/// <summary>
/// 使进度条停止滚动
/// </summary>
public void Stop()
{
this.Visibility = System.Windows.Visibility.Collapsed;
storyboard.Stop(this);
}
源代码下载:http://download.csdn.net/detail/lyclovezmy/7598897
WPF:Metro样式ProgressBar(圆点横向移动),自适应宽度的更多相关文章
-
ASP.NET实例——漂亮的自适应宽度的导航条(仿Discuz!)
PHP比较成熟的开放的源代码比较多,比方说PrestaShop,比方说Discuz!...... 虽然语言不同,但基本原理是一样的,有时间的话读一读,对学习ASP.NET应该是非常有好处的(唉,什么时 ...
-
shrink-to-fit(自适应宽度)
自适应宽度是指当未明白设定容器的宽度(或外边距设为auto)时,在特定的情况下容器的宽度会依据情况自行设定.而设定的结果往往并非我们想要的. W3C规范中描写叙述了几种shrink-to-fit的情况 ...
-
从三栏自适应宽度布局到css布局的讨论
如何实现一个三栏自适应布局,左右各100px,中间随着浏览器宽度自适应? 第一个想到的是使用table布局,设置table的宽度为100%,三个td,第1个和第3个固定宽度为100px,那么中间那个就 ...
-
UEditor百度富文本编辑器--让编辑器自适应宽度的解决方案
UEditor百度富文本编辑器的initialFrameWidth属性,默认值是1000. 不能够自适应屏幕宽度.如图1: 刚开始的时候,我是直接设置initialFrameWidth=null的.效 ...
-
JS实现自适应宽度的Tag切换
效果体验:http://hovertree.com/texiao/js/3.htm 该效果使用纯JavaScript代码,实现TAB页切换效果,TAB标签根据内容自适应宽度,点击TAB标签切换内容页. ...
-
WPF设置DataGrid行内容高度自适应 与 TextBox/TextBlock内容高度自适应
WPF设置DataGrid行内容高度自适应 TextBox/TextBlock内容高度自适应 参考: DataGrid 控件中的调整大小选项: http://msdn.microsoft.com/ ...
-
RelativeLayout中的格局,自适应宽度布局
RelativeLayout中的布局,自适应宽度布局 该图片中为android布局:总布局为 RelativeLayoutAtLeft 为居左 <TextView android:backgro ...
-
Qt的tablewidget行列头自适应宽度
Qt构造一个TableWidget后,窗口最大化后,列头默认不能自适应宽度,研究了一下,Qt提供了两种方式来处理这个问题,如下: 1. 使用horizontalHeader()->setRe ...
-
[Winform]DataGridView列自适应宽度
引言 在做winform项目中,数据控件DataGridView的使用多多少少是会用到的,如果不设置它的属性,默认情况下是不会自适应宽度的,你想查看某项的数据,就不得不将标题栏拖来拖去,挺烦的. 方法 ...
随机推荐
-
【bzoj3572】 世界树
http://www.lydsy.com/JudgeOnline/problem.php?id=3572 (题目链接) 题意 给出一棵n个节点的树,q次询问,每次给出k个关键点.规定对于树上每个节点归 ...
-
【转】安装OpenSSL缺失Microsoft Visual C++ 2008 Redistributables的解决方案
from: http://www.cnblogs.com/luguo3000/p/3539815.html 在安装OpenSSL的时候通常会提示以下错误: "The Win32 OpenSS ...
-
转载几篇关于GNU autotools的文章
http://www.laruence.com/2009/11/18/1154.html http://www.ibm.com/developerworks/cn/linux/l-makefile/ ...
-
[译]JavaScript insertAdjacentHTML
原文地址:http://davidwalsh.name/insertadjacenthtml-beforeend 该死的DOM慢的很.随着我们的网站动态交互和Ajax操作越来越多,我们需要寻找一种高性 ...
-
Telerik_2012_Q3 破解全套下载链接
1.Telerik_OpenAccess_ORM_2012_3_1012_SDK.zip (暂未提供下载) 2. Telerik_OpenAccess_ORM_2012_3_1012.zip 3. T ...
-
java--jsp+ssh+select动态结合数据和选择(解)
在三层体系结构和jsp合并项目,如何实现select动态绑定数据和动态选择指定的行?让我们来看看下面的: 1.首先定义一个Bean分类.它用于实例select的结合数据中的每一个id和name: pu ...
-
defer 和 async 区别
defer saync 共同点: script 标签属性, 控制脚本加载时间,解决script下载阻塞的问题. 区别: defer:推推推荐! 异步加载,所有元素解析完执行. async: 异步加载, ...
-
pypinyin, jieba分词与Gensim
一 . pypinyin from pypinyin import lazy_pinyin, TONE, TONE2, TONE3 word = '孙悟空' print(lazy_pinyin(wor ...
-
《DSP using MATLAB》Problem 5.5
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...
-
SpringBoot2 集成三种连接池 c3p0 hikari druid
Hikari 1.首先集成 hikari springboot默认集成,只需要简单的配置即可 1.1 首先导入包 <dependency> <groupId>com.zaxxe ...