Android / Monodroid ApplyTransformation未调用

时间:2013-03-14 12:27:32

标签: android animation xamarin.android

我正在使用自定义动画来扩展和收缩我的视图,在SO上的另一个答案中找到。我的问题是,ApplyTransformation永远不会被调用,因此没有任何反应。

我还有什么别的吗?

private void BrandTextClicked (object sender, EventArgs e)
{
    Animation animation = null;
    if (expanded) {
        animation = new ExpandAnimation (listView, 0, height);
    } else {
        animation = new ExpandAnimation(listView, height, 0);
    }
    animation.Duration = 500;
    animation.Interpolator = new AccelerateInterpolator(1);
    listView.Animation = animation;
    animation.StartNow ();
    listView.Invalidate ();

    expanded = !expanded;
}

...

public class ExpandAnimation : Animation {
    private int mStartHeight;
    private int mDeltaHeight;
    private View mContent;

    public ExpandAnimation(View content, int startHeight, int endHeight) : base() {
        mContent = content;
        mStartHeight = startHeight;
        mDeltaHeight = endHeight - startHeight;
    }

    public override void Initialize (int width, int height, int parentWidth, int parentHeight)
    {
        base.Initialize (width, height, parentWidth, parentHeight);
    }

    protected override void ApplyTransformation(float interpolatedTime, Transformation t) {
        ViewGroup.LayoutParams lp = mContent.LayoutParameters;
        lp.Height = (int) (mStartHeight + mDeltaHeight *
                           interpolatedTime);
        mContent.LayoutParameters = lp;
        mContent.RequestLayout();
    }

    public override bool WillChangeBounds() {
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

我最终使用了Android.Animation包/名称空间和ValueAnimation,如下所示:

    ValueAnimator animator = null;
    if (expanded) {
        handle.SetImageResource(Android.Resource.Drawable.ArrowDownFloat);
        animator = ValueAnimator.OfObject (new HeightEvaluator (listView), height, 0);
    } else {
        handle.SetImageResource(Android.Resource.Drawable.ArrowUpFloat);
        animator = ValueAnimator.OfObject (new HeightEvaluator (listView), 0, height);
    }

    animator.SetDuration(500);
    animator.SetInterpolator(new AccelerateInterpolator (1));
    animator.Start ();

    expanded = !expanded;