■ AnimationTimeline 클래스를 사용해 커스텀 브러시 애니메이션을 만드는 방법을 보여준다.
▶ BrushAnimation.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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; namespace TestProject { /// <summary> /// 브러시 애니메이션 /// </summary> public class BrushAnimation : AnimationTimeline { //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Easing 함수 속성 - EasingFunctionProperty /// <summary> /// Easing 함수 속성 /// </summary> public static readonly DependencyProperty EasingFunctionProperty = DependencyProperty.Register ( "EasingFunction", typeof(IEasingFunction), typeof(BrushAnimation), new PropertyMetadata(null) ); #endregion #region FROM 속성 - FromProperty /// <summary> /// FROM 속성 /// </summary> public static readonly DependencyProperty FromProperty = DependencyProperty.Register ( "From", typeof(Brush), typeof(BrushAnimation), new PropertyMetadata(null) ); #endregion #region To 속성 - ToProperty /// <summary> /// To 속성 /// </summary> public static readonly DependencyProperty ToProperty = DependencyProperty.Register ( "To", typeof(Brush), typeof(BrushAnimation), new PropertyMetadata(null) ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 타겟 속성 타입 - TargetPropertyType /// <summary> /// 타겟 속성 타입 /// </summary> public override Type TargetPropertyType => typeof(Brush); #endregion #region Easing 함수 - EasingFunction /// <summary> /// Easing 함수 /// </summary> public IEasingFunction EasingFunction { get { return (IEasingFunction)GetValue(EasingFunctionProperty); } set { SetValue(EasingFunctionProperty, value); } } #endregion #region From - From /// <summary> /// From /// </summary> public Brush From { get { return (Brush)GetValue(FromProperty); } set { SetValue(FromProperty, value); } } #endregion #region To - To /// <summary> /// To /// </summary> public Brush To { get { return (Brush)GetValue(ToProperty); } set { SetValue(ToProperty, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 현재 값 구하기 - GetCurrentValue(defaultOriginValue, defaultDestinationValue, animationClock) /// <summary> /// 현재 값 구하기 /// </summary> /// <param name="defaultOriginValue">디폴트 초기 값</param> /// <param name="defaultDestinationValue">디폴트 목적 값</param> /// <param name="animationClock">애니메이션 클럭</param> /// <returns>현재 값</returns> public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock) { if(!animationClock.CurrentProgress.HasValue) { return Brushes.Transparent; } Brush originValue = From ?? defaultOriginValue as Brush; Brush destinationValue = To ?? defaultDestinationValue as Brush; double progress = animationClock.CurrentProgress.Value; if(progress == 0) { return originValue; } if(progress == 1) { return destinationValue; } IEasingFunction easingFunction = EasingFunction; if(easingFunction != null) { progress = easingFunction.Ease(progress); } return new VisualBrush ( new Border() { Width = 1, Height = 1, Background = originValue, Child = new Border() { Background = destinationValue, Opacity = progress, } } ); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 인스턴스 생성하기 (코어) - CreateInstanceCore() /// <summary> /// 인스턴스 생성하기 (코어) /// </summary> /// <returns>인스턴스</returns> protected override Freezable CreateInstanceCore() { return new BrushAnimation(); } #endregion } } |
▶ MainWindow.xaml
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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="AnimationTimeline 클래스 : 커스텀 브러시 애니메이션 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Rectangle Name="rectangle" Width="300" Height="300" Fill="Gold" /> </Grid> <Window.Triggers> <EventTrigger RoutedEvent="Window.Loaded"> <BeginStoryboard> <Storyboard> <local:BrushAnimation Storyboard.TargetName="rectangle" Storyboard.TargetProperty="Fill" RepeatBehavior="Forever" AutoReverse="True" Duration="00:00:01" From="Gold" To="Blue" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Window.Triggers> </Window> |