程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WPF與緩動(二) 正弦與余弦緩動

WPF與緩動(二) 正弦與余弦緩動

編輯:關於.NET

如果要比二次緩動(請參見WPF與緩動(一) N次緩動)更平緩, 可以使用正弦或余弦緩動.

如果我們用曲線上點的斜率表示速度,可以發現,由0到PI/2,速度逐漸減小,從PI/2到PI速度逐漸增加.

我們可以總結出如下公式

其中位置的改變量相當於Animation中的To與From的差值, t/t(總)相當於animationClock.CurrentProgress.Value, b實際就是From值

參考如下代碼:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media.Animation;
using System.Windows;

namespace EaseMoveDemo
{
  public class EaseMoveAnimation : DoubleAnimationBase
  {

    public static readonly DependencyProperty FromProperty = DependencyProperty.Register(
      "From", typeof(double?), typeof(EaseMoveAnimation), new PropertyMetadata(null));

    public static readonly DependencyProperty ToProperty = DependencyProperty.Register(
      "To", typeof(double?), typeof(EaseMoveAnimation), new PropertyMetadata(null));

    public double? From
    {
      get
      {
        return (double?)this.GetValue(EaseMoveAnimation.FromProperty);
      }
      set
      {
        this.SetValue(EaseMoveAnimation.FromProperty, value);
      }
    }

    public double? To
    {
      get
      {
        return (double?)this.GetValue(EaseMoveAnimation.ToProperty);
      }
      set
      {
        this.SetValue(EaseMoveAnimation.ToProperty, value);
      }
    }

    //正余弦緩動
    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;

      //加速
      //return delta * (1 - Math.Cos(Math.PI / 2 * animationClock.CurrentProgress.Value)) + from;

      //減速
      //return delta * Math.Sin(Math.PI / 2 * animationClock.CurrentProgress.Value) + from;

      //先加速,後減速
      return delta/2 * (1 - Math.Cos(Math.PI * animationClock.CurrentProgress.Value)) + from;

    }

    protected override System.Windows.Freezable CreateInstanceCore()
    {
      return new EaseMoveAnimation();
    }
  }
}

本文配套源碼

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved