2011-10-02 106 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設置爲與您的轉換轉換相匹配。我相信這會讓你隨心所欲地進行擴展。