【问题标题】:how to draw a circle and move it as an animation in C# WPF如何在 C# WPF 中绘制一个圆圈并将其作为动画移动
【发布时间】:2012-02-21 02:06:52
【问题描述】:

我想画一个简单的圆圈,并通过编码将其移动到 X、Y 轴上的定义位置。

例如;

WPF 窗口上会有 2 个按钮,0,0(x,y) 上会有一个圆圈。当我单击第一个按钮时,它将转到 X = 150 和 Y= 40。但形状必须顺利到达那里。我的意思是我不希望它在当前位置消失并出现在定义的位置。我想让它去那里。我该怎么做?你能解释一下步骤吗?如果可能的话,还有一些示例代码?

更新代码:

    int X = 0;
    int Y = 0;

    public bool inside = true;

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {

        if (inside)
        {
            DoubleAnimation animatex = new DoubleAnimation();
            animatex.To = X++;
            // animatex.Duration = new Duration(TimeSpan.FromSeconds(1));
            // animatex.RepeatBehavior = RepeatBehavior.Forever;
            el.BeginAnimation(Canvas.LeftProperty, animatex);

            DoubleAnimation animatey = new DoubleAnimation();
            animatey.To = Y++;
            // animatey.RepeatBehavior = RepeatBehavior.Forever;
            el.BeginAnimation(Canvas.TopProperty, animatey);
        }
    }

【问题讨论】:

    标签: c# wpf animation drawing geometry


    【解决方案1】:

    xaml 类似于 <Canvas> <Ellipse Width="10" Height="10" Canvas.Left="0" Canvas.Top="0" Fill="Black" x:Name="el"/> </Canvas> 按钮的点击事件是这样的

    希望这会奏效。我试过了,效果很好。

    【讨论】:

    • 请参阅here,了解如何格式化代码块。而且你肯定不会永远重复动画,对吧?圆圈应简单地移动一次到目标位置。
    • 感谢您的解决方案。对于克莱门斯;实际上,我会改进代码,我会做 4 个箭头,从上到下,当我点击其中一个时,它会一直持续下去,直到我用另一个按钮结束移动。看来我会在if循环中一一增加animatex和animatey
    • @Ethicallogics 或其他人:你能检查一下我在上面更新的代码中做错了什么吗?我希望圆圈移动,直到我改变 if 条件。但它不是那样工作的。每次我点击按钮时它都会起作用。
    【解决方案2】:

    或仅在 XAML 中:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackPanel>
            <Button Content="Move it">
                <Button.Triggers>
                    <EventTrigger RoutedEvent="Button.Click" >
                        <BeginStoryboard>
                            <Storyboard Storyboard.TargetName="theEllipse" Duration="00:00:05">
                                <DoubleAnimation From="-5" To="145" Storyboard.TargetProperty="(Canvas.Left)"/>
                                <DoubleAnimation From="-5" To="35" Storyboard.TargetProperty="(Canvas.Top)"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Button.Triggers>
            </Button>
        </StackPanel>
        <Border Margin="10" Grid.Row="1">
            <Canvas>
                <Ellipse x:Name="theEllipse" Width="10" Height="10" Fill="BlueViolet" Canvas.Left="-5" Canvas.Top="-5" />
            </Canvas>
        </Border>
    </Grid>
    

    在此处或其他地方搜索有关使用 Storyboard 的信息。

    【讨论】:

    • 你能检查一下我在上面更新的代码中做错了什么吗?我希望圆圈移动,直到我改变 if 条件。但它不是那样工作的。每次我点击按钮时它都会起作用。谢谢。
    • Samet,问一个问题,然后在你得到一个或多个有用或正确的答案后更改它是不好的做法。请接受答案并提出新问题。
    • 好的,谢谢。我不知道。这是另一个链接:stackoverflow.com/questions/9366754/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 2019-09-04
    相关资源
    最近更新 更多