动画不适用于 Ellipse

Animation not work on Ellipse

我正在尝试制作一个椭圆的动画,它会随着状态变化而变大。 我似乎无法在宽度和高度上进行转换。

请注意,如果我将 TargetProperty 更改为 (FrameworkElement.RenderTransform).(CompositeTransform.TranslateX),则会应用转换。

这是我使用的 ControlTemplate:

<Border>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="ControlStates">
            <VisualStateGroup.Transitions>
                <VisualTransition x:Name="ClosedToOpened" 
                                      From="Closed" To="Opened"
                                      GeneratedDuration="0:0:0.5">
                    <Storyboard>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Width" Storyboard.TargetName="Bubble">
                            <EasingDoubleKeyFrame KeyTime="0:0:0" Value="10" EasingFunction="{StaticResource EaseOutQuintic}" />
                            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="50" EasingFunction="{StaticResource EaseOutQuintic}" />
                        </DoubleAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Height" Storyboard.TargetName="Bubble">
                            <EasingDoubleKeyFrame KeyTime="0:0:0" Value="10" EasingFunction="{StaticResource EaseOutQuintic}" />
                            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="50" EasingFunction="{StaticResource EaseOutQuintic}" />
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualTransition>
                <VisualTransition x:Name="OpenedToClosed" 
                                      From="Opened" To="Closed"
                                      GeneratedDuration="0:0:0.5">
                    <Storyboard>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Width" Storyboard.TargetName="Bubble">
                            <EasingDoubleKeyFrame KeyTime="0:0:0" Value="50" EasingFunction="{StaticResource EaseOutQuintic}" />
                            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="10" EasingFunction="{StaticResource EaseOutQuintic}" />
                        </DoubleAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Height" Storyboard.TargetName="Bubble">
                            <EasingDoubleKeyFrame KeyTime="0:0:0" Value="50" EasingFunction="{StaticResource EaseOutQuintic}" />
                            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="10" EasingFunction="{StaticResource EaseOutQuintic}" />
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualTransition>
            </VisualStateGroup.Transitions>
            <VisualStateGroup.States>
                <VisualState x:Name="Opened">
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="Bubble" Storyboard.TargetProperty="Width" Duration="0" To="50" />
                        <DoubleAnimation Storyboard.TargetName="Bubble" Storyboard.TargetProperty="Height" Duration="0" To="50" />
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Closed">
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="Bubble" Storyboard.TargetProperty="Width" Duration="0" To="10" />
                        <DoubleAnimation Storyboard.TargetName="Bubble" Storyboard.TargetProperty="Height" Duration="0" To="10" />
                    </Storyboard>
                </VisualState>
            </VisualStateGroup.States>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Ellipse x:Name="Bubble" Width="10" Height="10" Fill="Black" />
</Border>

如何让转换正常工作?

(我试过 ScaleX/Y 但它在动画时给出了像素 Ly 结果)

您的动画是依赖动画,默认情况下,动画系统不会运行依赖动画。要启用动画,您需要将动画对象的 EnableDependentAnimation 属性 设置为 True.

在WinRT中,有两种动画:Dependent and independent animations

An animation is independent if it has any of these characteristics:

  • The Duration of the animation is 0 seconds (see Caution)
  • The animation targets UIElement.Opacity
  • The animation targets a sub-property value of these UIElement properties: RenderTransform, Projection, Clip
  • The animation targets Canvas.Left or Canvas.Top
  • The animation targets a Brush value and uses a SolidColorBrush, animating its Color
  • The animation is an ObjectAnimationUsingKeyFrames

If your animation doesn't meet these criteria, it's probably a dependent animation.

要使您的转换正常进行,您可以更改代码,如下所示:

<Border>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="ControlStates">
            <VisualStateGroup.Transitions>
                <VisualTransition x:Name="ClosedToOpened"
                                  From="Closed"
                                  GeneratedDuration="0:0:0.5"
                                  To="Opened">
                    <Storyboard>
                        <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetName="Bubble" Storyboard.TargetProperty="Width">
                            <EasingDoubleKeyFrame EasingFunction="{StaticResource EaseOutQuintic}" KeyTime="0:0:0" Value="10" />
                            <EasingDoubleKeyFrame EasingFunction="{StaticResource EaseOutQuintic}" KeyTime="0:0:0.5" Value="50" />
                        </DoubleAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetName="Bubble" Storyboard.TargetProperty="Height">
                            <EasingDoubleKeyFrame EasingFunction="{StaticResource EaseOutQuintic}" KeyTime="0:0:0" Value="10" />
                            <EasingDoubleKeyFrame EasingFunction="{StaticResource EaseOutQuintic}" KeyTime="0:0:0.5" Value="50" />
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualTransition>
                <VisualTransition x:Name="OpenedToClosed"
                                  From="Opened"
                                  GeneratedDuration="0:0:0.5"
                                  To="Closed">
                    <Storyboard>
                        <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetName="Bubble" Storyboard.TargetProperty="Width">
                            <EasingDoubleKeyFrame EasingFunction="{StaticResource EaseOutQuintic}" KeyTime="0:0:0" Value="50" />
                            <EasingDoubleKeyFrame EasingFunction="{StaticResource EaseOutQuintic}" KeyTime="0:0:0.5" Value="10" />
                        </DoubleAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetName="Bubble" Storyboard.TargetProperty="Height">
                            <EasingDoubleKeyFrame EasingFunction="{StaticResource EaseOutQuintic}" KeyTime="0:0:0" Value="50" />
                            <EasingDoubleKeyFrame EasingFunction="{StaticResource EaseOutQuintic}" KeyTime="0:0:0.5" Value="10" />
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualTransition>
            </VisualStateGroup.Transitions>
            ...
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    ...
 </Border>