.NET WPF c#变色动画

本文介绍如何使用WPF创建变色动画,包括背景颜色、边框颜色、边框厚度及透明度的变化,并通过实例展示了两张图片的动画效果,涉及旋转、位移及消失等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
2.创建一个border设置border的属性,添加到canvas里
3.添加动画

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;//动画的命名空间
namespace _01变色动画
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Border br = new Border();
            br.Width = br.Height = 200;
            Canvas.SetLeft(br, 100);
            Canvas.SetTop(br, 100);
            BG.Children.Add(br);
            br.Background = Brushes.Red;
            br.BorderBrush = Brushes.Yellow;
            br.BorderThickness = new Thickness(20);
            br.Opacity = 1;
            //改变背景颜色
            Storyboard story = new Storyboard();
            ColorAnimation coloran = new ColorAnimation(Colors.Red,Colors.Green,new Duration(TimeSpan.FromSeconds(5)));
            Storyboard.SetTarget(coloran, br);
            Storyboard.SetTargetProperty(coloran, new PropertyPath("(Border.Background).(SolidColorBrush.Color)"));
            story.Children.Add(coloran);
            //改变边框颜色
            ColorAnimation colora = new ColorAnimation(Colors.Black, Colors.Yellow, new Duration(TimeSpan.FromSeconds(5)));
            Storyboard.SetTarget(colora, br);
            Storyboard.SetTargetProperty(colora, new PropertyPath("(Border.BorderBrush).(SolidColorBrush.Color)"));
            story.Children.Add(colora);
            //改变边框厚度
            ThicknessAnimation ta = new ThicknessAnimation(new Thickness(20),new Thickness(0),new Duration(TimeSpan.FromSeconds(5)));
            Storyboard.SetTarget(ta, br);
            Storyboard.SetTargetProperty(ta, new PropertyPath("BorderThickness"));
            story.Children.Add(ta);
            //改变透明度
            DoubleAnimation da = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(5)));
            Storyboard.SetTarget(da, br);
            Storyboard.SetTargetProperty(da, new PropertyPath("Opacity"));
            story.Children.Add(da);
            story.Begin();
        }
    }
}

案例:两张图片,分别在左上角和右下角,鼠标左单击左上角图片时,左上角图片旋转两圈后位移(改变left和top)到右下角图片位置消失
代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;//动画的命名空间
namespace _02案例
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        Storyboard story = new Storyboard();
        Storyboard story2 = new Storyboard();
        Image img = new Image();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Maximized;//窗口最大化
            Image box = new Image();
            box.Width = box.Height = 300;
            box.Source = new BitmapImage(new Uri("box.png",UriKind.Relative));
            Canvas.SetLeft(box, this.Width - box.Width);
            Canvas.SetTop(box, this.Height - box.Height);
            BG.Children.Add(box);

            img.Width = img.Height = 100;
            img.Source = new BitmapImage(new Uri("aa.png", UriKind.Relative));
            Canvas.SetLeft(img, 0);
            Canvas.SetTop(img, 0);
            BG.Children.Add(img);

            DoubleAnimation da = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(1)));
            Storyboard.SetTarget(da, img);
            Storyboard.SetTargetProperty(da, new PropertyPath("RenderTransform.Angle"));
            da.RepeatBehavior = new RepeatBehavior(2);//旋转两次
            RotateTransform rotate = new RotateTransform();
            img.RenderTransform = rotate;
            img.RenderTransformOrigin = new Point(0.5, 0.5);
            story.Children.Add(da);
            img.MouseLeftButtonDown += Img_MouseLeftButtonDown;

            DoubleAnimation dax = new DoubleAnimation(0, Canvas.GetLeft(box) + box.Width / 2 - img.Width / 2, new Duration(TimeSpan.FromSeconds(3)));
            DoubleAnimation day = new DoubleAnimation(0, Canvas.GetTop(box) + box.Height / 2 - img.Height / 2, new Duration(TimeSpan.FromSeconds(3)));
            Storyboard.SetTarget(dax, img);
            Storyboard.SetTarget(day, img);
            Storyboard.SetTargetProperty(dax, new PropertyPath("(Canvas.Left)"));
            Storyboard.SetTargetProperty(day, new PropertyPath("(Canvas.Top)"));
            story2.Children.Add(dax);
            story2.Children.Add(day);
        }

        private void Img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            story.Completed += Story_Completed;//completed事件必须写在动画开始代码之前
            story.Begin();
        }

        private void Story_Completed(object sender, EventArgs e)
        {
            story2.Completed += Story2_Completed;
            story2.Begin();
        }

        private void Story2_Completed(object sender, EventArgs e)
        {
            BG.Children.Remove(img);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆皮没有豆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值