2012-12-30 28 views
3

我希望能夠平滑地繪製一個圓(橢圓),以便您可以看到它正在屏幕上繪製。使用XAML在WPF中動畫繪製圓

反正是有可能使用DoubleAnimation是做到這一點?如果不是什麼是一種替代方法?

什麼,我有一個例子:

  • 一個外部橢圓形(黑色)
  • 內橢圓(白色) - 這是一個我想動畫

代碼:

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

我看了幾個例子,例如:

前兩個是我作爲新的WPF動畫有點confusimg。後者有7票作爲「正確的答案」,但它不適合我,因爲我收到錯誤「集合元素StrokeDashArray [0]不是一個依賴屬性」(我也不想破折號,雖然我試過這甚至與破折號橢圓按照上面的文章,它還是沒有對這個錯誤

更新:

我有一種方法,使用代碼排序的工作是這樣的:

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); 

     } 
    } 

回答

0

你可能會得到進一步使用ArcSegment代替橢圓:

<PathFigure StartPoint="100,100"> 
    <PathFigure.Segments> 
     <PathSegmentCollection> 
      <ArcSegment Size="100,100" IsLargeArc="True" 
         SweepDirection="CounterClockwise" Point="200,200" /> 
     </PathSegmentCollection> 
    </PathFigure.Segments> 
</PathFigure> 

它需要的PathFigure的一部分 - 在指定的起點。

然後,您可以動畫Point這是圓弧的終點到360度,從起點到終點。

+0

我無法將此工作納入工作。目前我唯一使用的方法是使用一些代碼,但似乎有點令人費解。我加入了問題。 –

1

您可能需要三個要素:

  1. 外圓(填充顏色,會發光的顏色)。

  2. 內圓與透明填充顏色。

  3. 弧段將具有在它們之間的半徑厚度差。

  4. 弧將定位在45°角,可以在兩個圓上進行動畫處理。

這只是一個想法,我可能需要自己測試一下。