桌面下雪效果

桌面下雪效果桌面下雪效果 原文:桌面下雪效果一个简单的下雪效果MainWindow:1.主界面通过DispatcherTimer给Grid生成雪花2.雪花飘落后再讲雪花从Grid容器中移除publicpartialclassMainWindow{publicMainWindow()…

原文:
桌面下雪效果

一个简单的下雪效果

MainWindow:

1.主界面通过DispatcherTimer给Grid生成雪花

2.雪花飘落后再讲雪花从Grid容器中移除

 public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            Closing += (s, e) => ViewModelLocator.Cleanup();
            Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var timer = new DispatcherTimer
            {
                Interval = TimeSpan.FromMilliseconds(100)
            };
            timer.Tick += timer_Tick;
            timer.Start();
        }

        //添加雪花
        private void timer_Tick(object sender, EventArgs e)
        {
            LayoutRoot.Children.Add(CreateSnowFlower());
        }

        //生成雪花
        private static SnowFlower CreateSnowFlower()
        {
            var snowFlower = new SnowFlower();

            snowFlower.Complete += args =>
            {
                //将雪花从父容器中移除
                var flower = args as SnowFlower;
                if (flower == null) return;
                var parent = VisualTreeHelper.GetParent(flower);
                var panel = parent as Panel;
                if (panel != null && panel.Children.Contains(flower))
                {
                    panel.Children.Remove(flower);
                }
            };

            return snowFlower;
        }
    }

SnowFlower

1.雪花的飘落动画以及旋转动画

2.增加动画完成事件

3.随机的起点

4.随机的旋转速度以及下落速度

  /// <summary>
    /// 雪花
    /// </summary>
    public class SnowFlower : Image
    {

        #region 字段

        //完全随机数生成器
        private readonly Random _random = RandomHelper.GetRandom();

        #endregion

        #region 属性

        /// <summary>
        /// 下落速度
        /// </summary>
        public int GravitySpeed
        {
            get { return _random.Next(5, 10); }
        }

        /// <summary>
        /// 旋转速度
        /// </summary>
        public int AngleSpeed
        {
            get { return _random.Next(1, 5); }
        }

        /// <summary>
        /// 起始的X坐标
        /// </summary>
        public int StartX
        {
            get
            {
                return _random.Next(0, (int)SystemParameters.PrimaryScreenWidth);
            }
        }


        #endregion

        public SnowFlower()
        {
            Opacity = GetOpacity();
            Source = GetSource();
            Height = GetSize();
            Width = Height;
            RenderTransformOrigin = new Point(0.5, 0.5);
            Margin = new Thickness(StartX, 0, 0, 0);
            HorizontalAlignment = HorizontalAlignment.Left;
            VerticalAlignment = VerticalAlignment.Top;
            RenderTransform = GetRenderTransform();
            Loaded += SnowFlower_Loaded;
        }

        #region 动画

        private void SnowFlower_Loaded(object sender, RoutedEventArgs e)
        {
            GetFallingStoryborad().Begin();
        }

        //获取变换的信息
        private static TransformGroup GetRenderTransform()
        {
            var result = new TransformGroup();
            result.Children.Add(new RotateTransform());
            result.Children.Add(new TranslateTransform());
            return result;
        }

        //获取飘落动画故事版
        private Storyboard GetFallingStoryborad()
        {
            var result = new Storyboard();

            //加入旋转动画
            result.Children.Add(GetAngleAnimation());

            //加入掉落动画
            var fallingAnimation = GetFallingAnimation();
            result.Duration = fallingAnimation.Duration;
            //当掉落完成之后触发完成事件
            result.Completed += (sender, args) => RaiseComplete();
            result.Children.Add(fallingAnimation);

            return result;
        }

        //属性链
        private readonly object[] _propertyChain = {
                RenderTransformProperty,
                TransformGroup.ChildrenProperty,
                RotateTransform.AngleProperty,
                TranslateTransform.YProperty
            };

        //获取旋转动画
        private DoubleAnimation GetAngleAnimation()
        {
            var da = new DoubleAnimation
            {
                From = 0,
                To = 360,
                Duration = TimeSpan.FromSeconds(AngleSpeed),
                RepeatBehavior = RepeatBehavior.Forever
            };
            //Storyboard.SetTargetProperty(da, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(RotateTransform.Angle)"));
            Storyboard.SetTargetProperty(da, new PropertyPath("(0).(1)[0].(2)", _propertyChain));
            Storyboard.SetTarget(da, this);
            return da;
        }

        //获取飘落动画
        private DoubleAnimation GetFallingAnimation()
        {
            var da = new DoubleAnimation
            {
                From = 0,
                To = SystemParameters.PrimaryScreenHeight,
                Duration = TimeSpan.FromSeconds(GravitySpeed)
            };

            Storyboard.SetTargetProperty(da, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[1].(TranslateTransform.Y)"));
            Storyboard.SetTarget(da, this);
            return da;
        }

        #endregion

        //获取初始的透明度
        private static double GetOpacity()
        {
            var result = (double)new Random().Next(1, 5);
            result = result / 10;
            return result;
        }

        //获取图像源
        private static BitmapImage GetSource()
        {
            var result = new BitmapImage();
            result.BeginInit();
            result.UriSource = new Uri("/SnowEffect;component/Resource/SnowFlower.png", UriKind.RelativeOrAbsolute);
            result.EndInit();
            return result;
        }

        //获取尺寸
        private static double GetSize()
        {
            var result = (double)new Random().Next(20, 50);
            return result;
        }

        #region 完成事件

        /// <summary>
        /// 完成处理委托
        /// </summary>
        /// <param name="sender"></param>
        public delegate void CompleteHandler(object sender);

        /// <summary>
        /// 完成事件
        /// </summary>
        public event CompleteHandler Complete;

        /// <summary>
        /// 完成事件触发时的操作
        /// </summary>
        protected virtual void OnComplete()
        {
            if (Complete != null)
            {
                Complete(this);
            }
        }

        /// <summary>
        /// 引发完成事件
        /// </summary>
        public void RaiseComplete()
        {
            OnComplete();
        }

        #endregion
    }

 

源码

posted on
2019-04-18 11:13
NET未来之路 阅读(
) 评论(
)
编辑
收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/10728474.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/37522.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注