在这里插入图片描述
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);
        }
    }
}
posted on 2019-01-08 14:06  豆皮没有豆  阅读(463)  评论(0)    收藏  举报