Trying to reset values from Property Animator to be used in recycler view

天大地大妈咪最大 提交于 2019-12-25 08:48:14

问题


Been doing some animation inside of a row in RecyclerView (not the row itself. Imagine expanding text) and there are times when the animation leaks to other recycled views which should not have that animation in them.

Since I use property animation the scale action resizes the inner view and the leak can be seen in 2 aspects: 1) The animation will go on (This I could overcome with some guards) 2) The view has been resized and stopped in its tracks and so it will be reflected in the recycled view.

How can I reset the views to their original state? I have tried many approaches from posts but none solved it. The closest definition I got was in this unanswered post: How to reset view to original state after using animators to animates its some properties?

Here is a sample of how I set up my animation in onBind (this one has an attempt to use onAnimationEnd which I found in one post but did not work)

ObjectAnimator scaleXUp = ObjectAnimator.ofFloat(mView, View.SCALE_X, 10f);
        scaleXUp.setRepeatCount(ValueAnimator.INFINITE);
        scaleXUp.setRepeatMode(ValueAnimator.REVERSE);
        scaleXUp.setDuration(700);
        ObjectAnimator scaleYUp = ObjectAnimator.ofFloat(mView, View.SCALE_Y, 10f);
        scaleYUp.setRepeatCount(ValueAnimator.INFINITE);
        scaleYUp.setRepeatMode(ValueAnimator.REVERSE);
        scaleYUp.setDuration(700);
        mTotalAnimation = new AnimatorSet();
        mTotalAnimation.play(scaleXUp).with(scaleYUp);
        mTotalAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        mTotalAnimation.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                animation.removeListener(this);
                animation.setDuration(0);
                for(Animator va : ((AnimatorSet)animation).getChildAnimations()) {
                    ((ValueAnimator)va).reverse();
                }
            }
        });
        mTotalAnimation.start();

And here is what I do in the onUnbindData:

if (mTotalAnimation != null) {
        mTotalAnimation.end();
        mTotalAnimation = null;
    }

And as I saw many people like the clearAnimation approach - tried and did not work either.


回答1:


4 days and not one response but meanwhile I solved it myself. So the approach was close, just wrongly placed.

I added this method:

private void stopAnimation() {
    for (Animator anim : mTotalAnimation.getChildAnimations()) {
        ((ObjectAnimator) anim).reverse();
        anim.end();
    }
}

and I call that when I want to reset the view.

Here I am getting the animations from the AnimatorSet and reversing and ending each. I did not understand why I had to do it manually but it looks like this ability will be added in Android O: https://developer.android.com/preview/api-overview.html#aset

Starting in Android O, the AnimatorSet API now supports seeking and playing in reverse. Seeking lets you set the position of the animation set to a specific point in time. Playing in reverse is useful if your app includes animations for actions that can be undone. Instead of defining two separate animation sets, you can play the same one in reverse.



来源:https://stackoverflow.com/questions/45629326/trying-to-reset-values-from-property-animator-to-be-used-in-recycler-view

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