【WPF学习手记】旋转动态图效果演示

目的:旋转动态图效果。

做成了UserControl,使用很方便。

  •  UCWaitingProgress.xaml
<UserControl x:Class="WPF_WaitingProgress.UCWaitingProgress"
             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:local="clr-namespace:WPF_WaitingProgress"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Storyboard x:Key="waiting" Name="waiting">
            <DoubleAnimation Storyboard.TargetName="SpinnerRotate" Storyboard.TargetProperty="(RotateTransform.Angle)" From="0" To="359" Duration="0:0:02" RepeatBehavior="Forever" />
        </Storyboard>
    </UserControl.Resources>
    <Image Name="image" Source="loading.png" RenderTransformOrigin="0.5,0.5" Stretch="Uniform" Loaded="Image_Loaded">
        <Image.RenderTransform>
            <RotateTransform x:Name="SpinnerRotate" Angle="0" />
        </Image.RenderTransform>
    </Image>
</UserControl>
  • UCWaitingProgress.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace WPF_WaitingProgress
{
    /// <summary>
    /// UCWaitingProgress.xaml 的交互逻辑
    /// </summary>
    public partial class UCWaitingProgress : UserControl
    {
        private Storyboard storyboard;

        public UCWaitingProgress()
        {
            InitializeComponent();
            storyboard = (Resources["waiting"] as Storyboard);
        }

        private void Image_Loaded(object sender, RoutedEventArgs e)
        {
            Begin();
        }

        public void Begin()
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                storyboard.Begin(image, true);
            }));
        }

        public void Stop()
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                storyboard.Pause(image);
            }));
        }
    }
}
  • xaml代码
<Window x:Class="WPF_WaitingProgress.MainWindow"
        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"
        xmlns:local="clr-namespace:WPF_WaitingProgress"
        mc:Ignorable="d"
        Title="MainWindow" Height="400" Width="400">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <local:UCWaitingProgress x:Name="waitingProgress" Height="100"/>
        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <Button Content="Start" Margin="5" Click="Start_Click"/>
            <Button Content="Stop" Margin="5" Click="Stop_Click"/>
        </StackPanel>
    </Grid>
</Window>
  • 后台代码
using System.Windows;

namespace WPF_WaitingProgress
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            waitingProgress.Begin();
        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            waitingProgress.Stop();
        }
    }
}

 

WPF(Windows Presentation Foundation)可以通过使用动画和转换效果来实现图片环形旋转轮动效果。首先,我们可以使用WPF中的Ellipse控件来创建一个圆形容器,然后在这个容器中放置图片控件。接下来,我们可以使用WPF中的动画功能来实现图片的环形旋转效果。 首先,我们可以使用DoubleAnimation来定义图片旋转的动画效果。通过改变图片控件的RenderTransform属性(例如RotateTransform),我们可以让图片围绕圆形容器的中心点进行旋转。我们可以使用Storyboard来组织动画,并通过使用AnimateProperty属性来让图片按照一定的速度和方向进行环形旋转。 另外,我们还可以使用Path类来定义图片的旋转路径,通过使用PathAnimationUsingPath属性来让图片沿着指定的路径进行环形旋转。在这个过程中,我们可以通过设置RepeatBehavior属性来让图片循环环形旋转一定的次数或者无限循环。 此外,我们还可以通过使用触发器和事件来控制图片环形旋转的起始和停止。例如,我们可以通过鼠标悬停事件(MouseEnter)来触发环形旋转效果的开始,而通过鼠标离开事件(MouseLeave)来触发环形旋转效果的停止。 总体来说,利用WPF中丰富的动画和转换功能,我们可以很容易地实现图片的环形旋转轮动效果。相比较于传统的WinForms等框架,WPF提供了更加灵活和强大的界面设计和动画效果实现方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值