我正在尝试应用动画来折叠 LinearLayout。此动画取决于 LinearLayout 的 MeasuredHeight 属性,其值大于 0,但该值始终为 0。
这是我正在使用的代码:
public void Collapse()
{
var v = FindViewById<LinearLayout>(Resource.Id.layoutBranding);
int initialHeight = v.MeasuredHeight; // always 0!!
var a = new MyAnimation(v, initialHeight);
a.Duration = (int)(initialHeight / v.Context.Resources.DisplayMetrics.Density);
v.StartAnimation(a);
}
其中 MyAnimation 定义为:
public class MyAnimation : Animation { private readonly View _view; 私有只读 int _initialHeight;
public MyAnimation(View view, int initalHeight)
{
_view = view;
_initalHeight = initalHeight;
}
protected override void ApplyTransformation(float interpolatedTime, Transformation t)
{
if (interpolatedTime == 1)
{
_view.Visibility = ViewStates.Gone;
}
else
{
_view.LayoutParameters.Height = _initalHeight - (int) (_initalHeight*interpolatedTime);
_view.RequestLayout();
}
}
public override bool WillChangeBounds()
{
return true;
}
}