2017-07-12 30 views
0

我想知道,是否可以在Visual Studio Xamarin中爲視圖製作高度/寬度動畫?例如,當用戶點擊一個按鈕時,視圖的高度會改變。Android Visual Studio中的高度/寬度動畫Xamarin

我在網上尋找解決方案,但只找到Xamarin.Forms解決方案,但我需要的解決方案不適用於XamarinForms。使用scaleY不是解決方案。

+0

所以不Xamarin的形式,但Xamarin的Android(母語)? – Digitalsa1nt

+0

To @ Digitalsa1nt Exactly –

回答

0

所以,我找到了解決辦法。我們必須使用ValueAnimator。

clickedButton.Click += (s,e) =>{ 
    int viewHeight = animatedView.Height; 
    ValueAnimator animator = ValueAnimator.OfInt(viewHeight, 0); 
    animator.SetDuration(500); 
    animator.Update += (object sender, ValueAnimator.AnimatorUpdateEventArgs e) => 
    { 
     var value = (int)animator.AnimatedValue; 
     ViewGroup.LayoutParams layoutParams = animatedView.LayoutParameters; 
     layoutParams.Height = value; 
     animatedView.LayoutParameters = layoutParams; 
    }; 
    animator.Start(); 
} 

或者,我們可以創建倒塌功能/擴展動畫

ValueAnimator animateCollapse(int from, int to, int duration, View animatedView) 
{ 
    ValueAnimator animator = ValueAnimator.OfInt(from, to); 
    animator.SetDuration(duration); 
    animator.Update += (object sender, ValueAnimator.AnimatorUpdateEventArgs e) => 
    { 
     var value = (int)animator.AnimatedValue; 
     ViewGroup.LayoutParams layoutParams = animatedView.LayoutParameters; 
     layoutParams.Height = value; 
     animatedView.LayoutParameters = layoutParams; 
    }; 
    return animator; 
} 
clickedButton.Click += (s,e) =>{ 
    ValueAnimator animator; 
    if (isExpanded){ 
     isExpanded = false; 
     animator = animateCollapse(originalHeight, 0, 500, animatedView); 
    } 
    else{ 
     isExpanded = true; 
     animator = animateCollapse(0, originalHeight, 500, animatedView); 
    } 
    animator.Start(); 
}