动画方法applyTransformation直到我点击任何布局才触发

Rei*_*erd 6 android android-animation android-layout

这太奇怪了,我有这个动画代码:

public class ExpandAnimation extends Animation {
private View mAnimatedView;
private MarginLayoutParams mViewLayoutParams;
private int mMarginStart, mMarginEnd;
private boolean mWasEndedAlready = false;

/**
* Initialize the animation
* @param view The layout we want to animate
* @param duration The duration of the animation, in ms
*/
    public ExpandAnimation(View view, int duration) {
        setDuration(duration);
        mAnimatedView = view;
        mViewLayoutParams = (MarginLayoutParams) view.getLayoutParams();

        mMarginStart = mViewLayoutParams.rightMargin;
        mMarginEnd = (mMarginStart == 0 ? (0- view.getWidth()) : 0);
        view.setVisibility(View.VISIBLE);
        mAnimatedView.requestLayout();
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        if (interpolatedTime < 1.0f) {
            // Calculating the new bottom margin, and setting it
            mViewLayoutParams.rightMargin = mMarginStart
                    + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
            // Invalidating the layout, making us seeing the changes we made
            mAnimatedView.requestLayout();

        // Making sure we didn't run the ending before (it happens!)
        } else if (!mWasEndedAlready) {
            mViewLayoutParams.rightMargin = mMarginEnd;
            mAnimatedView.requestLayout();
            mWasEndedAlready = true;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用这个动画:

View parent = (View) v.getParent();
View containerMenu = parent.findViewById(R.id.containerMenu);
ExpandAnimation anim=new ExpandAnimation(containerMenu, 1000);
containerMenu.startAnimation(anim);
Run Code Online (Sandbox Code Playgroud)

此动画切换布局隐藏/显示它.

默认情况下,它隐藏.当我点击时,动画工作并显示出来.当我再次单击时,它会正确收缩.但第三次,它什么也没做.我调试了,我发现构造函数被调用但没有 applyTransformation. 不知何故,如果我点击屏幕周围的任何布局,动画会突然开始.

任何的想法?

编辑 有没有人知道什么时候触发了applyTransformation?

Rei*_*erd 9

我无法理解为什么,但当我点击或对任何布局做任何动作时,动画终于开始了.所以我以编程方式添加了一个解决方法.我的布局中有一个scrollview,所以我移动了滚动位置:

hscv.scrollTo(hscv.getScrollX()+1, hscv.getScrollY()+1);
Run Code Online (Sandbox Code Playgroud)

这之后 containerMenu.startAnimation(anim);

这只是有效,我无法理解为什么.

此外,我发现有些动画在android> 4上运行完美,但在2.3上,例如,它有相同的问题,努力扩展,缩小,但不是第二次扩展.

parent.invalidate();
Run Code Online (Sandbox Code Playgroud)

诀窍.