首页前端开发HTMLWpf一个简单的物体移动动画

Wpf一个简单的物体移动动画

时间2024-01-25 12:14:36发布访客分类HTML浏览407
导读:收集整理的这篇文章主要介绍了html5教程-Wpf一个简单的物体移动动画,觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 [htML] <Wi...
收集整理的这篇文章主要介绍了html5教程-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核实处理,我们将尽快回复您,谢谢合作!

ClassdivHTMLpost-format-galleryProp

若转载请注明出处: Wpf一个简单的物体移动动画
本文地址: https://pptw.com/jishu/586502.html
Google App Engine (Java + String + Velocity)数据访问调试,出现错误 Cannot find class [javax.naming.Context] HTML5游戏开发---弹跳球

游客 回复需填写必要信息