原文:
WPF与缓动(三) 指数缓动
WPF与缓动(三) 指数缓动
周银辉
指数缓动给人的感觉是加速度很大.
它的原始公式来自:P(t) = Math.Pow(2, 10*(t-1));
与其他缓动一样,我在这里就直接给出其核心代码了:
//
指数缓动
protected override double GetCurrentValueCore( double defaultOriginValue, double defaultDestinationValue, AnimationClock animationClock)
{
double from = (this.From==null?defaultDestinationValue:(double)this.From);
double to = (this.To==null?defaultOriginValue:(double)this.To);
double delta = to - from;
double value = animationClock.CurrentProgress.Value;
//加速
//return delta * Math.Pow(2, 10 * (value - 1)) + from;
//减速
//return delta * (-Math.Pow(2, -10 * value) + 1) + from;
//先加速,后减速
double t = value * this.Duration.TimeSpan.Ticks;
double d = this.Duration.TimeSpan.Ticks;
if ((t/=(d/2)) < 1)
{
return delta / 2 * Math.Pow(2, 10 * (t - 1)) + from;
}
return delta / 2 * (-Math.Pow(2, -10 * --t) + 2) + from;
}
protected override double GetCurrentValueCore( double defaultOriginValue, double defaultDestinationValue, AnimationClock animationClock)
{
double from = (this.From==null?defaultDestinationValue:(double)this.From);
double to = (this.To==null?defaultOriginValue:(double)this.To);
double delta = to - from;
double value = animationClock.CurrentProgress.Value;
//加速
//return delta * Math.Pow(2, 10 * (value - 1)) + from;
//减速
//return delta * (-Math.Pow(2, -10 * value) + 1) + from;
//先加速,后减速
double t = value * this.Duration.TimeSpan.Ticks;
double d = this.Duration.TimeSpan.Ticks;
if ((t/=(d/2)) < 1)
{
return delta / 2 * Math.Pow(2, 10 * (t - 1)) + from;
}
return delta / 2 * (-Math.Pow(2, -10 * --t) + 2) + from;
}
点击这里下载源代码