■ DoubleAnimation 클래스를 사용해 동심원 애니메이션을 만드는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 |
<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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
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 } } |