2

我正在尝试应用动画来折叠 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;
    }
}
4

3 回答 3

5

尝试这个:

v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);


int initialHeight = v.MeasuredHeight; 
于 2013-09-11T11:20:08.020 回答
1

尝试测量视图的高度和宽度 -

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus)
        {
            // Get the height and the width of the view 
        }
    }
于 2013-09-11T11:00:04.473 回答
0

在 View 真正布局之前(就在渲染之前),它的高度和宽度始终为 0

如果你测量onMeasureonLayout(我认为)的高度,那么它应该有一个非零高度

于 2013-09-11T10:53:35.803 回答