【问题标题】:Center drawn Ellipse origin中心绘制椭圆原点
【发布时间】:2016-11-12 23:30:52
【问题描述】:

我正在尝试制作一个简单的动画,其中一个圆圈正在扩展和消失,我遇到的问题是它从左上角扩展,我希望它从中心扩展,我试过设置RenderTransformOrigin 到 (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);

【问题讨论】:

  • 您的目标是什么:Winforms、WPF、ASP..? 始终正确标记您的问题!
  • 对不起,我以为你只能在 wpf 上使用 DoubleAnimation,所以我使用的是 wpf 和画布来绘制所有内容。
  • 不是标签是什么!这是关于过滤Tagged Questions页面上的问题!!未能正确标记是在浪费大家的时间!我们是来帮忙的,但我们不想玩猜谜和推理游戏。所以:不要告诉我,标记它!

标签: c# wpf canvas


【解决方案1】:

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

您现在可以调整 EllipseGoemetry 的中心和半径以达到所需的效果。也不清楚为什么你的圆以 25 的半径开始,只是为了立即从 0 动画到 30。

【讨论】:

  • 谢谢,这正是我想要的,感谢。
猜你喜欢
  • 2014-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-10
  • 1970-01-01
相关资源
最近更新 更多