簡體   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