0

我在 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(让它稍微抖动一下)并让它看起来流畅。

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

4

1 回答 1

2

我不太确定你为什么要自己做这么多关键帧,但是为了做

我怎样才能让动画从 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))));
于 2012-07-04T23:54:04.790 回答