WPF路径动画——C#篇

     在上一篇《WPF路径动画——XAML篇》中,所有的元素都是在XAML中定义的,用了少量的代码让动画运行起来。毕竟C#更灵活和动态,这次就重点用C#代码来完成动画,实现一个方块绕椭圆路径切线转动。

 

pgeUsingPath2.xaml

<Page x:Class="CnblogsDemo.pgeUsingPath2"
      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" 
      mc:Ignorable="d" d:DesignHeight="400" d:DesignWidth="500"    Title="路径动画使用C#"
      Loaded="Page_Loaded">

  <Grid ShowGridLines="True">
    <Grid.RowDefinitions>
      <RowDefinition Height="*"></RowDefinition>
      <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="3*"></ColumnDefinition>
      <ColumnDefinition Width="7*"></ColumnDefinition>
    </Grid.ColumnDefinitions>

    <Canvas Name="mainCanvas" Grid.Row="0" Grid.Column="1" Background="LightCyan" Margin="50,30,0,0">
      <Canvas.Resources>
        <SolidColorBrush x:Key="PathStrokeBrush01" Color="#FF686964" />
      </Canvas.Resources>
      <Rectangle x:Name="rotatingLine" Width="80" Height="20" Stroke="Blue" Fill="Red" />
      <!--用来指示椭圆路径的中心点-->
      <Ellipse Name="ese1" Fill="red" Width="7" Height="7" Stroke="black" StrokeThickness="2.5" HorizontalAlignment="Left" VerticalAlignment="Top"></Ellipse>
    </Canvas>

  </Grid>
</Page>

============================================================

pgeUsingPath2.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CnblogsDemo
{
    /// <summary>
    /// pgeUsingPath2.xaml 的交互逻辑
    /// </summary>
    public partial class pgeUsingPath2 : Page
    {
        public pgeUsingPath2()
        {
            InitializeComponent();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            rotatingLine.RenderTransformOrigin = new Point(0.5, 0.5);
            Canvas.SetLeft(rotatingLine, -rotatingLine.ActualWidth * rotatingLine.RenderTransformOrigin.X);
            Canvas.SetTop(rotatingLine, -rotatingLine.ActualHeight * rotatingLine.RenderTransformOrigin.Y);
            MatrixTransform MatrixTransform_01 = new MatrixTransform();

            this.RegisterName("MatrixTransform_01", MatrixTransform_01);
            rotatingLine.RenderTransform = MatrixTransform_01;

            Point centerPt = new Point(150, 150);
            ese1.Margin = new Thickness(centerPt.X, centerPt.Y, 0, 0);                    //指示中心点
            Canvas.SetLeft(ese1, -ese1.ActualWidth / 2);
            Canvas.SetTop(ese1, -ese1.ActualHeight / 2);

            PathGeometry aniPath = new PathGeometry();
            EllipseGeometry egStandard = new EllipseGeometry(centerPt, 100, 80);
            aniPath.AddGeometry(egStandard);

            //外层
            Path ph = new Path();
            SolidColorBrush StrokeBrush = (SolidColorBrush)mainCanvas.Resources["PathStrokeBrush01"];
            ph.Stroke = StrokeBrush;
            ph.StrokeThickness = 1;
            EllipseGeometry eg2 = new EllipseGeometry(centerPt, egStandard.RadiusX + rotatingLine.ActualHeight / 2, egStandard.RadiusY + rotatingLine.ActualHeight / 2);
            ph.Data = eg2;
            mainCanvas.Children.Add(ph);

            //内层
            Path ph3 = new Path();
            ph3.Stroke = StrokeBrush;
            //ph.Fill = System.Windows.Media.Brushes.MediumSlateBlue;
            ph3.StrokeThickness = 1;
            EllipseGeometry eg3 = new EllipseGeometry(centerPt, egStandard.RadiusX - rotatingLine.ActualHeight / 2, egStandard.RadiusY - rotatingLine.ActualHeight / 2);
            ph3.Data = eg3;
            mainCanvas.Children.Add(ph3);

            Path phStandard = new Path();
            phStandard.Stroke = System.Windows.Media.Brushes.Blue;
            //ph.Fill = System.Windows.Media.Brushes.MediumSlateBlue;
            phStandard.StrokeThickness = 1;
            phStandard.Data = egStandard;
            mainCanvas.Children.Add(phStandard);

            MatrixAnimationUsingPath matrixAnimation = new MatrixAnimationUsingPath();
            matrixAnimation.PathGeometry = aniPath;                                //动画的路径
            matrixAnimation.Duration = TimeSpan.FromSeconds(10);
            matrixAnimation.RepeatBehavior = RepeatBehavior.Forever;
            matrixAnimation.DoesRotateWithTangent = true;                            

            Storyboard.SetTargetName(matrixAnimation, "MatrixTransform_01");                        //动画的对象
            Storyboard.SetTargetProperty(matrixAnimation, new PropertyPath(MatrixTransform.MatrixProperty));

            Storyboard pathAnimationStoryboard = new Storyboard();
            pathAnimationStoryboard.Children.Add(matrixAnimation);
            pathAnimationStoryboard.Begin(this);
        }
    }
}

   
image

转载于:https://www.cnblogs.com/edong/archive/2012/08/27/2658611.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#运用WPF实现游戏动画制作 从QXGame(WPF GAME ENGINE)游戏引擎公布以来,受到很多朋友的热切关注,于是乎有了写教程的想法。那么从今天开始,我将带领大家一步一步的学会如何使用纯C#开发WPF/Silverlight游戏引擎,过程中我会尽量的开源,并对相关小技巧进行解释和介绍,比较复杂的算法原理我会给大家一条绝对可行的思路,这里只想客观的告诉读者下面两点: 一、WPF开发的是桌面应用程序,自包括Vista在内以后的Windows系列操作系统均大量以之为主流图形工具,即将全面取代Winform,并且Windows 7将集成.NET3.5+框架,在当今Windows系列操作系统占据90%同类市场的现状下,这意味着什么呢? 二、Silverlight基于一个约4M左右的MINI型.NET框架,目前版本2.0,3.0的beta英文版,从发展趋势看是绝对有与Flash抗衡并且在未来超越它的可能性。Silverlight的优势更表现在它可以用一切.NET语言例如C#,VB.NET,C++.NET等开发,拓展度与可以参与开发的人群远远高于只能用AS开发的FLASH。 转入正题,网上已经有很多关于如何创建WPF/Silverlight动画的教程,但是均为使用Blend工具制作,或直接写在xaml代码内的动画,这样往往造成很多朋友误以为其实WPF/Silverlight不就是MS的Flash?诚然,如果您真的像那些教程里说的去开发WPF/Silverlight程序,我个人觉得一点意义都没有。这样开发出来的东西根本就超越不了Flash,那何苦还要投入如此多的精力来学习它? 所以本系列教程将全方位的以纯C#程序语言进行动态创建一切可视化对象,从而构建出一个如QXGame(WPF GAME ENGINE)游戏引擎,这才是我本系列教程希望达到的目的。 (注:本教程使用的开发工具为Visual studio 2008 版本sp1)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值