■ DoubleAnimation 클래스 : 동심원 애니메이션 만들기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainWindow.xaml

 

 

<Window x:Class="TestProject.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Width="800"

    Height="600"

    Title="DoubleAnimation 클래스 : 동심원 애니메이션 만들기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Canvas Name="canvas" Background="#ffe0e0e0" />

</Window>

 

 

MainWindow.xaml.cs

 

 

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 난수기

        /// </summary>

        private Random random;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            this.random = new Random(DateTime.Now.Millisecond);

 

            Loaded += Window_Loaded;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)

 

        /// <summary>

        /// 윈도우 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            CreateCircle();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 원 생성하기 - CreateCircle()

 

        /// <summary>

        /// 원 생성하기

        /// </summary>

        private void CreateCircle()

        {

            double centerX = this.canvas.ActualWidth  / 2.0;

            double centerY = this.canvas.ActualHeight / 2.0;

 

            Color[] colorArray = new Color[] { Colors.White, Colors.Green, Colors.Green, Colors.Lime };

 

            for(int i = 0; i < 24; i++)

            {

                Ellipse ellipse = new Ellipse();

 

                byte alpha      = (byte)this.random.Next(96,192);

                int  colorIndex = this.random.Next(4);

 

                ellipse.Stroke = new SolidColorBrush

                (

                    Color.FromArgb

                    (

                        alpha,

                        colorArray[colorIndex].R,

                        colorArray[colorIndex].G,

                        colorArray[colorIndex].B

                    )

                );

 

                ellipse.StrokeThickness = this.random.Next(1, 4);

                ellipse.Width           = 0.0;

                ellipse.Height          = 0.0;

 

                double offsetX = 16 - this.random.Next(32);

                double offsetY = 16 - this.random.Next(32);

 

                this.canvas.Children.Add(ellipse);                

 

                ellipse.SetValue(Canvas.LeftProperty, centerX + offsetX);

                ellipse.SetValue(Canvas.TopProperty , centerY + offsetY);

 

                double duration = 6.0 + 10.0 * this.random.NextDouble();

                double delay    =       16.0 * this.random.NextDouble();

 

                TranslateTransform transform = new TranslateTransform();

                

                DoubleAnimation offsetXAnimation = new DoubleAnimation

                (

                    0.0,

                    -256.0,

                    new Duration(TimeSpan.FromSeconds(duration))

                );

 

                offsetXAnimation.RepeatBehavior = RepeatBehavior.Forever;

                offsetXAnimation.BeginTime      = TimeSpan.FromSeconds(delay);

 

                transform.BeginAnimation(TranslateTransform.XProperty, offsetXAnimation);

                transform.BeginAnimation(TranslateTransform.YProperty, offsetXAnimation);

 

                ellipse.RenderTransform = transform;

 

                DoubleAnimation sizeAnimation = new DoubleAnimation

                (

                    0.0,

                    512.0,

                    new Duration(TimeSpan.FromSeconds(duration))

                );

 

                sizeAnimation.RepeatBehavior = RepeatBehavior.Forever;

                sizeAnimation.BeginTime      = TimeSpan.FromSeconds(delay);

 

                ellipse.BeginAnimation(Ellipse.WidthProperty , sizeAnimation);

                ellipse.BeginAnimation(Ellipse.HeightProperty, sizeAnimation);

 

                DoubleAnimation opacityAnimation = new DoubleAnimation

                (

                    duration - 1.0,

                    0.0,

                    new Duration(TimeSpan.FromSeconds(duration))

                );

 

                opacityAnimation.BeginTime      = TimeSpan.FromSeconds(delay);

                opacityAnimation.RepeatBehavior = RepeatBehavior.Forever;

 

                ellipse.BeginAnimation(Ellipse.OpacityProperty, opacityAnimation);

            }

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요