手把手教你 用 wpf 制作metro ProgressRing (Windows8 等待动画)

19 篇文章 0 订阅
17 篇文章 0 订阅

效果图:

 

还在羡慕metro的ProgressRing吗?

wpf 也可以拥有

首先说下思路,

一共6个点围绕一直圆转,所以需要使用rotation动画 并且一直转下去。

那么下面的问题就好解决了。

首先是xaml 部分

我们需要实现旋转动画:

所以要用到这个:

 

<DoubleAnimationUsingKeyFrames 
      Storyboard.TargetProperty="(Ellipse.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
                <LinearDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
                <EasingDoubleKeyFrame Value="90" KeyTime="0:0:0.2">
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame Value="270" KeyTime="0:0:1.6">
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame Value="450" KeyTime="0:0:1.8">
                </EasingDoubleKeyFrame>
                <LinearDoubleKeyFrame Value="630" KeyTime="0:0:3.2"/>
                <EasingDoubleKeyFrame Value="720" KeyTime="0:0:3.4">
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame Value="720" KeyTime="0:0:5.0">
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>

上面这一段是单个ellipse的运动轨迹,当然你需要在属性中设置他的中心点值

代码如下:

<Ellipse x:Name="el" Width="10" Height="10" Fill="White" Canvas.Left="73" Canvas.Top="57" >
            <Ellipse.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform CenterX="-20" CenterY="-40"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Ellipse.RenderTransform>
        </Ellipse>


 

 

接下来的事情就好办了,我们需要他转1圈就消失 结束后也消失,所以需要控制透明度,

<DoubleAnimationUsingKeyFrames 
						Storyboard.TargetProperty="Opacity">
                <LinearDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
                <EasingDoubleKeyFrame Value="1" KeyTime="0:0:0.2">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <BackEase EasingMode="EaseInOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame Value="1" KeyTime="0:0:1.6">
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame Value="1" KeyTime="0:0:1.8">
                </EasingDoubleKeyFrame>
                <LinearDoubleKeyFrame Value="1" KeyTime="0:0:3.2"/>
                <EasingDoubleKeyFrame Value="0" KeyTime="0:0:3.5">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <BackEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame Value="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>
        <Storyboard x:Key="Trans" RepeatBehavior="Forever">
            <DoubleAnimationUsingKeyFrames 
						Storyboard.TargetProperty="(Ellipse.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
                <LinearDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
                <EasingDoubleKeyFrame Value="90" KeyTime="0:0:0.2">
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame Value="270" KeyTime="0:0:1.6">
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame Value="450" KeyTime="0:0:1.8">
                </EasingDoubleKeyFrame>
                <LinearDoubleKeyFrame Value="630" KeyTime="0:0:3.2"/>
                <EasingDoubleKeyFrame Value="720" KeyTime="0:0:3.4">
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame Value="720" KeyTime="0:0:5.0">
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames 
						Storyboard.TargetProperty="Opacity">
                <LinearDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
                <EasingDoubleKeyFrame Value="1" KeyTime="0:0:0.2">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <BackEase EasingMode="EaseInOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame Value="1" KeyTime="0:0:1.6">
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame Value="1" KeyTime="0:0:1.8">
                </EasingDoubleKeyFrame>
                <LinearDoubleKeyFrame Value="1" KeyTime="0:0:3.2"/>
                <EasingDoubleKeyFrame Value="0" KeyTime="0:0:3.5">
                    <EasingDoubleKeyFrame.EasingFunction>
                        <BackEase EasingMode="EaseOut"/>
                    </EasingDoubleKeyFrame.EasingFunction>
                </EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame Value="0" KeyTime="0:0:5.0">
                </EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </UserControl.Resources>
    <Canvas>
        <Ellipse x:Name="el" Width="10" Height="10" Fill="White" Canvas.Left="73" Canvas.Top="57" >
            <Ellipse.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform CenterX="-20" CenterY="-40"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Ellipse.RenderTransform>
        </Ellipse>
        <Ellipse x:Name="el2" Width="10" Height="10" Fill="White" Canvas.Left="73" Canvas.Top="57" Opacity="0" >
            <Ellipse.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform CenterX="-20" CenterY="-40"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Ellipse.RenderTransform>
        </Ellipse>
        <Ellipse x:Name="el3" Width="10" Height="10" Fill="White" Canvas.Left="73" Canvas.Top="57" Opacity="0">
            <Ellipse.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform CenterX="-20" CenterY="-40"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Ellipse.RenderTransform>
        </Ellipse>
        <Ellipse x:Name="el4" Width="10" Height="10" Fill="White" Canvas.Left="73" Canvas.Top="57" Opacity="0">
            <Ellipse.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform CenterX="-20" CenterY="-40"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Ellipse.RenderTransform>
        </Ellipse>
        <Ellipse x:Name="el5" Width="10" Height="10" Fill="White" Canvas.Left="73" Canvas.Top="57" Opacity="0">
            <Ellipse.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform CenterX="-20" CenterY="-40"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Ellipse.RenderTransform>
        </Ellipse>
        <Ellipse x:Name="el6" Width="10" Height="10" Fill="White" Canvas.Left="73" Canvas.Top="57" Opacity="0">
            <Ellipse.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform CenterX="-20" CenterY="-40"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Ellipse.RenderTransform>
        </Ellipse>
    </Canvas>
</UserControl>


 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Transvalue.MetroStyleBusyIndicator
{
    /// <summary>
    /// MetroRotaionIndicator.xaml 的交互逻辑
    /// </summary>
    public partial class MetroRotaionIndicator : UserControl
    {
        Storyboard trans;
        public MetroRotaionIndicator()
        {
            InitializeComponent();
            trans = Resources["Trans"] as Storyboard;
            this.Loaded += ((sender, e) =>
            {
                Active();
            });
        }

        public async void Active()
        {
            el.BeginStoryboard(trans);
            await Task.Delay(170);
            el2.BeginStoryboard(trans);
            await Task.Delay(170);
            el3.BeginStoryboard(trans);
            await Task.Delay(170);
            el4.BeginStoryboard(trans);
            await Task.Delay(170);
            el5.BeginStoryboard(trans);
            await Task.Delay(170);
            el6.BeginStoryboard(trans);
        }

        public void Stop()
        {
            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" Height="187" Margin="924,534,0,0" VerticalAlignment="Top" Width="217"/>

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值