How can I animate an image along a circular path in WPF?
I need to create a rather complex animation involving images in WPF where an image has to move along a circular path. Here is a picture of what it looks like...
Here is my xaml:
<ControlTemplate x:Key="ValveStyle" TargetType="{x:Type CheckBox}">
<ControlTemplate.Resources>
<PathGeometry x:Key="CirclePathGeometry" PresentationOptions:Freeze="True">
<PathGeometry.Figures>
<PathFigureCollection>
<PathFigure StartPoint="340,120">
<PathFigure.Segments>
<PathSegmentCollection>
<ArcSegment Size="100,50" RotationAngle="0" IsLargeArc="False" SweepDirection="CounterClockwise"
Point="-30,120" />
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
</PathFigureCollection>
</PathGeometry.Figures>
</PathGeometry>
</ControlTemplate.Resources>
<Viewbox Stretch="Uniform">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CheckStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="00:00:00.2000000" />
</VisualStateGroup.Transitions>
<VisualState x:Name="Unchecked" />
<VisualState x:Name="Checked">
<Storyboard>
<!--DoubleAnimationUsingPath BeginTime="00:00:00" Storyboard.TargetName="image3" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.开发者_如何学JAVAChildren)[3].(TranslateTransform.X)" Source="X"
DoubleAnimationUsingPath.PathGeometry="{StaticResource CirclePathGeometry}" />
<DoubleAnimationUsingPath BeginTime="00:00:00" Storyboard.TargetName="image3" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Source="Y"
DoubleAnimationUsingPath.PathGeometry="{StaticResource CirclePathGeometry}" /-->
<MatrixAnimationUsingPath Storyboard.TargetName="Image3MatrixTransform" Storyboard.TargetProperty="Matrix"
MatrixAnimationUsingPath.PathGeometry="{StaticResource CirclePathGeometry}" />
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000"
Storyboard.TargetName="image1"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="-90" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000"
Storyboard.TargetName="image2"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="-90" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000"
Storyboard.TargetName="image"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="-90" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<VisualStateManager.CustomVisualStateManager>
<ic:ExtendedVisualStateManager />
</VisualStateManager.CustomVisualStateManager>
<Canvas Width="685" Height="527">
<Image Source="/VT;component/Images/Valve121.1-Base.png" Stretch="Fill" />
<Image x:Name="image" Source="/VT;component/Images/Valve121.1-LeftBar1.png" Stretch="Fill"
Canvas.Left="61.991" Canvas.Top="49.329" RenderTransformOrigin="0.286,0.5428">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform />
<SkewTransform />
<RotateTransform />
<TranslateTransform />
</TransformGroup>
</Image.RenderTransform>
</Image>
<Image x:Name="image2" Source="/VT;component/Images/Valve121.1-RightBar.png" Stretch="Fill"
Canvas.Left="436.491" Canvas.Top="49.388" RenderTransformOrigin="0.249,0.756">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform />
<SkewTransform />
<RotateTransform />
<TranslateTransform />
</TransformGroup>
</Image.RenderTransform>
</Image>
<Image x:Name="image3" Source="/VT;component/Images/Valve121.1-HzBar.png" Stretch="Fill"
Canvas.Left="277.491" Canvas.Top="58.345">
<Image.RenderTransform>
<MatrixTransform x:Name="Image3MatrixTransform" />
</Image.RenderTransform>
</Image>
<Image Source="/VT;component/Images/Valve121.1-Overlay.png" Stretch="Fill" />
<Image x:Name="image1" Source="/VT;component/Images/Valve121.1-LeftBar2.png" Stretch="Fill"
Canvas.Left="129.824" Canvas.Top="24.661" RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform />
<SkewTransform />
<RotateTransform />
<TranslateTransform />
</TransformGroup>
</Image.RenderTransform>
</Image>
<Path Stroke="Blue" StrokeThickness="10" RenderTransformOrigin="-0.333,-0.227">
<Path.Data>
<PathGeometry>
<PathFigureCollection>
<PathFigure StartPoint="340,120">
<PathFigure.Segments>
<PathSegmentCollection>
<ArcSegment Size="100,50" RotationAngle="0" IsLargeArc="False" SweepDirection="CounterClockwise"
Point="-30,120" />
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
</PathFigureCollection>
</PathGeometry>
</Path.Data>
</Path>
</Canvas>
</Viewbox>
</ControlTemplate>
I'm facing several problems:
- The image (image3) doesn't seem to move correctly along the path I have set, so I guess I made a mistake there...
- The animation of the image (image3) starts after all other animation are done. I would like that all animations start at the same time.
I've tried both DoubleAnimationUsingPath
and MatrixAnimationUsingPath
but none of them worked the way I want. Is it even possible to mix Key Frames animation and Path animations ?
Is it possible to do such an animation in WPF? If so, how can I achieve this?
Define your MainWindow
(or Page
) class as follows:
<Window x:Class="TestCustomTab.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<PathGeometry x:Key="path" Figures="M 0,50 A 50,50 0 1 1 100,50 " />
<Storyboard x:Key="ellipseSB">
<DoubleAnimationUsingPath Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(Canvas.Left)"
PathGeometry="{StaticResource path}"
Duration="0:0:5" RepeatBehavior="Forever"
AutoReverse="True" Source="X" />
<DoubleAnimationUsingPath Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(Canvas.Top)"
PathGeometry="{StaticResource path}"
Duration="0:0:5" RepeatBehavior="Forever"
AutoReverse="True" Source="Y" />
</Storyboard>
</Window.Resources>
<Grid x:Name="grid">
<Canvas>
<Ellipse x:Name="ellipse" Width="10" Height="10" Stroke="Black" Fill="Black"
RenderTransformOrigin="0.5,0.5" />
<Path Stroke="Red" StrokeThickness="1" Canvas.Left="5" Canvas.Top="5"
Data="{StaticResource path}"/>
</Canvas>
<Button VerticalAlignment="Bottom" Content="Start" Click="Button_Click"/>
</Grid>
</Window>
Then in codebehind define Button_Click event handler:
private void Button_Click(object sender, RoutedEventArgs e)
{
var sb = FindResource("ellipseSB") as Storyboard;
if (sb != null) sb.Begin();
}
It will animate the 10-by-10 ellipse exactly along the defined arc segment which is a half of a circle.
Hope this helps.
I had the same situation. Eugene's example was very helpful. I extended it slightly to make the full circle:
<Window x:Class="TestCustomTab.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300">
<Window.Resources>
<PathGeometry x:Key="pathUpperHalf" Figures="M 0,50 A 50,50 0 1 1 100,50 " />
<PathGeometry x:Key="pathLowerHalf" Figures="M 100,50 A 50,50 0 1 1 0,50" />
<Storyboard x:Key="ellipseSB" RepeatBehavior="Forever">
<DoubleAnimationUsingPath Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(Canvas.Left)"
PathGeometry="{StaticResource pathUpperHalf}"
Duration="0:0:1" RepeatBehavior="1x" AutoReverse="False" BeginTime="0:0:0" Source="X" />
<DoubleAnimationUsingPath Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(Canvas.Top)"
PathGeometry="{StaticResource pathUpperHalf}"
Duration="0:0:1" RepeatBehavior="1x" AutoReverse="False" BeginTime="0:0:0" Source="Y" />
<DoubleAnimationUsingPath Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(Canvas.Left)"
PathGeometry="{StaticResource pathLowerHalf}"
Duration="0:0:1" RepeatBehavior="1x" AutoReverse="False" BeginTime="0:0:1" Source="X" />
<DoubleAnimationUsingPath Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(Canvas.Top)"
PathGeometry="{StaticResource pathLowerHalf}"
Duration="0:0:1" RepeatBehavior="1x" AutoReverse="False" BeginTime="0:0:1" Source="Y" />
</Storyboard>
</Window.Resources>
<Grid x:Name="grid">
<Canvas>
<Ellipse x:Name="ellipse" Width="10" Height="10" Stroke="Black" Fill="Black" RenderTransformOrigin="0.5,0.5" />
<Path Stroke="Blue" StrokeThickness="1" Canvas.Left="5" Canvas.Top="5" Data="{StaticResource pathUpperHalf}"/>
<Path Stroke="Red" StrokeThickness="1" Canvas.Left="5" Canvas.Top="5" Data="{StaticResource pathLowerHalf}"/>
</Canvas>
<Button VerticalAlignment="Bottom" Content="Start" Click="Button_Click"/>
</Grid>
</Window>
精彩评论