Skip to content

Latest commit

 

History

History
74 lines (41 loc) · 6.11 KB

coloranimation.md

File metadata and controls

74 lines (41 loc) · 6.11 KB
-api-id -api-type
T:Windows.UI.Xaml.Media.Animation.ColorAnimation
winrt class

Windows.UI.Xaml.Media.Animation.ColorAnimation

-description

Animates the value of a Color property between two target values using linear interpolation over a specified Duration.

-xaml-syntax

<ColorAnimation .../>

-remarks

Use ColorAnimation to animate the property value of any dependency property that is of type Color.

Linear interpolation for a Color means that each of the ARGB values is treated as a byte and the interpolation is simply a mathematical operation. You get best results from color interpolation if at least one of the RGB components is the same or close to the same in both the starting value and ending value.

You usually need to use indirect property targeting in order to target a sub-property of another object that's the value of a property on the target. This is because very few properties that display color information in UI elements are actually of type Color. Most are instead of type Brush. To use ColorAnimation on UI elements, you typically are targeting the Color property of a SolidColorBrush that's the sub-property value. Syntax for this is shown in the XAML example in the "Examples" section. For more info on indirect property targeting and other storyboarded animation concepts, see Storyboarded animations or Property-path syntax.

A ColorAnimation typically has at least one of the From, By or To properties set, but never all three.

  • From only: The animation progresses from the value specified by the From property to the base value of the property being animated.
  • From and To: The animation progresses from the value specified by the From property to the value specified by the To property.
  • From and By: The animation progresses from the value specified by the From property to the value specified by the sum of the From and By properties.
  • To only: The animation progresses from the animated property's base value or a previous animation's output value to the value specified by the To property.
  • By only: The animation progresses from the base value of the property being animated or a previous animation's output value to the sum of that value and the value specified by the By property.

The From, By and To properties of a ColorAnimation aren't strictly a Color. Instead these are a Nullable for Color. The default value for these is null, not an uninitialized structure. That null value is how the animation system distinguishes that you haven't specifically set a value. Visual C++ component extensions (C++/CX) doesn't have a Nullable type, so it uses IReference instead.

-examples

The following example shows how to use ColorAnimation to animate the background color of a StackPanel.

[!code-xamlColoranimation]

[!code-xamlColoranimation]

[!code-xamlColoranimation]

[!code-csharpColoranimation_cs]

[!code-vbColoranimation_cs]

Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"

Alternatively, you could explicitly create the SolidColorBrush, name it, and target its Color property directly. The example below shows how to create the same animation as the previous one except it uses direct property targeting.

[!code-xamlColoranimation_direct_targeting]

[!code-csharpColoranimation_direct_targeting_cs]

[!code-vbColoranimation_direct_targeting_cs]

-see-also

Storyboarded animations, XAML animation sample, Timeline, SolidColorBrush, LinearGradientBrush, Color, Colors, LinearGradientBrush, Color, Colors