繁体   English   中英

如何仅使用c#代码在图像对象上创建旋转动画(在WPF窗口内)

[英]How do I create a rotate animation on an image object using c# code only (inside a WPF window)

我有几个与同类事情有关的未解决的问题,

我是WPF的新手,但对C#和Winforms有经验。

我已经在互联网上浏览了一个有效的例子,但还没找到一个有效的。

我想要实现的是在C#函数中创建以下内容

  • 创建图像(图像1)
  • 创建图像(图像2)
  • 将图像并排放在窗口上
  • 创建一个故事板
  • 将image1的rotate属性设置为0到360(animation1)
  • 将图像2的不透明度属性从完全设置为不可见(animation2)
  • 故事板应运行十秒钟,动画1从0秒开始,动画2从5秒开始

明确的代码请求道歉,但是,我已经看了,并尝试过,我之前的问题有完整的代码执行但没有动画显示(链接如下)

如何使用c#代码创建故事板并在wpf中旋转图像

提前致谢

担。

这是一个有问题的XAML版本的问题,后面是C#中的相同内容。 可能不完全是你所追求的,但它应该说明它。

XAML版本:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Storyboard x:Key="Storyboard" BeginTime="00:00:00.000" Duration="00:00:10.000">
            <DoubleAnimation Storyboard.TargetName="RotateImage" 
                             Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" 
                             From="0" To="360" BeginTime="00:00:05.000" Duration="00:00:05.000" />
            <DoubleAnimation Storyboard.TargetName="OpacityImage" 
                             Storyboard.TargetProperty="Opacity" 
                             From="1" To="0" Duration="00:00:10.000" />
        </Storyboard>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Image x:Name="RotateImage" Stretch="Uniform" Source="Chrysanthemum.jpg">
            <Image.RenderTransform>
                <RotateTransform Angle="0" />
            </Image.RenderTransform>
        </Image>
        <Image x:Name="OpacityImage" Grid.Column="1" Stretch="Uniform" Source="Desert.jpg" />
        <Button Grid.Row="1" Grid.ColumnSpan="2" Content="Start">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard Storyboard="{StaticResource Storyboard}" />
                </EventTrigger>
            </Button.Triggers>
        </Button>
    </Grid>
</Window>

和C#版本:

    public MainWindow()
    {
        InitializeComponent();

        Image rotateImage = new Image()
        {
            Stretch = Stretch.Uniform,
            Source = new BitmapImage(new Uri("pack://application:,,,/Chrysanthemum.jpg")),
            RenderTransform = new RotateTransform()
        };
        Image opacityImage = new Image()
        {
            Stretch = Stretch.Uniform,
            Source = new BitmapImage(new Uri("pack://application:,,,/Desert.jpg"))
        };

        LayoutRoot.Children.Add(rotateImage);
        LayoutRoot.Children.Add(opacityImage);

        Grid.SetColumn(opacityImage, 1);

        Storyboard storyboard = new Storyboard();
        storyboard.Duration = new Duration(TimeSpan.FromSeconds(10.0));
        DoubleAnimation rotateAnimation = new DoubleAnimation()
        {
            From = 0,
            To = 360,
            Duration = storyboard.Duration
        };
        DoubleAnimation opacityAnimation = new DoubleAnimation()
        {
            From = 1.0,
            To = 0.0,
            BeginTime = TimeSpan.FromSeconds(5.0),
            Duration = new Duration(TimeSpan.FromSeconds(5.0))
        };

        Storyboard.SetTarget(rotateAnimation, rotateImage);
        Storyboard.SetTargetProperty(rotateAnimation, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));
        Storyboard.SetTarget(opacityAnimation, opacityImage);
        Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));

        storyboard.Children.Add(rotateAnimation);
        storyboard.Children.Add(opacityAnimation);

        Resources.Add("Storyboard", storyboard);

        Button button = new Button()
        {
            Content = "Begin"
        };
        button.Click += button_Click;

        Grid.SetRow(button, 1);
        Grid.SetColumnSpan(button, 2);

        LayoutRoot.Children.Add(button);
    }

    void button_Click(object sender, RoutedEventArgs e)
    {
        ((Storyboard)Resources["Storyboard"]).Begin();
    }

动画的工作如下。

1 - 程序创建一个计时器。

2 - 程序以设定的间隔检查计时器,以查看已经过了多长时间。

3 - 每次程序检查计时器时,它都会根据经过的时间计算矩形的当前不透明度值。

4-程序然后用新值更新矩形并重绘它。

以下是创建Rectangle并为其设置动画的代码。

<Window x:Class="Animation.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Animated Rectangle" Height="350" Width="525">
<Grid>
    <StackPanel Margin="10">
        <Image Name="MyImage" Source="e:\a.jpg" Width="100" Margin="50" ></Image>
        <Rectangle
            Name="MyRectangle"
            Width="100" 
            Height="100"
            Fill="Blue">

            <Rectangle.Triggers>
                <!-- Animates the rectangle's opacity. -->
                <EventTrigger RoutedEvent="Rectangle.Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation
                                Storyboard.TargetName="MyImage" 
                                Storyboard.TargetProperty="Opacity"
                                From="1.0" To="0.0" Duration="0:0:3" 
                                AutoReverse="True" RepeatBehavior="Forever" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Rectangle.Triggers>
        </Rectangle>
    </StackPanel>
</Grid>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM