如何为GeometryDrawing的属性设置动画?

时间:2011-01-19 22:57:51

标签: wpf

我正在尝试制作一个花哨的闪烁图标并为GeometryDrawing的画笔设置动画,但无法弄清楚如何执行此操作。有可能吗?

这是一些基础xaml

<Window.Resources>
     <GeometryDrawing x:Key="_geometryTest" Brush="#FF6C897B"  Geometry="F1 M 66,188L 194,60L 322,188L 250.889,188L 250.889,348L 137.111,348L 137.111,188L 66,188 Z ">
         <GeometryDrawing.Pen>
            <Pen LineJoin="Round" Brush="#FF000000"/>
        </GeometryDrawing.Pen>      
     </GeometryDrawing>
</Window.Resources>
<Grid>
    <Image>
        <Image.Source>
            <DrawingImage x:Name="_myImage" Drawing="{StaticResource _geometryTest}"/>
        </Image.Source>
    </Image>
  </Grid>

1 个答案:

答案 0 :(得分:2)

使用ColorAnimation。此示例将在加载Window时启动动画

<Window.Resources>
    <GeometryDrawing x:Key="_geometryTest"
                     Brush="#FF6C897B"
                     Geometry="F1 M 66,188L 194,60L 322,188L 250.889,188L 250.889,348L 137.111,348L 137.111,188L 66,188 Z">
        <GeometryDrawing.Pen>
            <Pen LineJoin="Round" Brush="#FF000000"/>
        </GeometryDrawing.Pen>
    </GeometryDrawing>
</Window.Resources>
<Window.Triggers>
    <EventTrigger RoutedEvent="Window.Loaded">
        <BeginStoryboard>
            <Storyboard>
                <ColorAnimation Storyboard.TargetName="myImage" 
                                Storyboard.TargetProperty="(Image.Source).(DrawingImage.Drawing).(GeometryDrawing.Brush).(SolidColorBrush.Color)"
                                To="Red"
                                Duration="0:0:0.5"
                                AutoReverse="True"
                                RepeatBehavior="Forever" />
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Window.Triggers>
<Grid>
    <Image Name="myImage">
        <Image.Source>
            <DrawingImage x:Name="_myImage" Drawing="{StaticResource _geometryTest}"/>
        </Image.Source>
    </Image>
</Grid>