Wpf一个简单的物体移动动画
[htML]
Window x:Class="Wpfdemo1.MainWindow"
XMlns="https://schemas.microsoft.COM/winfx/2006/xaml/PResentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
TITle="mainWindow" Height="350" Width="525" Loaded="Window_Loaded" MouseLeftButtonDown="Window_MouseLeftButtonDown">
Canvas x:Name="body">
/Canvas>
/Window>
[csharp]
/// summary>
/// MainWindow.xaml 的交互逻辑
/// /summary>
public partial class MainWindow : Window
{
Ellipse ell;
public MainWindow()
{
Initializecomponent();
ell = new Ellipse();
ell.Fill = new SolIDColorbrush(Colors.red);
ell.Width = 50;
ell.Height = 50;
body.Children.Add(ell);
Canvas.SetLeft(ell, 100);
Canvas.SetTop(ell,100);
}
private void Window_Loaded(object sender, RoutedEventargs e)
{
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
moveTo(e.GetPosition(body));
}
private void moveTo(Point deskPoint)
{
//Point p = e.GetPosition(body);
Point curPoint = new Point();
curPoint.X = Canvas.GetLeft(ell);
curPoint.Y = Canvas.GetTop(ell);
double _s = System.Math.Sqrt(Math.Pow((deskPoint.X - curPoint.X), 2) + Math.Pow((deskPoint.Y - curPoint.Y), 2));
double _secNumber = (_s / 1000) * 500;
Storyboard storyboard = new Storyboard();
//创建X轴方向动画
DoubleAnimation doubleAnimation = new DoubleAnimation(
Canvas.GetLeft(ell),
deskPoint.X,
new Duration(TimeSpan.FromMilliseconds(_secNumber))
);
Storyboard.SetTarget(doubleAnimation, ell);
Storyboard.SetTargetProperty(doubleAnimation, new ProPErtyPath("(Canvas.Left)"));
storyboard.Children.Add(doubleAnimation);
//创建Y轴方向动画
doubleAnimation = new DoubleAnimation(
Canvas.GetTop(ell),
deskPoint.Y,
new Duration(TimeSpan.FromMilliseconds(_secNumber))
);
Storyboard.SetTarget(doubleAnimation, ell);
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Top)"));
storyboard.Children.Add(doubleAnimation);
//动画播放
storyboard.Begin();
}
}
通过动画析storyboard实现元素移动功能。
[html]
Window x:Class="Wpfdemo1.MainWindow"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" MouseLeftButtonDown="Window_MouseLeftButtonDown">
Canvas x:Name="body">
/Canvas>
/Window>
[csharp]
/// summary>
/// MainWindow.xaml 的交互逻辑
/// /summary>
public partial class MainWindow : Window
{
Ellipse ell;
public MainWindow()
{
InitializeComponent();
ell = new Ellipse();
ell.Fill = new SolidColorBrush(Colors.Red);
ell.Width = 50;
ell.Height = 50;
body.Children.Add(ell);
Canvas.SetLeft(ell, 100);
Canvas.SetTop(ell,100);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
moveTo(e.GetPosition(body));
}
private void moveTo(Point deskPoint)
{
//Point p = e.GetPosition(body);
Point curPoint = new Point();
curPoint.X = Canvas.GetLeft(ell);
curPoint.Y = Canvas.GetTop(ell);
double _s = System.Math.Sqrt(Math.Pow((deskPoint.X - curPoint.X), 2) + Math.Pow((deskPoint.Y - curPoint.Y), 2));
double _secNumber = (_s / 1000) * 500;
Storyboard storyboard = new Storyboard();
//创建X轴方向动画
DoubleAnimation doubleAnimation = new DoubleAnimation(
Canvas.GetLeft(ell),
deskPoint.X,
new Duration(TimeSpan.FromMilliseconds(_secNumber))
);
Storyboard.SetTarget(doubleAnimation, ell);
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Left)"));
storyboard.Children.Add(doubleAnimation);
//创建Y轴方向动画
doubleAnimation = new DoubleAnimation(
Canvas.GetTop(ell),
deskPoint.Y,
new Duration(TimeSpan.FromMilliseconds(_secNumber))
);
Storyboard.SetTarget(doubleAnimation, ell);
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Top)"));
storyboard.Children.Add(doubleAnimation);
//动画播放
storyboard.Begin();
}
}
通过动画析storyboard实现元素移动功能。
觉得可用,就经常来吧! 欢迎评论哦! html5教程,巧夺天工,精雕玉琢。小宝典献丑了!
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Wpf一个简单的物体移动动画
本文地址: https://pptw.com/jishu/586502.html