阅读 127

WPF 之动画(十二)

WPF 中的动画主要分为 AnimationTimeline(简单动画) 和 Storyboard(一组协同的动画)。

一、简单线性动画

        
        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            DoubleAnimation dx = new DoubleAnimation();
            DoubleAnimation dy = new DoubleAnimation();

            // 指定起点
            dx.From = 0;
            dy.From = 0;

            // 指定终点
            dx.To = new Random().NextDouble() * 200;
            dy.To = new Random().NextDouble() * 200;

            //// 指定幅度
            //dx.By = 100;
            //dy.By = 100;

            // 指定时长
            dx.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
            dy.Duration = new Duration(TimeSpan.FromMilliseconds(2000));

            // 动画主体(TranslateTransform)
            tt.BeginAnimation(TranslateTransform.XProperty,dx);
            tt.BeginAnimation(TranslateTransform.YProperty,dy);
        }

二、高级动画控制

        
        
        private void ButtonBase_OnClick1(object sender, RoutedEventArgs e)
        {
           DoubleAnimation dx=new DoubleAnimation();
           DoubleAnimation dy=new DoubleAnimation();

            // 设置弹跳
            BounceEase be=new BounceEase()
            {
                Bounces = 4, // 弹跳次数
                Bounciness = 2,// 弹跳值
            };
            dy.EasingFunction = be;

            // 设置终点
            dx.To = 300;
            dy.To = 300;

            // 设置时长
            dx.Duration = new Duration(TimeSpan.FromMilliseconds(2000));
           dy.Duration = new Duration(TimeSpan.FromMilliseconds(2000));

            // 动画主体
            tt1.BeginAnimation(TranslateTransform.XProperty,dx);
           tt1.BeginAnimation(TranslateTransform.YProperty, dy);
        }

三、关键帧动画

        
        
        
        
        private void ButtonBase_OnClick2(object sender, RoutedEventArgs e)
        {
            PathGeometry path=this.FindResource("MovePath") as PathGeometry;
            Duration duration =new Duration(TimeSpan.FromMilliseconds(3000));

            // 创建动画
            DoubleAnimationUsingPath dx=new DoubleAnimationUsingPath()
            {
                PathGeometry = path,
                Source = PathAnimationSource.X,
                Duration = duration,
            };

            DoubleAnimationUsingPath dy = new DoubleAnimationUsingPath()
            {
                PathGeometry = path,
                Source = PathAnimationSource.Y,
                Duration = duration,
            };

            // 执行动画
            tt2.BeginAnimation(TranslateTransform.XProperty,dx);
            tt2.BeginAnimation(TranslateTransform.YProperty, dy);
        }

四、场景——Storyboard

例如:实现三个小球(红球、绿球、蓝球)分别沿着跑到运动的动画,具体示例如下:

     
        
            
            
        
        
            
            
            
        

        
            
                
                    
                
            
        

        
            
                
                    
                
            
        

        
            
                
                    
                
            
        

        
    

原文:https://www.cnblogs.com/dongweian/p/14657382.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐