2012-09-23 13 views
1

我能夠動畫邊框的運動:WPF動畫的寬度和高度與後面的代碼(放大)

private void MoveTo(Border target, double newX, double newY) 
{ 
    Vector offset = VisualTreeHelper.GetOffset(target); 
    var top = offset.Y; 
    var left = offset.X; 
    TranslateTransform trans = new TranslateTransform(); 
    target.RenderTransform = trans; 
    DoubleAnimation anim1 = new DoubleAnimation(0, newY - top, TimeSpan.FromMilliseconds(500)); 
    DoubleAnimation anim2 = new DoubleAnimation(0, newX - left, TimeSpan.FromMilliseconds(500)); 
    trans.BeginAnimation(TranslateTransform.YProperty, anim1); 
    trans.BeginAnimation(TranslateTransform.XProperty, anim2); 
} 

但是我想能夠進行動畫的高度和寬度的增加以及作爲放大圖像的印象(包含在我的情況和上面的示例中的邊框中))。

這是可能的代碼背後?


好吧,我試過了縮放轉換,但它似乎沒有做任何事 - 我是否需要一個故事板?

private void Zoom(Border target) 
    { 
     TranslateTransform trans = new TranslateTransform(); 
     target.RenderTransform = trans; 
     DoubleAnimation anim1 = new DoubleAnimation(1, 2, TimeSpan.FromMilliseconds(1000)); 
     DoubleAnimation anim2 = new DoubleAnimation(1, 2, TimeSpan.FromMilliseconds(1000)); 
     trans.BeginAnimation(ScaleTransform.ScaleXProperty, anim1); 
     trans.BeginAnimation(ScaleTransform.ScaleYProperty, anim2); 
    } 

回答

0

使用ScaleTransform,無需Height & Width動畫時,ScaleTransform會影響你的邊框的VisualTree所以內部的圖像會被拉伸爲好。

private void Zoom(Border target) 
    { 
     ScaleTransform trans = new ScaleTransform(); 
     target.RenderTransform = trans; 
     // if you use the same animation for X & Y you don't need anim1, anim2 
     DoubleAnimation anim = new DoubleAnimation(1, 2, TimeSpan.FromMilliseconds(1000)); 
     trans.BeginAnimation(ScaleTransform.ScaleXProperty, anim); 
     trans.BeginAnimation(ScaleTransform.ScaleYProperty, anim); 

    } 
+0

感謝您的回覆 - 我試過了,但它似乎沒有做任何事情。我已經用我的嘗試更新了我的答案。 –

+1

不要使用'TranslateTransform trans = new TranslateTransform();'而是使用'ScaleTransform trans = new ScaleTransform();'更新我的代碼,它的工作完美@DavidRelihan – S3ddi9

7

這是更好地利用尺度變換進行縮放,但如果你堅持動畫W/H,你可以做到這一點,因爲這些都是正常的DP您可以使用標準DoubleAnimation是/ DoubleAnimationUsingKeyFrames 喜歡的東西

它們的動畫
DoubleAnimation doubleAnimation = new DoubleAnimation(100, 200, new Duration(TimeSpan.FromMilliseconds(500))); 
     this.BeginAnimation(FrameworkElement.WidthProperty, doubleAnimation);