Cesitar Ps Ответов: 1

Как повернуть изображение вдоль оси X, а затем изменить его на другое, в WPF


I have an image that I want to rotate along the X axis and when the animation ends, I want to change it to another image.

This 04 second video is an example of what I intend to achieve
<a href="https://www.youtube.com/watch?v=gzyAemyU0Rs"></a>

What I have tried:

<pre lang="HTML"><Window.Resources>
        <Style x:Key="AnimationImage" TargetType="{x:Type Image}">
            <Setter Property="RenderTransform">
                <Setter.Value>
                    <!--<RotateTransform Angle="0" CenterX="0.5" CenterY="0.5" />-->
                    <ScaleTransform ScaleX ="1" ScaleY ="1" CenterX="0.5" CenterY="0.5"/>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="true">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX"
                                    From="1" To="-1" Duration="0:0:1" AutoReverse="False" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    
    <StackPanel Margin="0,25,0,0">
        <Button Name="buPrueba" Content="Rotar" Height="25" Width="150" HorizontalAlignment="Center" Click="buPrueba_Click"/>
        <Image Name="imgPeru" Source="/Peru1.png" Height="150" Margin="0,25,0,0"/>
    </StackPanel>
</Window>


private void buPrueba_Click(object sender, RoutedEventArgs e)
        {
            imgPeru.Style = FindResource("AnimationImage") as Style;
        }

1 Ответов

Рейтинг:
6

Graeme_Grant

Вот так, все в XAML и сделано с Blend2017:

<Window x:Class="WpfRotateImage.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"



        mc:Ignorable="d"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"



        Title="WPF CodeProject Answer - Rotate Image" Height="350" Width="525">

    <Window.Resources>
        <Storyboard x:Key="Rotate">
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="Image1">
                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Hidden}"/>
                <DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="{x:Static Visibility.Visible}"/>
                <DiscreteObjectKeyFrame KeyTime="0:0:1" Value="{x:Static Visibility.Visible}"/>
                <DiscreteObjectKeyFrame KeyTime="0:0:1.01" Value="{x:Static Visibility.Hidden}"/>
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="Image2">
                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                <DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="{x:Static Visibility.Hidden}"/>
                <DiscreteObjectKeyFrame KeyTime="0:0:1.01" Value="{x:Static Visibility.Visible}"/>
            </ObjectAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="Image1">
                <EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="1"/>
                <EasingDoubleKeyFrame KeyTime="0:0:1" Value="-1"/>
                <EasingDoubleKeyFrame KeyTime="0:0:1.2" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="button">
            <BeginStoryboard Storyboard="{StaticResource Rotate}"/>
        </EventTrigger>
    </Window.Triggers>
    <!--<Window.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource Rotate}"/>
        </EventTrigger>
    </Window.Triggers>-->

    <Grid>
        <Grid.Resources>
            <Style TargetType="Image">
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="VerticalAlignment" Value="Top"/>
                <Setter Property="Margin" Value="0 10 0 0"/>
                <Setter Property="Grid.ColumnSpan" Value="8"/>
                <Setter Property="Grid.RowSpan" Value="8"/>
                <Setter Property="Width" Value="100"/>
                <Setter Property="Height" Value="100"/>
            </Style>
            <Style TargetType="Button">
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="VerticalAlignment" Value="Bottom"/>
                <Setter Property="Margin" Value="0 10"/>
                <Setter Property="Padding" Value="10 5"/>
                <Setter Property="Grid.ColumnSpan" Value="8"/>
                <Setter Property="Grid.RowSpan" Value="8"/>
            </Style>
        </Grid.Resources>

        <Image x:Name="Image1" Source="https://vc.vse.cz/wp-content/uploads/2014/08/Accept-icon.png" RenderTransformOrigin="0.5,0.5" Visibility="Hidden">
            <Image.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </Image.RenderTransform>
        </Image>
        <Image x:Name="Image2" Source="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c0/MUTCD_R1-1.svg/2000px-MUTCD_R1-1.svg.png"/>

        <Button x:Name="button" Content="Animate"/>
    </Grid>

</Window>