■ CompositionColorGradientStop 클래스의 StartAnimation 메소드를 사용해 브러시 애니메이션을 시작하는 방법을 보여준다.
▶ 예제 코드 (C#)
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 |
using System; using System.Numerics; using Microsoft.UI; using Microsoft.UI.Composition; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Hosting; // ... Grid grid = null; // ... Visual gridVisual = ElementCompositionPreview.GetElementVisual(grid); Compositor compositor = gridVisual.Compositor; SpriteVisual spriteVisual = compositor.CreateSpriteVisual(); spriteVisual.Size = new Vector2((float)grid.ActualWidth, (float)grid.ActualHeight); ElementCompositionPreview.SetElementChildVisual(grid, spriteVisual); CompositionLinearGradientBrush compositionLinearGradientBrush = compositor.CreateLinearGradientBrush(); compositionLinearGradientBrush.StartPoint = new Vector2(0, 0); compositionLinearGradientBrush.EndPoint = new Vector2(1, 1); CompositionColorGradientStop compositionColorGradientStop1 = compositor.CreateColorGradientStop(0.0f, Colors.Transparent); CompositionColorGradientStop compositionColorGradientStop2 = compositor.CreateColorGradientStop(1.0f, Colors.Blue ); compositionLinearGradientBrush.ColorStops.Add(compositionColorGradientStop1); compositionLinearGradientBrush.ColorStops.Add(compositionColorGradientStop2); spriteVisual.Brush = compositionLinearGradientBrush; ScalarKeyFrameAnimation offsetScalarKeyFrameAnimation = compositor.CreateScalarKeyFrameAnimation(); offsetScalarKeyFrameAnimation.InsertKeyFrame(0.0f, -1.0f); offsetScalarKeyFrameAnimation.InsertKeyFrame(1.0f, 1.0f); offsetScalarKeyFrameAnimation.IterationBehavior = AnimationIterationBehavior.Forever; offsetScalarKeyFrameAnimation.Duration = TimeSpan.FromSeconds(1.5); compositionColorGradientStop1.StartAnimation("Offset", offsetScalarKeyFrameAnimation); compositionColorGradientStop2.StartAnimation("Offset", offsetScalarKeyFrameAnimation); |