WPF:各種easing 函式(Easing Function)動畫預覽切換

來源:互聯網
上載者:User

寫了一個小程式,用來示範WPF中的各種easing 函式(Easing Function)動畫類型。

 

使用者可以選擇動畫類型,包含所有WPF 4.0定義的類型:

BackEase, BounceEase, CircleEase, CubicEase, ElasticEase, ExponentialEase, PowerEase, QuadraticEase, QuarticEase, QuinticEase, SineEase

 

緩動模式(即EasingMode枚舉):

EaseIn, EaseOut, EaseInOut

 

 

原理則是在資源中定義好Storyboard:

<Storyboard x:Key="storyboard" Storyboard.TargetName="ellipse">

    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Canvas.Left)">

        <EasingDoubleKeyFrame KeyTime="0:0:0" Value="40"/>

        <EasingDoubleKeyFrame KeyTime="0:0:3" Value="300"/>

    </DoubleAnimationUsingKeyFrames>

    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Canvas.Top)">

        <EasingDoubleKeyFrame KeyTime="0:0:0" Value="40"/>

        <EasingDoubleKeyFrame KeyTime="0:0:3" Value="150"/>

    </DoubleAnimationUsingKeyFrames>

</Storyboard>

 

然後在代碼中根據使用者選擇產生EasingFunctionBase,和EasingMode。最後再設定Storyboard動畫中的參數,然後開始動畫,如下代碼:

void SetKeyFrame(Storyboard storyboard, int idx, EasingFunctionBase easingFunc, EasingMode mode)

{

    //第一個子Timeline是DoubleAnimationUsingKeyFrames

    var frames = storyboard.Children[0] as DoubleAnimationUsingKeyFrames;

    if (frames != null)

    {

        //第一個KeyFrame是時間為0的初始值

        //這裡要設定第二個KeyFrame

        var easingkey = (EasingDoubleKeyFrame)frames.KeyFrames[1];

        easingFunc.EasingMode = mode;

        easingkey.EasingFunction = easingFunc;

    }

}

 

原始碼下載

注意:此為微軟SkyDrive存檔,請用瀏覽器直接下載,用某些下載工具可能無法下載

原始碼環境:Microsoft Visual C# 2010 Express

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.