请帮我写一个WPF 圆圈圆点动画
时间: 2023-10-09 07:14:18 浏览: 157
WPF动画实例
5星 · 资源好评率100%
好的,以下是一个简单的 WPF 圆圈圆点动画示例:
首先,在 XAML 中添加一个 `Canvas` 控件,并在其中添加一个 `Ellipse` 控件作为圆圈,还需要添加一个 `Rectangle` 控件作为圆点。
```xaml
<Canvas>
<Ellipse x:Name="circle" Width="100" Height="100" Fill="Red" Canvas.Left="50" Canvas.Top="50"/>
<Rectangle x:Name="dot" Width="10" Height="10" Fill="Blue" Canvas.Left="95" Canvas.Top="95"/>
</Canvas>
```
然后,在代码中添加以下动画:
```c#
private void Animate()
{
// 创建圆点动画
var dotAnimation = new DoubleAnimation
{
From = 0,
To = 90,
Duration = TimeSpan.FromSeconds(1),
RepeatBehavior = RepeatBehavior.Forever
};
dotAnimation.EasingFunction = new SineEase { EasingMode = EasingMode.EaseInOut };
Storyboard.SetTarget(dotAnimation, dot);
Storyboard.SetTargetProperty(dotAnimation, new PropertyPath("(Canvas.Left)"));
// 创建圆圈动画
var circleAnimation = new DoubleAnimation
{
From = 0,
To = 360,
Duration = TimeSpan.FromSeconds(1),
RepeatBehavior = RepeatBehavior.Forever
};
circleAnimation.EasingFunction = new SineEase { EasingMode = EasingMode.EaseInOut };
Storyboard.SetTarget(circleAnimation, circle);
Storyboard.SetTargetProperty(circleAnimation, new PropertyPath("(Canvas.Left)"));
// 创建 Storyboard 并开始动画
var storyboard = new Storyboard();
storyboard.Children.Add(dotAnimation);
storyboard.Children.Add(circleAnimation);
storyboard.Begin();
}
```
这个动画会让圆点沿着圆圈运动。你可以根据需要调整控件的大小、颜色、位置和动画参数。
阅读全文