wpf实现两点动画效果

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 = 100;
            br.Background = Brushes.Pink;
            Canvas.SetLeft(br,100);
            Canvas.SetTop(br,100);
            BG.Children.Add(br);
            br.BorderThickness = new Thickness(20);
            br.BorderBrush = Brushes.Red;
            br.CornerRadius = new CornerRadius(50);

            //动画
            //1.找剧本   故事本
            Storyboard story1 = new Storyboard();
            //2.选择动画类型  
            //两点动画  差值动画
            DoubleAnimation da = new DoubleAnimation();
            //必要属性1:设置动画的起始值
            da.From = 100;
            //必要属性2:设置动画的终止值
            da.To = 500;
            //必要属性3:动画的进行时间
            da.Duration = new Duration(TimeSpan.FromSeconds(2));
            //必要属性4:设置某个动画的主角(男女一号)
            Storyboard.SetTarget(da,br);
            //必要属性5:设置动画的属性
            Storyboard.SetTargetProperty(da,new PropertyPath("(Canvas.Left)"));
            //可选属性1.是否反着播放
            da.AutoReverse = true;
            //可选属性2.是否重复播放
            da.RepeatBehavior = RepeatBehavior.Forever;
            //重复几次
           // da.RepeatBehavior = new RepeatBehavior(3);
            //将动画添加进故事版里面
            story1.Children.Add(da);
            //动画开始
            story1.Begin();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43551373/article/details/86067577