4
我在Storyboard方法中做了這樣的事情,但無法達到預期的效果。這個動畫我想在頁面加載後播放。如何同時爲兩個軸(X,Y)做翻譯動畫?
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
CreateTranslateAnimation(image1);
}
private void CreateTranslateAnimation(UIElement source)
{
Storyboard sb = new Storyboard();
DoubleAnimationUsingKeyFrames animationFirstX = new DoubleAnimationUsingKeyFrames();
source.RenderTransform = new CompositeTransform();
Storyboard.SetTargetProperty(animationFirstX, new PropertyPath(CompositeTransform.TranslateXProperty));
Storyboard.SetTarget(animationFirstX, source.RenderTransform);
animationFirstX.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = kt1, Value = 20 });
DoubleAnimationUsingKeyFrames animationFirstY = new DoubleAnimationUsingKeyFrames();
source.RenderTransform = new CompositeTransform();
Storyboard.SetTargetProperty(animationFirstY, new PropertyPath(CompositeTransform.TranslateYProperty));
Storyboard.SetTarget(animationFirstY, source.RenderTransform);
animationFirstY.KeyFrames.Add(new EasingDoubleKeyFrame() { KeyTime = kt1, Value = 30 });
sb.Children.Add(animationFirstX);
sb.Children.Add(animationFirstY);
sb.Begin();
}
長話短說...
我想寫的.cs代碼相當於該代碼
<Storyboard x:Name="Storyboard1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="image1">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="20"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="image1">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="30"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
這是什麼做的是不是你想要的?請提供一個完整的示例,顯示您如何使用此代碼。 – 2012-03-15 22:15:32
這只是沿y軸移動圖像,而我要求 寫這段代碼是沿着兩個軸,像在一個診斷中移動它。時尚,從(0,0)到(20,30)都是合作伙伴。在 animationFirstX.KeyFrames.Add給出(新EasingDoubleKeyFrame(){KeyTime = KT1,值= 20} animationFirstY.KeyFrames.Add(新EasingDoubleKeyFrame(){KeyTime = KT1,值= 30} 這些線(見代碼) – user1235555 2012-03-16 06:03:13
你也可以在xaml中定義你的動畫,然後在運行時訪問你的代碼並修改它的道具。 – Vitalij 2012-03-26 03:04:31