c#动画(两点动画,旋转动画,变色动画)及案例

 C#动画:

两点动画,旋转动画,变色动画

1.边框

border:可以容纳一个其他元素

BorderThickness:边框厚度

BorderBrush:边框颜色

CornerRadius:设置边框圆角

添加动画的几大步骤:

1.设置边框border

2.命名空间(必写)

using System.Windows.Media.Animation;添加动画前必须先命名动画空间

3,找剧本,故事本

Storyboard story1 = new Storyboard();

4,选择动画类型:DoubleAnimation 动画名称= new DoubleAnimation();

5,设置动画的属性(5个必选属性,2个可选属性):①----⑤必选属性   ⑥ ⑦可选属性

①From:设置动画的起始值

②to:设置动画的终止值

③Duration:设置动画的进行时间

④Storyboard.SetTarget:设置某个动画的主角(设置的动画,要作为目标的依赖对象)

⑤  Storyboard.SetTargetProperty:设置动画的属性

⑥ AutoReverse:设置动画可逆播放

⑦RepeatBehavior:是否重复播放

6,.将动画添加进故事本中:story1.children.Add(动画名);

7.开启动画:动画名称.Begin();

案例1:动画类型:两点动画(差值动画)

window中的代码

<Window x:Class="动画.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:动画"
        mc:Ignorable="d"
        Title="MainWindow" Height="400" Width="400" Loaded="Window_Loaded">
    <Canvas Name="BG" Width="400" Height="400">
        
    </Canvas>
</Window>
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 动画
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //实例化一个边框对象
            Border bd= new Border();
            bd.Width = 100;
            bd.Height = 100;
            //设置背景颜色
            bd.Background = Brushes.LightBlue;
            Canvas.SetLeft(bd, 100);
            Canvas.SetTop(bd, 100);
            BG.Children.Add(bd);
            //设置边框厚度
            bd.BorderThickness = new Thickness(10);
            bd.BorderBrush = Brushes.Yellow;
            bd.CornerRadius = new CornerRadius(100);//画圆
            //动画:
           //1.添加故事本                                       
            Storyboard story1 = new Storyboard();
            //2.选择动画类型:两点动画
            DoubleAnimation anim = new DoubleAnimation();
            //3.设置动画起始值
            anim.From =100;
            //3.设置动画终止值
            anim.To = 200;
            //4.设置动画进行时间
            anim.Duration = new Duration(TimeSpan.FromSeconds(2));
            //5.设置动画主角
            Storyboard.SetTarget(anim, bd);
            //6.设置动画属性:
            Storyboard.SetTargetProperty(anim, new PropertyPath("(Canvas.Left)"));
            //Storyboard.SetTargetProperty(anim, new PropertyPath("(Canvas.Top)"));
            //可选属性 1.设置动画逆向播放
            anim.AutoReverse = true;
            //可选属性 2.设置动画重复播放
            anim.RepeatBehavior = RepeatBehavior.Forever;
            //设置动画重复播放几次
            //anim.RepeatBehavior = new RepeatBehavior(6);
            //将动画添加进故事本中
            story1.Children.Add(anim);
            //启动动画
            story1.Begin();
        }
    }
}

案例2.动画类型:旋转动画

实现旋转必写:

设置旋转的方向 RotateTransform rotate = new RotateTransform();
​ 给对象设置,绑定旋转中心 br.RenderTransform = rotate;

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 旋转动画1
{
    /// <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 = 200;
            br.Height = 210;
            br.Background = Brushes.LightBlue;
            Canvas.SetLeft(br, 150);
            Canvas.SetTop(br, 150);
            BG.Children.Add(br);

            //设置旋转方向
            RotateTransform rotate = new RotateTransform();
            //绑定旋转中心
            br.RenderTransform = rotate;
            //设置旋转中心百分比
            br.RenderTransformOrigin = new Point(0.5, 0.5);

            Storyboard story = new Storyboard();
            DoubleAnimation da = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(3)));
            Storyboard.SetTarget(da, br);
            Storyboard.SetTargetProperty(da, new PropertyPath("RenderTransform.Angle"));
            story.Children.Add(da);
            story.Begin();
        }
    }
}

案例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 变色动画
{
    /// <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.BorderThickness = new Thickness(20);
            br.BorderBrush = Brushes.GreenYellow;
            br.Width = br.Height = 200;
            br.Background = Brushes.Yellow;
            Canvas.SetLeft(br,100);
            Canvas.SetTop(br, 100);
            BG.Children.Add(br);
            Opacity = 1;
            //改变背景颜色
            Storyboard story = new Storyboard();
            ColorAnimation coloran = new ColorAnimation(Colors.SkyBlue, Colors.Plum, new Duration(TimeSpan.FromSeconds(6)));
            Storyboard.SetTarget(coloran, br);
            Storyboard.SetTargetProperty(coloran, new PropertyPath("(Border.Background).(SolidColorBrush.Color)"));
            story.Children.Add(coloran);
            story.Begin();
            //改变边框颜色
            //Storyboard story = new Storyboard();
            ColorAnimation colorbr = new ColorAnimation(Colors.YellowGreen, Colors.Yellow, new Duration(TimeSpan.FromSeconds(6)));
            Storyboard.SetTarget(colorbr, br);
            Storyboard.SetTargetProperty(colorbr, new PropertyPath("(Border.BorderBrush).(SolidColorBrush.Color)"));
            story.Children.Add(colorbr);
            story.Begin();
            //改变边框厚度,
            ThicknessAnimation ta = new ThicknessAnimation(new Thickness(20),new Thickness(0),new Duration(TimeSpan.FromSeconds(6)));
            Storyboard.SetTarget(ta, br);
            Storyboard.SetTargetProperty(ta, new PropertyPath("BorderThickness"));
            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();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43552118/article/details/86181617