2

我这样定义了一个多边形:

            <!-- Draws a hexagon by specifying the vertices of a polygon -->
            <Polygon x:Name="polygon"
                     Points="0,50 0,120 50,170 120,170 170,120 170,50 120,0 50,0"
                     Margin="25, 0, 25, 25"
                     Stroke="Red"
                     RenderTransformOrigin="0.5,0.5">

                <Polygon.RenderTransform>
                    <CompositeTransform />
                </Polygon.RenderTransform>

                <Polygon.Fill>
                    <ImageBrush x:Name="imageBrush"
                                ImageSource="Assets/image1.jpg"
                                Stretch="Fill">
                    </ImageBrush>
                </Polygon.Fill>
            </Polygon>

我还定义了一个这样的故事板:

    <Storyboard x:Name="Storyboard2">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.Rotation)"
                                       Storyboard.TargetName="polygon">
            <EasingDoubleKeyFrame KeyTime="0"
                                  Value="0" />
            <EasingDoubleKeyFrame KeyTime="0:0:2"
                                  Value="-360" />
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>

我像这样开始多边形旋转:

        Storyboard2.BeginTime = new TimeSpan( 0 );
        Storyboard2.Begin();

上面的代码按预期旋转了整个多边形。但是我想修改它,让多边形静止,只是在多边形内旋转位图。

我怎么做?谢谢

4

2 回答 2

2

要在多边形内旋转图像,您可以尝试使用如下ImageBrush.RelativeTransform属性

<Polygon x:Name="polygon"
         Margin="25, 0, 25, 25"
         Points="0,50 0,120 50,170 120,170 170,120 170,50 120,0 50,0"
         Stroke="Red">
    <Polygon.Fill>
        <ImageBrush x:Name="imageBrush" ImageSource="Assets/Capture.PNG" Stretch="Fill">
            <ImageBrush.RelativeTransform>
                <CompositeTransform CenterX="0.5" CenterY="0.5" />
            </ImageBrush.RelativeTransform>
        </ImageBrush>
    </Polygon.Fill>
</Polygon>

并更改您的Storyboard以下内容:

<Storyboard x:Name="Storyboard2">
    <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True"
                                   Storyboard.TargetName="imageBrush"
                                   Storyboard.TargetProperty="(Brush.RelativeTransform).(CompositeTransform.Rotation)">
        <EasingDoubleKeyFrame KeyTime="0" Value="0" />
        <EasingDoubleKeyFrame KeyTime="0:0:2" Value="-360" />
    </DoubleAnimationUsingKeyFrames>
</Storyboard>

在这里,由于目标是ImageBrush,所以我们需要更改Storyboard.TargetProperty(Brush.RelativeTransform).(CompositeTransform.Rotation)。并且作为动画目标Brush.RelativeTransform,所以它是一个依赖动画。我们需要设置EnableDependentAnimation属性true来启用这个动画。有关依赖动画的更多信息,请参阅依赖动画和独立动画

在此之后,您可以调用Storyboard2.Begin();以启动动画。

于 2016-05-13T10:02:42.060 回答
1

由于我目前无法在 UWP enviro 上进行测试,这就是它在 WPF 中的完成方式,相同的概念......但我认为语法可能有点不同,因此我想测试。

故事板:

<Storyboard x:Key="SpinThisBugger" RepeatBehavior="Forever">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(Brush.RelativeTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="polygon">
                <EasingDoubleKeyFrame KeyTime="0:0:1" Value="360"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>

触发器启动它:

<Window.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
        <BeginStoryboard Storyboard="{StaticResource SpinThisBugger}"/>
    </EventTrigger>
</Window.Triggers>

聚:

<Polygon x:Name="polygon"
                     Points="0,50 0,120 50,170 120,170 170,120 170,50 120,0 50,0"
                        HorizontalAlignment="Center" VerticalAlignment="Center"
                     Stroke="Red"
                     RenderTransformOrigin="0.5,0.5">
            <Polygon.Fill>
                <ImageBrush x:Name="imageBrush"
                                ImageSource="Images/learn-hypnosis.jpg"
                                Stretch="Fill">
                    <ImageBrush.RelativeTransform>
                        <TransformGroup>
                            <ScaleTransform CenterY="0.5" CenterX="0.5"/>
                            <SkewTransform CenterY="0.5" CenterX="0.5"/>
                            <RotateTransform CenterY="0.5" CenterX="0.5"/>
                            <TranslateTransform/>
                        </TransformGroup>
                    </ImageBrush.RelativeTransform>
                </ImageBrush>
            </Polygon.Fill>

        </Polygon>

...以及我使用的图像,因为它很漂亮,哈哈:

在此处输入图像描述

于 2016-05-12T21:42:12.523 回答