我现在在做一个C#开发,用的是VS2010WPF编的,现在要实现一辆小汽车由开始到结束一直做着直线运动,想问问应该通过什么方法实现,求各位大神帮帮忙。
PS:因为时间比较紧迫,希望能得到代码一份,谢谢。c#wpf直线运动

解决方案 »

  1.   

    对animation这个不太了解啊,在网上查了一下,没太看懂,它是跟Android有关的?
      

  2.   

    2个类,画出小汽车,和直线然后对直线坐标做roll操作,可以?
      

  3.   

    加个timer控件、每秒钟使物体的X坐标+10  这样看起来都效果就像物体移动了起来一样
      

  4.   


    请问,timer控件应该怎么插入?请明示。是新建一个类吗?还是怎么办啊?
    求助
      

  5.   

    WPF的项目动画,直接用Blend设计就好了啊,直线运动,连代码都不用写
      

  6.   

    直接Storyboard 就可以实现吧。。<Storyboard x:Key="Move" RepeatBehavior="Forever" Timeline.DesiredFrameRate="30">
               <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="Car">
                    <EasingDoubleKeyFrame KeyTime="0" Value="起始点值"/>
                    <EasingDoubleKeyFrame KeyTime="0:0:15" Value="目标点值"/>
                </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Window.Triggers>
            <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                <BeginStoryboard Storyboard="{StaticResource Move}"/>
            </EventTrigger>
    </Window.Triggers>然后你用个Image控件放上汽车的图片就好了          <Image Name="Car" Stretch="Fill" Source="汽车图片路径">
                    <Image.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform/>
                            <SkewTransform/>
                            <RotateTransform/>
                            <TranslateTransform/>
                        </TransformGroup>
                    </Image.RenderTransform>
                </Image>
      

  7.   

    最简单的就是用个timer控件,每隔一段时间就更新一下坐标
      

  8.   

    //创建故事板
                    Storyboard storyboard = new Storyboard();
                    int cost = 100; //每移动一个方格花费100毫秒
                    //创建X轴方向逐帧动画
                    DoubleAnimationUsingKeyFrames keyFramesAnimationX = new DoubleAnimationUsingKeyFrames();
                    //总共花费时间 = path.Count * cost
                    keyFramesAnimationX.Duration = new Duration(TimeSpan.FromMilliseconds(path.Count * cost));
                    Storyboard.SetTarget(keyFramesAnimationX, player);
                    Storyboard.SetTargetProperty(keyFramesAnimationX, new PropertyPath("(Canvas.Left)"));
                    //创建Y轴方向逐帧动画
                    DoubleAnimationUsingKeyFrames keyFramesAnimationY = new DoubleAnimationUsingKeyFrames();
                    keyFramesAnimationY.Duration = new Duration(TimeSpan.FromMilliseconds(path.Count * cost));
                    Storyboard.SetTarget(keyFramesAnimationY, player);
                    Storyboard.SetTargetProperty(keyFramesAnimationY, new PropertyPath("(Canvas.Top)"));
                    for (int i = 0; i < framePosition.Count(); i++) {
                        //加入X轴方向的匀速关键帧
                        LinearDoubleKeyFrame keyFrame = new LinearDoubleKeyFrame();
                        keyFrame.Value = i == 0 ? Canvas.GetLeft(player) : framePosition[i].X; //平滑衔接动画
                        keyFrame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(cost * i));
                        keyFramesAnimationX.KeyFrames.Add(keyFrame);
                        //加入X轴方向的匀速关键帧
                        keyFrame = new LinearDoubleKeyFrame();
                        keyFrame.Value = i == 0 ? Canvas.GetTop(player) : framePosition[i].Y;
                        keyFrame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(cost * i));
                        keyFramesAnimationY.KeyFrames.Add(keyFrame);
                    }
                    storyboard.Children.Add(keyFramesAnimationX);
                    storyboard.Children.Add(keyFramesAnimationY);
                    //故事板动画开始
                    storyboard.Begin();
      

  9.   

    用Timer控件..该控件的默认事件就是时间触发时间,
    可以设置该控件的属性..主要是设置它多长时间运行一次.
    事件里面的内容就是每次获取小汽车的位置..然后在原有X点的坐标值上加上一个你需要移动的距离.
    就可以实现小汽车直线运动了.!
      

  10.   

    比较急的话找本书看看动画例子就会了。用blend做动画的话控制教简单,但是学会用blend做要花点时间^_^
      

  11.   


    您好,为什么我这边的工具箱里找不到timer这个控件呢?
      

  12.   


    用你这段代码时运行出错了,整了好久。话说我把起始点值、目标点值、图片存储位置都改了的啊。要不我把我的代码给你看下吧。你看看应该把它们插入哪个地方。您看看。
        
    <Window x:Class="Microsoft.Samples.Kinect.SpeechBasics.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Speeh Basics" Height="600" Width="640"
            Loaded="WindowLoaded" Closing="WindowClosing">
        <Window.Resources>
            <SolidColorBrush x:Key="MediumGreyBrush" Color="#ff6e6e6e"/>
            <SolidColorBrush x:Key="LightGreyBrush" Color="#ffd2d2d2"/>
            <SolidColorBrush x:Key="KinectPurpleBrush" Color="#ff52318f"/>
            <Style TargetType="{x:Type Image}">
                <Setter Property="SnapsToDevicePixels" Value="True"/>
            </Style>
        </Window.Resources>
       
        <Grid Margin="10 0 10 0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <DockPanel Height="77" Margin="0,-10,0,0">
                <Image Source="file:///G:\已修改的成功版\SpeechBasics-WPF\Images\xiaohui.png" Stretch="Fill" Height="55" Width="137" />
            </DockPanel>
            <Viewbox Grid.Row="1" Stretch="Uniform">
                <Canvas Name="playArea"  Width="600" Height="400">
                    <Image Canvas.Left="-18" Canvas.Top="-53" Grid.Column="1" Source="/SpeechBasics-WPF;component/Images/sky.png" Stretch="None" Height="507" Width="638" />
                    <Canvas Name="car" Width="126" Height="244" Canvas.Top="20">
                        <Canvas.RenderTransform>
                            <TransformGroup>
                                <TranslateTransform X="-49.5285" Y="-64.4295"/>
                                <RotateTransform Angle="0" x:Name="carRotation"/>
                                <TranslateTransform x:Name="carTranslation" X="300" Y="200"/>
                            </TransformGroup>
                        </Canvas.RenderTransform>
                        <Image Source="/SpeechBasics-WPF;component/Images/car.png" Stretch="Fill" Canvas.Left="17" Canvas.Top="-18" Height="192" Width="120" HorizontalAlignment="Center" VerticalAlignment="Stretch" />
                    </Canvas>
                </Canvas>
            </Viewbox>
            <TextBlock Grid.Row="2" HorizontalAlignment="Center" FontSize="16" Margin="0 10 0 10" Foreground="{StaticResource MediumGreyBrush}">
                Say: "<Span Name="forleftSpan">Northwest</Span>", "<Span Name="forrightSpan">Catright</Span>","<Span Name="forwardSpan">Forward</Span>", "<Span Name="backSpan">Back</Span>", "<Span Name="leftSpan">To the Left</Span>" or "<Span Name="rightSpan">Go to East</Span>"
            </TextBlock>
            <StatusBar Grid.Row="3" Height="23" HorizontalAlignment="Stretch" Name="statusBar" VerticalAlignment="Bottom" Background="White" Foreground="{StaticResource MediumGreyBrush}">
                <StatusBarItem Padding="0">
                    <TextBlock Name="statusBarText"></TextBlock>
                </StatusBarItem>
            </StatusBar>
           
            <TextBlock Margin="0,0,-64,0" HorizontalAlignment="Right" VerticalAlignment="Bottom" Foreground="{StaticResource MediumGreyBrush}" FontFamily="Segoe UI" FontSize="18" Text=" Paparazzi" Height="23" Width="152"></TextBlock>
        </Grid>
    </Window>
        
      

  13.   

    <Window x:Class="Microsoft.Samples.Kinect.SpeechBasics.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Speeh Basics" Height="600" Width="640"
            Loaded="WindowLoaded" Closing="WindowClosing">
        <Window.Resources>
            <SolidColorBrush x:Key="MediumGreyBrush" Color="#ff6e6e6e"/>
            <SolidColorBrush x:Key="LightGreyBrush" Color="#ffd2d2d2"/>
            <SolidColorBrush x:Key="KinectPurpleBrush" Color="#ff52318f"/>
            <Style TargetType="{x:Type Image}">
                <Setter Property="SnapsToDevicePixels" Value="True"/>
            </Style>
            <Storyboard x:Key="Move" RepeatBehavior="Forever" Timeline.DesiredFrameRate="30">
               <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="Car">
                    <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                    <EasingDoubleKeyFrame KeyTime="0:0:15" Value="100"/>
                </DoubleAnimationUsingKeyFrames>
             </Storyboard>
        </Window.Resources>
        <Window.Triggers>
            <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                <BeginStoryboard Storyboard="{StaticResource Move}"/>
            </EventTrigger>
        </Window.Triggers>
        <Grid Margin="10 0 10 0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <DockPanel Height="77" Margin="0,-10,0,0">
                <Image Source="file:///G:\已修改的成功版\SpeechBasics-WPF\Images\xiaohui.png" Stretch="Fill" Height="55" Width="137" />
            </DockPanel>
            <Viewbox Grid.Row="1" Stretch="Uniform">
                <Canvas Name="playArea"  Width="600" Height="400">
                    <Image Canvas.Left="-18" Canvas.Top="-53" Grid.Column="1" Source="/SpeechBasics-WPF;component/Images/sky.png" Stretch="None" Height="507" Width="638" />
                    <Canvas Width="126" Height="244" Canvas.Top="20">
                        <Canvas.RenderTransform>
                            <TransformGroup>
                                <TranslateTransform X="-49.5285" Y="-64.4295"/>
                                <RotateTransform Angle="0" x:Name="carRotation"/>
                                <TranslateTransform x:Name="carTranslation" X="300" Y="200"/>
                            </TransformGroup>
                        </Canvas.RenderTransform>
                        <Image Name="Car" Source="/SpeechBasics-WPF;component/Images/car.png" Stretch="Fill" Canvas.Left="17" Canvas.Top="-18" Height="192" Width="120" HorizontalAlignment="Center" VerticalAlignment="Stretch" >
                         <Image.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform/>
                                <SkewTransform/>
                                <RotateTransform/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </Image.RenderTransform>
                        </Image>
                    </Canvas>
                </Canvas>
            </Viewbox>
            <TextBlock Grid.Row="2" HorizontalAlignment="Center" FontSize="16" Margin="0 10 0 10" Foreground="{StaticResource MediumGreyBrush}">
                Say: "<Span Name="forleftSpan">Northwest</Span>", "<Span Name="forrightSpan">Catright</Span>","<Span Name="forwardSpan">Forward</Span>", "<Span Name="backSpan">Back</Span>", "<Span Name="leftSpan">To the Left</Span>" or "<Span Name="rightSpan">Go to East</Span>"
            </TextBlock>
            <StatusBar Grid.Row="3" Height="23" HorizontalAlignment="Stretch" Name="statusBar" VerticalAlignment="Bottom" Background="White" Foreground="{StaticResource MediumGreyBrush}">
                <StatusBarItem Padding="0">
                    <TextBlock Name="statusBarText"></TextBlock>
                </StatusBarItem>
            </StatusBar>
            
            <TextBlock Margin="0,0,-64,0" HorizontalAlignment="Right" VerticalAlignment="Bottom" Foreground="{StaticResource MediumGreyBrush}" FontFamily="Segoe UI" FontSize="18" Text=" Paparazzi" Height="23" Width="152"></TextBlock>
        </Grid>
    </Window>
      

  14.   

    +1  wpf里面有线性动画 直接xaml就搞定了
      

  15.   


    手动添加Timer例子
    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Timer timer = new Timer();
                timer.Interval = 100;
                timer.Tick += new EventHandler(timer_Tick);
                timer.Start();
            }        void timer_Tick(object sender, EventArgs e)
            {
                this.Text += "1";
            }    }