2011-10-02 105 views
0

我有一个WPF应用程序和图像内的画布。图像被放置在0,0。翻译和缩放动画

我需要动画从0,0到500,200移动的图像,并在同一时间增长(我喜欢做出像从远到近的效果)。

如果我这样做:

 TranslateTransform ttx = new TranslateTransform(); 
     TranslateTransform tty = new TranslateTransform(); 

     DoubleAnimationUsingKeyFrames dax = new DoubleAnimationUsingKeyFrames(); 
     dax.KeyFrames.Add(new LinearDoubleKeyFrame(500, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1)))); 

     DoubleAnimationUsingKeyFrames day = new DoubleAnimationUsingKeyFrames(); 
     day.KeyFrames.Add(new LinearDoubleKeyFrame(200, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1)))); 

     TransformGroup tg = new TransformGroup(); 
     tg.Children.Add(ttx); 
     tg.Children.Add(tty); 

     krug.RenderTransform = tg; 

     ttx.BeginAnimation(TranslateTransform.XProperty, dax); 
     tty.BeginAnimation(TranslateTransform.YProperty, day); 

这工作得很好。它将图像“krug”的翻译从0,0动画到500,200。

但是,当我为缩放图像添加逻辑,而翻译是这样的:

 ScaleTransform zoom = new ScaleTransform(); 
     DoubleAnimationUsingKeyFrames zoomTimeline = new DoubleAnimationUsingKeyFrames(); 
     zoomTimeline.KeyFrames.Add(new LinearDoubleKeyFrame(2, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1)))); 
     tg.Children.Add(zoom); 
     zoom.BeginAnimation(ScaleTransform.ScaleXProperty, zoomTimeline); 
     zoom.BeginAnimation(ScaleTransform.ScaleYProperty, zoomTimeline); 

那么图像不会停止为500,200,但去越远。如果缩放比例更大,翻译将会更有前途。我如何控制动画在500,200停止?

回答

0

将动画放在单个故事板中,并设置故事板的持续时间以控制动画的长度。

然后启动故事板。

+0

那里存在同样的问题。添加ScaleTransform时,翻译结束点会增加。 – darpet

0

我找到了解决方案。我创建了独立的用户控件,只有ScaleTransform。然后我在用户控件上应用TranslateTransform。

1

你碰上规模相结合时和翻译转换的问题是,它会按比例转换,如果你想将其从原产地(ScaleTransform.CenterX,ScaleTransform.CenterY)

例如,点变换向右移动50倍,并将其缩小两倍,实际上移动的网络距离为100.

尝试将ScaleTransform.CenterX和ScaleTransform.CenterY设置为与您的转换转换相匹配。我相信这会让你随心所欲地进行扩展。