如何在中心设置MatrixTransform.Matrix的动画?

时间:2014-02-28 02:37:21

标签: c# wpf xaml animation

我正在开发一个WPF应用程序,允许用户使用多点触控来使用MatrixTransform.Matrix上的RenderTransform来操作图像。使用this very helpful snippet of code,我能够使用在C#中创建的故事板为其设置动画。

问题是currentItem似乎总是相对于左上角旋转和缩放。

如何围绕FrameworkElement的中心旋转?

这是我到目前为止的一个使用动画的基本示例:

Matrix fromMatrix = ((MatrixTransform)currentItem.RenderTransform).Matrix;

Matrix toMatrix = new MatrixTransform().Matrix;
//Making sure location of the toMatrix is in the same place as the fromMatrix
toMatrix.OffsetX = fromMatrix.OffsetX;
toMatrix.OffsetY = fromMatrix.OffsetY;

MatrixAnimation myDoubleAnimation = new MatrixAnimation();
myDoubleAnimation.From = fromMatrix;
myDoubleAnimation.To = toMatrix;

myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(.5));

Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);
Storyboard.SetTarget(myDoubleAnimation, currentItem);
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath("(FrameworkElement.RenderTransform).(MatrixTransform.Matrix)"));
myStoryboard.Begin(this);

我觉得我需要在fromMatrix.OffsetYfromMatrix.OffsetX中添加或减去。我尝试用currentItem的宽度/高度的一半进行此操作,但这要么将其旋转并围绕中心(好) OR 两次缩放远离中心(两倍差),具体取决于currentItem在开始制作动画时转向的方式。

1 个答案:

答案 0 :(得分:0)

这里的回答太晚了 - 但我相信你只是使用 XAML 属性

RenderTransformOrigin="0.5, 0.5"

将其放在应用了转换的 XAML 上。不确定是否需要在代码隐藏中执行此操作,除非您在代码中构建了该对象。

参考:RenderTransformOrigin from MS Docs

相关问题