1

我已閱讀如何在Android中執行此操作,但我似乎無法找到Xamarin Android等效於動畫布局的LeftMargin和TopMargin。顯然Xamarin有「動畫」,但我無法弄清楚Xamarin中的「applyTransformation」部分,我設置了「newLeftMargin * interpolatedTime」。Animate LayoutParams LeftMargin和TopMargin

下面是標準的Android參考: Android - Change left margin using animation

Animation a = new Animation(); 
//applyTransformation???? with "newLeftMargin * interpolatedTime" 
a.Duration = 500; 
MyThingy.StartAnimation(a); 

感謝user Apineda提供答案。這是我最終編寫的代碼,以防萬一任何人想要它。第一個構造函數從當前狀態動畫邊距,而不是從零開始。第二個構造函數要求您指定起始頁邊距。

class LayoutMarginAnimation : Android.Views.Animations.Animation 
{ 
    private ViewGroup ViewToTransform; 
    private int LeftMargin_Destination; 
    private int TopMargin_Destination; 
    private int LeftMargin_Source; 
    private int TopMargin_Source; 

    /// <summary> 
    /// Animates a layout from it's current margins to specified margins 
    /// </summary> 
    /// <param name="a_viewToTransform">A view to transform.</param> 
    /// <param name="a_LeftMargin_Destination">A left margin destination.</param> 
    /// <param name="a_TopMargin_Destination">A top margin destination.</param> 
    public LayoutMarginAnimation(
     ViewGroup a_viewToTransform, 
     int a_LeftMargin_Destination, 
     int a_TopMargin_Destination 
    ) 
    { 
     this.ViewToTransform = a_viewToTransform; 
     this.LeftMargin_Destination = a_LeftMargin_Destination; 
     this.TopMargin_Destination = a_TopMargin_Destination; 

     this.LeftMargin_Source = (this.ViewToTransform.LayoutParameters as RelativeLayout.LayoutParams).LeftMargin; 
     this.TopMargin_Source = (this.ViewToTransform.LayoutParameters as RelativeLayout.LayoutParams).TopMargin; 
    } 

    /// <summary> 
    /// Animates a layout from specified margins to specified margins, regardless of what the margins are currently set to. 
    /// </summary> 
    /// <param name="a_viewToTransform">A view to transform.</param> 
    /// <param name="a_LeftMargin_Source">A left margin source.</param> 
    /// <param name="a_TopMargin_Source">A top margin source.</param> 
    /// <param name="a_LeftMargin_Destination">A left margin destination.</param> 
    /// <param name="a_TopMargin_Destination">A top margin destination.</param> 
    public LayoutMarginAnimation(
     ViewGroup a_viewToTransform, 
     int a_LeftMargin_Source, 
     int a_TopMargin_Source, 
     int a_LeftMargin_Destination, 
     int a_TopMargin_Destination 
    ) 
    { 
     this.ViewToTransform = a_viewToTransform; 
     this.LeftMargin_Destination = a_LeftMargin_Destination; 
     this.TopMargin_Destination = a_TopMargin_Destination; 
     this.LeftMargin_Source = a_LeftMargin_Source; 
     this.TopMargin_Source = a_TopMargin_Source; 
    } 

    protected override void ApplyTransformation(float interpolatedTime, Transformation t) 
    { 
     //Console.WriteLine("ApplyTransformation with interpolatedTime = " + interpolatedTime); 
     RelativeLayout.LayoutParams layoutParams = this.ViewToTransform.LayoutParameters as RelativeLayout.LayoutParams; 

     layoutParams.LeftMargin = this.LeftMargin_Source + (int)((this.LeftMargin_Destination - this.LeftMargin_Source) * interpolatedTime); 
     layoutParams.TopMargin = this.TopMargin_Source + (int)((this.TopMargin_Destination - this.TopMargin_Source) * interpolatedTime); 

     this.ViewToTransform.LayoutParameters = layoutParams; 
    } 
} 

這裏是如何調用兩個構造函數。

  // Animates a layout from it's current margins to specified margins 
      LayoutMarginAnimation animation = new LayoutMarginAnimation(this.DraggableSeedImageContainer, 1000, 1000); 
      // Animates a layout from specified margins to specified margins, regardless of what the margins are currently set to. 
      //LayoutMarginAnimation animation = new LayoutMarginAnimation(this.DraggableSeedImageContainer, 200, 200, 1000, 1000); 
      animation.Duration = 500; 
      this.DraggableSeedImageContainer.StartAnimation(animation); 

回答

1

你有問題要做,因爲Animation類是一個抽象類。您必須創建您自己的實現並覆蓋ApplyTransformation()方法。

使用你提供的鏈接,將其翻譯爲Xamarin.Android我們:

我的自定義動畫類:

class ViewLeftMargingAnimation : Animation 
{ 
    View _viewToTransform; 

    int _newLeftMargin; 

    public ViewLeftMargingAnimation (View viewToTransform, int newLeftMargin) 
    { 
     _viewToTransform = viewToTransform; 

     _newLeftMargin = newLeftMargin; 
    } 

    protected override void ApplyTransformation (float interpolatedTime, Transformation t) 
    { 
     var layoutParams = (LinearLayout.LayoutParams)_viewToTransform.LayoutParameters; 
     layoutParams.LeftMargin = (int)(_newLeftMargin * interpolatedTime); 
     _viewToTransform.LayoutParameters = layoutParams; 
    } 
} 

使用您的動畫:

// View that will be animated: 
var button = FindViewById<Button> (Resource.Id.myButton); 

// Animation object: 
var a = new ViewLeftMargingAnimation (button, 150); 

// Animation's duration: 
a.Duration = 500; 

// Start my animation 
button.StartAnimation (a); 

這可能也可以使用ValueAnimator類。更多關於Xamarin.Android動畫here

+0

Ahhh我以前做過這個。這正是我所期待的,謝謝! – LampShade

相關問題