Reputation: 171
I'd like to know, is it possible to make height/width animation for View in Visual Studio Xamarin? For example, when a user clicks on a button the height of a view changes.
I looked for solution on the Internet, but only found Xamarin.Forms solution, but I need solution not for XamarinForms. Using scaleY is not a solution.
Upvotes: 0
Views: 1092
Reputation: 171
So, I have found solution. We have to use 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();
}
Or we can create function for collapse/expand animation
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();
}
Upvotes: 4