使用 XAML 在 WPF 中为绘图圆设置动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14090889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Animate Drawing Circle in WPF with XAML
提问by Malcolm McCaffery
I want to be able to smoothly draw a circle (ellipse) so that you can see it being drawn on screen.
我希望能够平滑地绘制一个圆(椭圆),以便您可以在屏幕上看到它正在绘制。
Is there anyway possible using DoubleAnimation to do this? If not what is an alternative method?
有没有可能使用 DoubleAnimation 来做到这一点?如果不是什么是替代方法?
An example of what I have:
我所拥有的一个例子:
- One outer ellipse (black)
- Inner ellipse (white) - this is the one I want to animate
- 一个外椭圆(黑色)
- 内椭圆(白色) - 这是我想要制作动画的椭圆
Code:
代码:
<Ellipse Width="200" Height="200" Stroke="Black" StrokeThickness="20"/>
<Ellipse Width="190" Height="190" Stroke="White" StrokeThickness="10" Canvas.Left="5" Canvas.Top="5" x:Name="animatedEllipse">
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Ellipse.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
I've look at a few examples for example:
例如,我查看了一些示例:
- http://elegantcode.com/2009/08/21/a-simple-wpf-loading-animation/
- http://www.charlespetzold.com/blog/2012/03/The-Animated-Pie-Slice-in-Windows-8.html
- Ellipse Drawing WPF Animation
- http://elegantcode.com/2009/08/21/a-simple-wpf-loading-animation/
- http://www.charlespetzold.com/blog/2012/03/The-Animated-Pie-Slice-in-Windows-8.html
- 椭圆绘图 WPF 动画
The first two are a bit confusimg for me being new to WPF animation. The latter has 7 votes as "Correct answer" but it is not working for me as I get error "the collection element StrokeDashArray[0] is not a dependency property" (and also I don't want dashes, Although I tried this even on an ellipse with dashes as per the above article and it still failed on this error.
前两个对我来说是 WPF 动画的新手有点令人困惑。后者有 7 票作为“正确答案”,但它对我不起作用,因为我收到错误“集合元素 StrokeDashArray[0] 不是依赖属性”(而且我不想要破折号,尽管我什至尝试过根据上述文章在带有破折号的椭圆上,它仍然因此错误而失败。
Update:
更新:
A method I got sort of working using code is this:
我使用代码工作的一种方法是:
public static class ExtensionMethods
{
private static Action EmptyDelegate = delegate() { };
public static void Refresh(this UIElement uiElement)
{
uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
}
}
public partial class Page1 : Page
{
private void Page_Loaded_1(object sender, RoutedEventArgs e)
{
path = new Path();
group = new GeometryGroup();
path.Data = group;
path.Stroke = new SolidColorBrush(Colors.White);
path.StrokeThickness = 3;
canvas.Children.Add(path);
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.RunWorkerAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
int radius = 90;
for (double i = 0.0; i < 360.0; i += 1)
{
double angle = i * System.Math.PI / 180;
double x = (int)(100 + radius * System.Math.Cos(angle));
double y = (int)(100 + radius * System.Math.Sin(angle));
canvas.Dispatcher.Invoke(new Action(delegate
{
group.Children.Add(new EllipseGeometry(new Point(x, y), 5, 5));
}));
canvas.Refresh();
System.Threading.Thread.Sleep(1);
}
}
回答by Paritosh
You might need three elements:
您可能需要三个要素:
Outer circle (fill color will be light colors).
inner circle with transparent fill color.
Arc segment will be having thickness difference of radius between them.
Arc will be positioned at 45 angle , can be animated over the both circles.
外圆(填充颜色将是浅色)。
具有透明填充颜色的内圆。
弧段之间会有半径的粗细差异。
弧将定位在 45 角,可以在两个圆上设置动画。
This is just an idea, I might need to test it on my own.
这只是一个想法,我可能需要自己测试。
回答by ChrisF
You might get further by using an ArcSegmentinstead of an ellipse:
你可能会通过使用一个ArcSegment而不是一个椭圆来进一步:
<PathFigure StartPoint="100,100">
<PathFigure.Segments>
<PathSegmentCollection>
<ArcSegment Size="100,100" IsLargeArc="True"
SweepDirection="CounterClockwise" Point="200,200" />
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
It needs to be part of the PathFigure- where the start point is specified.
它需要是PathFigure- 指定起点的一部分。
You can then animate Pointwhich is the end point of the arc to go from the start point through 360 degrees to the end point.
然后Point,您可以将圆弧的终点设置为从起点经过 360 度到终点的动画。

