WPF中如何自定義GridLengthAnimation-創(chuàng)新互聯(lián)

小編給大家分享一下WPF中如何自定義GridLengthAnimation,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、金水網(wǎng)絡(luò)推廣、成都微信小程序、金水網(wǎng)絡(luò)營銷、金水企業(yè)策劃、金水品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供金水建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:muchs.cn

需求


我們想在編輯一個(gè)列表中某一個(gè)條目時(shí),將編輯的詳情內(nèi)容也放置當(dāng)前面,比如右側(cè)。

可以通過將一個(gè)Grid,分成兩個(gè)Cloumn,動(dòng)態(tài)調(diào)整兩個(gè)Cloumn的Width,就可以實(shí)現(xiàn)這個(gè)需求。

我們知道,Clomun的Width是個(gè),而默認(rèn)的動(dòng)畫沒有這樣子的。我們就需要自己實(shí)現(xiàn)這樣一人動(dòng)畫。

設(shè)計(jì)
我們從Animation的類圖上看到

WPF中如何自定義GridLengthAnimation

我們可以從需求

我們想在編輯一個(gè)列表中某一個(gè)條目時(shí),將編輯的詳情內(nèi)容也放置當(dāng)前面,比如右側(cè)。

可以通過將一個(gè)Grid,分成兩個(gè)Cloumn,動(dòng)態(tài)調(diào)整兩個(gè)Cloumn的Width,就可以實(shí)現(xiàn)這個(gè)需求。

我們知道,Clomun的Width是個(gè)GridLength,而默認(rèn)的動(dòng)畫沒有這樣子的。我們就需要自己實(shí)現(xiàn)這樣一人動(dòng)畫。

設(shè)計(jì)

我們從Animation的類圖上看到AnimationTimeline繼承,重寫其GetCurrentValue

public class GridLengthAnimation : AnimationTimeline
  {
    /// <summary>
    /// Returns the type of object to animate
    /// </summary>
    public override Type TargetPropertyType => typeof(GridLength);
 
    /// <summary>
    /// Creates an instance of the animation object
    /// </summary>
    /// <returns>Returns the instance of the GridLengthAnimation</returns>
    protected override System.Windows.Freezable CreateInstanceCore()
    {
      return new GridLengthAnimation();
    }
 
    /// <summary>
    /// Dependency property for the From property
    /// </summary>
    public static readonly DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(GridLength),
      typeof(GridLengthAnimation));
 
    /// <summary>
    /// CLR Wrapper for the From depenendency property
    /// </summary>
    public GridLength From
    {
      get
      {
        return (GridLength)GetValue(GridLengthAnimation.FromProperty);
      }
      set
      {
        SetValue(GridLengthAnimation.FromProperty, value);
      }
    }
 
    /// <summary>
    /// Dependency property for the To property
    /// </summary>
    public static readonly DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(GridLength),
      typeof(GridLengthAnimation));
 
    /// <summary>
    /// CLR Wrapper for the To property
    /// </summary>
    public GridLength To
    {
      get
      {
        return (GridLength)GetValue(GridLengthAnimation.ToProperty);
      }
      set
      {
        SetValue(GridLengthAnimation.ToProperty, value);
      }
    }
 
    /// <summary>
    /// Animates the grid let set
    /// </summary>
    /// <param name="defaultOriginValue">The original value to animate</param>
    /// <param name="defaultDestinationValue">The final value</param>
    /// <param name="animationClock">The animation clock (timer)</param>
    /// <returns>Returns the new grid length to set</returns>
    public override object GetCurrentValue(object defaultOriginValue,
      object defaultDestinationValue, AnimationClock animationClock)
    {
      double fromVal = ((GridLength)GetValue(GridLengthAnimation.FromProperty)).Value;
 
      double toVal = ((GridLength)GetValue(GridLengthAnimation.ToProperty)).Value;
 
      if (fromVal > toVal)
        return new GridLength((1 - animationClock.CurrentProgress.Value) * (fromVal - toVal) + toVal, GridUnitType.Star);
      else
        return new GridLength(animationClock.CurrentProgress.Value * (toVal - fromVal) + fromVal, GridUnitType.Star);
    }

如上所示,我們仿著默認(rèn)動(dòng)畫實(shí)現(xiàn)了From,To,同時(shí)將其屬性定義為GridLength,當(dāng)動(dòng)畫執(zhí)行時(shí),我們重寫了GetCurrentValue,使其根據(jù)From/To屬性相關(guān)聯(lián)。

優(yōu)化


通過以上代碼,我們實(shí)現(xiàn)了在GridLength變化時(shí),實(shí)現(xiàn)動(dòng)畫。但是,試用后我們發(fā)現(xiàn),動(dòng)畫,有點(diǎn)太線性。這個(gè)時(shí)候,怎么辦?

可以通過引入EasingFunction來實(shí)現(xiàn)。我們知道EasingFunction其實(shí)就是一個(gè)與時(shí)間t有關(guān)的時(shí)間函數(shù)f(t).通過時(shí)間函數(shù)的處理,我們使動(dòng)畫過渡不要那么線性。

 /// <summary>
    /// The <see cref="EasingFunction" /> dependency property's name.
    /// </summary>
    public const string EasingFunctionPropertyName = "EasingFunction";
 
    /// <summary>
    /// Gets or sets the value of the <see cref="EasingFunction" />
    /// property. This is a dependency property.
    /// </summary>
    public IEasingFunction EasingFunction
    {
      get
      {
        return (IEasingFunction)GetValue(EasingFunctionProperty);
      }
      set
      {
        SetValue(EasingFunctionProperty, value);
      }
    }
 
    /// <summary>
    /// Identifies the <see cref="EasingFunction" /> dependency property.
    /// </summary>
    public static readonly DependencyProperty EasingFunctionProperty = DependencyProperty.Register(
      EasingFunctionPropertyName,
      typeof(IEasingFunction),
      typeof(GridLengthAnimation),
      new UIPropertyMetadata(null));

對應(yīng)的,還要重寫GetCurrentValue函數(shù)。

public override object GetCurrentValue(object defaultOriginValue,
      object defaultDestinationValue, AnimationClock animationClock)
    {
      double fromVal = ((GridLength)GetValue(FromProperty)).Value;
 
      double toVal = ((GridLength)GetValue(ToProperty)).Value;
 
      //check that from was set from the caller
      //if (fromVal == 1)
      //  //set the from as the actual value
      //  fromVal = ((GridLength)defaultDestinationValue).Value;
 
      double progress = animationClock.CurrentProgress.Value;
 
      IEasingFunction easingFunction = EasingFunction;
      if (easingFunction != null)
      {
        progress = easingFunction.Ease(progress);
      }
 
 
      if (fromVal > toVal)
        return new GridLength((1 - progress) * (fromVal - toVal) + toVal, GridUnitType.Star);
 
        return new GridLength(progress * (toVal - fromVal) + fromVal, GridUnitType.Star);
    }

使用


 <anim:GridLengthAnimation Storyboard.TargetProperty="Width" From="0" To="*" Duration="0:0:0.5"/>

以上是“WPF中如何自定義GridLengthAnimation”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文名稱:WPF中如何自定義GridLengthAnimation-創(chuàng)新互聯(lián)
文章出自:http://muchs.cn/article10/degsgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、做網(wǎng)站、網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)企業(yè)建站、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司