[英]immediate translate animation in WPF in code behind
我想使其涉及平移的圆的动画_fixedCircle
从起点(5,0),结束点(200,0)上的x轴,然后从(200,0)到(5,0) 在一个循环永远但不遵循路径,而是“跳跃”,换句话说,是从点到点的立即翻译。
要为该圆制作动画以进行线性插值,请执行以下操作:
Storyboard sb = new Storyboard();
DoubleAnimation anim = new DoubleAnimation();
sb.Children.Add(anim);
Storyboard.SetTarget(anim, _fixedCircle);
Storyboard.SetTargetProperty(anim, new PropertyPath("(Canvas.Left)"));
anim.From = 5;
anim.To = 200;
anim.RepeatBehavior = RepeatBehavior.Forever;
anim.AutoReverse = true;
MYCANVAS.Resources.Add("CIRCLE_ID", sb);
Duration duration = new Duration(TimeSpan.FromSeconds(2));
anim.Duration = duration;
anim.Begin();
但是如何创建“跳跃”效果呢?
尝试使用DoubleAnimationUsingKeyFrames
,为想要的每个跳转添加一个DiscreteDoubleKeyFrame
,例如(跳转值的计算有很大的改进空间;):
DoubleAnimationUsingKeyFrames anim = new DoubleAnimationUsingKeyFrames();
int maxNrOfJumps = 10;
for (int jump = 0; jump < maxNrOfJumps; jump++) {
double value = ((200 - 5) / maxNrOfJumps) * jump;
anim.KeyFrames.Add(new DiscreteDoubleKeyFrame(value, KeyTime.FromPercent(jump / (double)maxNrOfJumps)));
}
离散关键帧(例如DiscreteDoubleKeyFrame)会在值之间创建突然的“跳跃”(无插值)。 换句话说,直到达到关键帧的关键时间,动画属性才会改变,此时动画属性会突然变为目标值。
编辑:
无需Storyboard
即可运行它,这是我对其进行测试的方式:
_fixedCircle.BeginAnimation(Canvas.LeftProperty, anim);
编辑2:
您仍然需要设置以下属性以保持动画运行:
anim.RepeatBehavior = RepeatBehavior.Forever;
anim.AutoReverse = true;
另外,不要忘记设置Duration
...
立即点对点翻译
您是否正在寻找从第一个点到最后一个点的即时传送?
如果是这样的话 :
Storyboard sb = new Storyboard();
DoubleAnimation anim = new DoubleAnimation();
sb.Children.Add(anim);
Storyboard.SetTarget(anim, _fixedCircle);
Storyboard.SetTargetProperty(anim, new PropertyPath("(Canvas.Left)"));
anim.From = 5;
anim.To = 200;
// anim.RepeatBehavior = RepeatBehavior.Forever; // nope
anim.AutoReverse = false; // well
MYCANVAS.Resources.Add("CIRCLE_ID", sb);
Duration duration = new Duration(TimeSpan.FromSeconds(0)); // here we go
anim.Duration = duration;
anim.Begin();
为什么不使用skipToFill
属性? 只需将其添加到storyboard
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.