【问题标题】:How to get a fluid animation with storyboard如何使用情节提要获得流畅的动画
【发布时间】:2012-07-05 07:54:04
【问题描述】:

我在 Behavior 中有一个动画,但它运行不流畅。

这是我的动画代码:

DoubleAnimationUsingKeyFrames animation = new DoubleAnimationUsingKeyFrames();

animation.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("(0).(1)", UIElement.RenderTransformProperty, RotateTransform.AngleProperty));

int keyFrameCount = 16;
double timeOffsetInSeconds = 0.1;
int targetValue = 12;

double totalAnimationLength = keyFrameCount * timeOffsetInSeconds;
double repeatInterval = RepeatInterval;
bool isShaking = IsShaking;

// Can't be less than zero and pointless to be less than total length
if (repeatInterval < totalAnimationLength)
    repeatInterval = totalAnimationLength;

animation.Duration = new Duration(TimeSpan.FromSeconds(repeatInterval));

for (int i = 0; i < keyFrameCount; i++)
{
    animation.KeyFrames.Add(new LinearDoubleKeyFrame(i % 2 == 0 ? targetValue : -targetValue, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(i * timeOffsetInSeconds))));
}

animation.KeyFrames.Add(new LinearDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(totalAnimationLength))));

但是,如果我选择

int keyFrameCount = 360;

for (int i = 0; i < keyFrameCount; i++)
{
    animation.KeyFrames.Add(new LinearDoubleKeyFrame(i, keyTime.FromTimeSpan(TimeSpan.FromSeconds(i * timeOffsetInSeconds))));
}

它会旋转一个非常平滑的圆圈。

我怎样才能让动画从0度到30度,回到-30度, 然后回到 0(让它稍微抖动一下)并让它看起来流利。

经过一些尝试,我发现(正常)来回在这里不起作用, 它的行为完全不受控制!

【问题讨论】:

    标签: c# wpf xaml animation behavior


    【解决方案1】:

    我不太清楚你为什么要自己做这么多关键帧,但为了做

    我怎样才能让动画从 0 度到 30 度,回到 -30 度,然后回到 0(让它稍微抖动一下)并让它看起来流畅。

    你可以把动画改成类似

    DoubleAnimationUsingKeyFrames animation = new DoubleAnimationUsingKeyFrames();
    
    animation.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("(0).(1)", UIElement.RenderTransformProperty, RotateTransform.AngleProperty));
    
    var totalAnimationLength = 1600; // Milliseconds
    
    double repeatInterval = 1600;// Milliseconds
    
    if (repeatInterval < totalAnimationLength) repeatInterval = totalAnimationLength; // Can't be less than zero and pointless to be less than total length 
    
    animation.Duration = new Duration(TimeSpan.FromMilliseconds(repeatInterval));
    animation.RepeatBehavior = RepeatBehavior.Forever; // assuming this was intended from having a repeat interval?
    
    animation.KeyFrames.Add(new LinearDoubleKeyFrame(30, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(totalAnimationLength * 0.25))));
    animation.KeyFrames.Add(new LinearDoubleKeyFrame(-30, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(totalAnimationLength * 0.75))));
    animation.KeyFrames.Add(new LinearDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(totalAnimationLength))));
    

    【讨论】:

    • 完美运行!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    相关资源
    最近更新 更多