效果图:
还在羡慕metro的ProgressRing吗?
wpf 也可以拥有
首先说下思路,
一共6个点围绕一直圆转,所以需要使用rotation动画 并且一直转下去。
那么下面的问题就好解决了。
首先是xaml 部分
我们需要实现旋转动画:
所以要用到这个:
- <DoubleAnimationUsingKeyFrames
- Storyboard.TargetProperty="(Ellipse.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
- <LinearDoubleKeyFrameValue="0"KeyTime="0:0:0"/>
- <EasingDoubleKeyFrameValue="90"KeyTime="0:0:0.2">
- </EasingDoubleKeyFrame>
- <EasingDoubleKeyFrameValue="270"KeyTime="0:0:1.6">
- </EasingDoubleKeyFrame>
- <EasingDoubleKeyFrameValue="450"KeyTime="0:0:1.8">
- </EasingDoubleKeyFrame>
- <LinearDoubleKeyFrameValue="630"KeyTime="0:0:3.2"/>
- <EasingDoubleKeyFrameValue="720"KeyTime="0:0:3.4">
- </EasingDoubleKeyFrame>
- <EasingDoubleKeyFrameValue="720"KeyTime="0:0:5.0">
- </EasingDoubleKeyFrame>
- </DoubleAnimationUsingKeyFrames>
上面这一段是单个ellipse的运动轨迹,当然你需要在属性中设置他的中心点值
代码如下:
- <Ellipsex:Name="el"Width="10"Height="10"Fill="White"Canvas.Left="73"Canvas.Top="57">
- <Ellipse.RenderTransform>
- <TransformGroup>
- <ScaleTransform/>
- <SkewTransform/>
- <RotateTransformCenterX="-20"CenterY="-40"/>
- <TranslateTransform/>
- </TransformGroup>
- </Ellipse.RenderTransform>
- </Ellipse>
接下来的事情就好办了,我们需要他转1圈就消失 结束后也消失,所以需要控制透明度,
- <DoubleAnimationUsingKeyFrames
- Storyboard.TargetProperty="Opacity">
- <LinearDoubleKeyFrameValue="0"KeyTime="0:0:0"/>
- <EasingDoubleKeyFrameValue="1"KeyTime="0:0:0.2">
- <EasingDoubleKeyFrame.EasingFunction>
- <BackEaseEasingMode="EaseInOut"/>
- </EasingDoubleKeyFrame.EasingFunction>
- </EasingDoubleKeyFrame>
- <EasingDoubleKeyFrameValue="1"KeyTime="0:0:1.6">
- </EasingDoubleKeyFrame>
- <EasingDoubleKeyFrameValue="1"KeyTime="0:0:1.8">
- </EasingDoubleKeyFrame>
- <LinearDoubleKeyFrameValue="1"KeyTime="0:0:3.2"/>
- <EasingDoubleKeyFrameValue="0"KeyTime="0:0:3.5">
- <EasingDoubleKeyFrame.EasingFunction>
- <BackEaseEasingMode="EaseOut"/>
- </EasingDoubleKeyFrame.EasingFunction>
- </EasingDoubleKeyFrame>
- <EasingDoubleKeyFrameValue="0"KeyTime="0:0:5.0">
- </EasingDoubleKeyFrame>
- </DoubleAnimationUsingKeyFrames>
最终把一个圆变成多个圆的工作 就交给代码了,需要一点点小技巧 以下使用.net 4.5实现 其他版本可以吧Task.Delay 替换成Thread.Sleep
- <UserControl
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
- xmlns:local="clr-namespace:Transvalue.MetroStyleBusyIndicator"
- x:Class="Transvalue.MetroStyleBusyIndicator.MetroRotaionIndicator"
- mc:Ignorable="d"
- d:DesignHeight="300"d:DesignWidth="300">
- <UserControl.Resources>
- <Storyboardx:Key="Trans"RepeatBehavior="Forever">
- <DoubleAnimationUsingKeyFrames
- Storyboard.TargetProperty="(Ellipse.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
- <LinearDoubleKeyFrameValue="0"KeyTime="0:0:0"/>
- <EasingDoubleKeyFrameValue="90"KeyTime="0:0:0.2">
- </EasingDoubleKeyFrame>
- <EasingDoubleKeyFrameValue="270"KeyTime="0:0:1.6">
- </EasingDoubleKeyFrame>
- <EasingDoubleKeyFrameValue="450"KeyTime="0:0:1.8">
- </EasingDoubleKeyFrame>
- <LinearDoubleKeyFrameValue="630"KeyTime="0:0:3.2"/>
- <EasingDoubleKeyFrameValue="720"KeyTime="0:0:3.4">
- </EasingDoubleKeyFrame>
- <EasingDoubleKeyFrameValue="720"KeyTime="0:0:5.0">
- </EasingDoubleKeyFrame>
- </DoubleAnimationUsingKeyFrames>
- <DoubleAnimationUsingKeyFrames
- Storyboard.TargetProperty="Opacity">
- <LinearDoubleKeyFrameValue="0"KeyTime="0:0:0"/>
- <EasingDoubleKeyFrameValue="1"KeyTime="0:0:0.2">
- <EasingDoubleKeyFrame.EasingFunction>
- <BackEaseEasingMode="EaseInOut"/>
- </EasingDoubleKeyFrame.EasingFunction>
- </EasingDoubleKeyFrame>
- <EasingDoubleKeyFrameValue="1"KeyTime="0:0:1.6">
- </EasingDoubleKeyFrame>
- <EasingDoubleKeyFrameValue="1"KeyTime="0:0:1.8">
- </EasingDoubleKeyFrame>
- <LinearDoubleKeyFrameValue="1"KeyTime="0:0:3.2"/>
- <EasingDoubleKeyFrameValue="0"KeyTime="0:0:3.5">
- <EasingDoubleKeyFrame.EasingFunction>
- <BackEaseEasingMode="EaseOut"/>
- </EasingDoubleKeyFrame.EasingFunction>
- </EasingDoubleKeyFrame>
- <EasingDoubleKeyFrameValue="0"KeyTime="0:0:5.0">
- </EasingDoubleKeyFrame>
- </DoubleAnimationUsingKeyFrames>
- </Storyboard>
- </UserControl.Resources>
- <Canvas>
- <Ellipsex:Name="el"Width="10"Height="10"Fill="White"Canvas.Left="73"Canvas.Top="57">
- <Ellipse.RenderTransform>
- <TransformGroup>
- <ScaleTransform/>
- <SkewTransform/>
- <RotateTransformCenterX="-20"CenterY="-40"/>
- <TranslateTransform/>
- </TransformGroup>
- </Ellipse.RenderTransform>
- </Ellipse>
- <Ellipsex:Name="el2"Width="10"Height="10"Fill="White"Canvas.Left="73"Canvas.Top="57"Opacity="0">
- <Ellipse.RenderTransform>
- <TransformGroup>
- <ScaleTransform/>
- <SkewTransform/>
- <RotateTransformCenterX="-20"CenterY="-40"/>
- <TranslateTransform/>
- </TransformGroup>
- </Ellipse.RenderTransform>
- </Ellipse>
- <Ellipsex:Name="el3"Width="10"Height="10"Fill="White"Canvas.Left="73"Canvas.Top="57"Opacity="0">
- <Ellipse.RenderTransform>
- <TransformGroup>
- <ScaleTransform/>
- <SkewTransform/>
- <RotateTransformCenterX="-20"CenterY="-40"/>
- <TranslateTransform/>
- </TransformGroup>
- </Ellipse.RenderTransform>
- </Ellipse>
- <Ellipsex:Name="el4"Width="10"Height="10"Fill="White"Canvas.Left="73"Canvas.Top="57"Opacity="0">
- <Ellipse.RenderTransform>
- <TransformGroup>
- <ScaleTransform/>
- <SkewTransform/>
- <RotateTransformCenterX="-20"CenterY="-40"/>
- <TranslateTransform/>
- </TransformGroup>
- </Ellipse.RenderTransform>
- </Ellipse>
- <Ellipsex:Name="el5"Width="10"Height="10"Fill="White"Canvas.Left="73"Canvas.Top="57"Opacity="0">
- <Ellipse.RenderTransform>
- <TransformGroup>
- <ScaleTransform/>
- <SkewTransform/>
- <RotateTransformCenterX="-20"CenterY="-40"/>
- <TranslateTransform/>
- </TransformGroup>
- </Ellipse.RenderTransform>
- </Ellipse>
- <Ellipsex:Name="el6"Width="10"Height="10"Fill="White"Canvas.Left="73"Canvas.Top="57"Opacity="0">
- <Ellipse.RenderTransform>
- <TransformGroup>
- <ScaleTransform/>
- <SkewTransform/>
- <RotateTransformCenterX="-20"CenterY="-40"/>
- <TranslateTransform/>
- </TransformGroup>
- </Ellipse.RenderTransform>
- </Ellipse>
- </Canvas>
- </UserControl>
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Threading;
- usingSystem.Threading.Tasks;
- usingSystem.Windows;
- usingSystem.Windows.Controls;
- usingSystem.Windows.Data;
- usingSystem.Windows.Documents;
- usingSystem.Windows.Input;
- usingSystem.Windows.Media;
- usingSystem.Windows.Media.Animation;
- usingSystem.Windows.Media.Imaging;
- usingSystem.Windows.Navigation;
- usingSystem.Windows.Shapes;
- namespaceTransvalue.MetroStyleBusyIndicator
- {
- ///<summary>
- ///MetroRotaionIndicator.xaml的交互逻辑
- ///</summary>
- publicpartialclassMetroRotaionIndicator:UserControl
- {
- Storyboardtrans;
- publicMetroRotaionIndicator()
- {
- InitializeComponent();
- trans=Resources["Trans"]asStoryboard;
- this.Loaded+=((sender,e)=>
- {
- Active();
- });
- }
- publicasyncvoidActive()
- {
- el.BeginStoryboard(trans);
- awaitTask.Delay(170);
- el2.BeginStoryboard(trans);
- awaitTask.Delay(170);
- el3.BeginStoryboard(trans);
- awaitTask.Delay(170);
- el4.BeginStoryboard(trans);
- awaitTask.Delay(170);
- el5.BeginStoryboard(trans);
- awaitTask.Delay(170);
- el6.BeginStoryboard(trans);
- }
- publicvoidStop()
- {
- trans.Stop(el);
- trans.Stop(el2);
- trans.Stop(el3);
- trans.Stop(el4);
- trans.Stop(el5);
- trans.Stop(el6);
- }
- }
- }
将以上内容编译成用户控件即可使用。
xmlns:MetroStyleBusyIndicator="clr-namespace:Transvalue.MetroStyleBusyIndicator;assembly=Transvalue.MetroStyleBusyIndicator"
<MetroStyleBusyIndicator:MetroRotaionIndicator HorizontalAlignment="Left" Margin="924,534,0,0" VerticalAlignment="Top" />