我现在在做一个C#开发,用的是VS2010WPF编的,现在要实现一辆小汽车由开始到结束一直做着直线运动,想问问应该通过什么方法实现,求各位大神帮帮忙。
PS:因为时间比较紧迫,希望能得到代码一份,谢谢。c#wpf直线运动
PS:因为时间比较紧迫,希望能得到代码一份,谢谢。c#wpf直线运动
解决方案 »
- 将DataRow添加到DataTable中报错
- windows Mobile 5 上 datagrid 主从表显示问题:从表显示了所有记录。
- 怎样停止一个正在执行的RESPONSE.REDIRECT进程 C#.NET
- 有没有什么方法可以暂时暂停timer
- iis5.0如何设置默认上传文件大小
- 怎么从b/s到c/s啊
- 求N(N>=0)个元素的所有子集
- 假如用代理上服务器,怎么测出他的外网IP地址呀(相当于外网IP访问内网IP)用C#
- 如何定义一个sqlconnection类,可以让该namespace下的其他类可以访问
- 用filestream读取文件的时候为什么显示很多异常?
- 如何自定义一个图片类
- Comet代码在VS下能正常,IIS下不能通信?什么原因?
请问,timer控件应该怎么插入?请明示。是新建一个类吗?还是怎么办啊?
求助
<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>
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();
可以设置该控件的属性..主要是设置它多长时间运行一次.
事件里面的内容就是每次获取小汽车的位置..然后在原有X点的坐标值上加上一个你需要移动的距离.
就可以实现小汽车直线运动了.!
您好,为什么我这边的工具箱里找不到timer这个控件呢?
用你这段代码时运行出错了,整了好久。话说我把起始点值、目标点值、图片存储位置都改了的啊。要不我把我的代码给你看下吧。你看看应该把它们插入哪个地方。您看看。
<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>
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>
手动添加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";
} }