Animation method applyTransformation not triggered until I click any layout

回眸只為那壹抹淺笑 提交于 2020-01-03 09:38:21

问题


This is so weird, I've this animation code:

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;
        }
    }
}

And I use this Animation:

View parent = (View) v.getParent();
View containerMenu = parent.findViewById(R.id.containerMenu);
ExpandAnimation anim=new ExpandAnimation(containerMenu, 1000);
containerMenu.startAnimation(anim);

This animation toggle a layout hidding / showing it.

By default, its hidden. When I click, animation works and it's shown. When I click again, it shrinks correctly. But the 3rd time, it does nothing. I've debugged and I found out that the constructor is called but not applyTransformation. Somehow, if I click any layout around the screen, the animation suddenly starts.

Any idea?

Edit Does anyone know WHEN is applyTransformation triggered?


回答1:


I can't understand why, but when I click or do any action to any layout, the animation finally starts. So I programatically added a workaround. I've a scrollview in my layout, so I move the scroll position:

hscv.scrollTo(hscv.getScrollX()+1, hscv.getScrollY()+1);

This just after containerMenu.startAnimation(anim);

This just works, I can't understand why.

Also, I found out that some animations worked flawless on android > 4, but on 2.3 for instance, it had the same issue, worked to expand, and to shrink, but not to expand for the second time.

parent.invalidate();

Did the trick.



来源:https://stackoverflow.com/questions/15656113/animation-method-applytransformation-not-triggered-until-i-click-any-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!