代码之家  ›  专栏  ›  技术社区  ›  Alexander

中心绘制椭圆原点

  •  2
  • Alexander  · 技术社区  · 8 年前

    我正在尝试制作一个简单的动画,其中一个圆圈正在扩展和消失,我的问题是它从左上角扩展,我希望它从中心扩展,我尝试将RenderTransferMorigin设置为(0.5,0.5),但仍然不起作用。

    这是我的代码:

    Ellipse impact = new Ellipse();   
    
    impact.Width = 50;   
    impact.Height = 50;
    impact.StrokeThickness = 1.5f;
    
    impact.RenderTransformOrigin = new Point(0.5, 0.5);
    MainCanvas.Children.Add(impact);
    
    
    Storyboard story = new Storyboard();
    DoubleAnimation anim = new DoubleAnimation(0, 60, TimeSpan.FromSeconds(0.9));
    DoubleAnimation anim2 = new DoubleAnimation(0, 60, TimeSpan.FromSeconds(0.9));
    DoubleAnimation anim3 = new DoubleAnimation(1, 0, TimeSpan.FromSeconds(0.9));
    
    
    Storyboard.SetTargetProperty(anim, new PropertyPath("(Ellipse.Height)"));
    Storyboard.SetTargetProperty(anim2, new PropertyPath("(Ellipse.Width)"));
    Storyboard.SetTargetProperty(anim3, new PropertyPath("(Ellipse.Opacity)"));
    
    story.Children.Add(anim);
    story.Children.Add(anim2);
    story.Children.Add(anim3);
    
    impact.BeginStoryboard(story);
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Clemens    8 年前

    设置 RenderTransformOrigin 属性,而不设置 RenderTransform 这没有任何效果。

    您可能想要做的是使用 EllipseGeometry 而不是椭圆:

    var impact = new Path
    {
        Data = new EllipseGeometry(new Point(25, 25), 25, 25),
        StrokeThickness = 1.5,
        Stroke = Brushes.Black
    };
    
    MainCanvas.Children.Add(impact);
    
    Storyboard story = new Storyboard();
    DoubleAnimation anim1 = new DoubleAnimation(0, 30, TimeSpan.FromSeconds(0.9));
    DoubleAnimation anim2 = new DoubleAnimation(0, 30, TimeSpan.FromSeconds(0.9));
    DoubleAnimation anim3 = new DoubleAnimation(1, 0, TimeSpan.FromSeconds(0.9));
    
    Storyboard.SetTargetProperty(anim1, new PropertyPath("Data.RadiusX"));
    Storyboard.SetTargetProperty(anim2, new PropertyPath("Data.RadiusY"));
    Storyboard.SetTargetProperty(anim3, new PropertyPath("Opacity"));
    
    story.Children.Add(anim1);
    story.Children.Add(anim2);
    story.Children.Add(anim3);
    
    impact.BeginStoryboard(story);
    

    现在可以调整椭圆几何的中心和半径,以获得所需效果。也不清楚为什么圆以半径25开始,以便立即从0到30设置动画。