첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
0.01MB

▶ BrushAnimation.cs

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
    }
}

 

728x90

 

▶ 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"
    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>
728x90
반응형
그리드형(광고전용)
Posted by 사용자 icodebroker

댓글을 달아 주세요