mrnateriver
mrnateriver

Reputation: 2112

How to change animations used by animateLayoutChanges mechanism?

I have a RecyclerView in which user selects an item. Upon selection certain view becomes visible before the text (see image). I want to animate its appearance.

Graphical representation

animateLayoutChanges property on the item's layout does the job perfectly, except for the animation type for view appearance - it fades in. I want it to scale from 0 to 100% size, I believe it's called 'pop up' animation.

If I disable animateLayoutChanges and use XML animation for that, it works, but the nearby text is no longer animated (it should slide to accomodate space for the view and its margin). It instantly shifts to the right and then animation is played. This is worse with reverse animation, since the text overlaps the view before it has disappeared.

So I need to combine default mechanism and my own animation somehow. What would be the simplest way to accomplish that without delving into custom animations?

Upvotes: 24

Views: 13764

Answers (3)

HasaDev
HasaDev

Reputation: 117

I found that i can set animation type and duration this way

rootLayout.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
rootLayout.layoutTransition.setDuration(3000)

Where rootLayout - is layout, where animationLayoutChanges = true

Upvotes: 2

Mirza Ahmed Baig
Mirza Ahmed Baig

Reputation: 5865

You can do same thing with animateLayoutChange just see following article

https://proandroiddev.com/the-little-secret-of-android-animatelayoutchanges-e4caab2fddec

Upvotes: 2

mrnateriver
mrnateriver

Reputation: 2112

After much digging in the wrong places, found the answer myself. Might help somebody.

animateLayoutChanges property if enabled utilizes an instance of LayoutTransition to do its job in ViewGroup.

So in order to change animations, you can create an instance of LayoutTransition, set Animator for the transition that you need to change and then assign that instance to ViewGroup via setLayoutTransition().

In my case the result is:

Animator scaleDown = ObjectAnimator.ofPropertyValuesHolder((Object)null, PropertyValuesHolder.ofFloat("scaleX", 1, 0), PropertyValuesHolder.ofFloat("scaleY", 1, 0));
scaleDown.setDuration(300);
scaleDown.setInterpolator(new OvershootInterpolator());

Animator scaleUp = ObjectAnimator.ofPropertyValuesHolder((Object)null, PropertyValuesHolder.ofFloat("scaleX", 0, 1), PropertyValuesHolder.ofFloat("scaleY", 0, 1));
scaleUp.setDuration(300);
scaleUp.setStartDelay(300);
scaleUp.setInterpolator(new OvershootInterpolator());

LayoutTransition itemLayoutTransition = new LayoutTransition();
itemLayoutTransition.setAnimator(LayoutTransition.APPEARING, scaleUp);
itemLayoutTransition.setAnimator(LayoutTransition.DISAPPEARING, scaleDown);

ViewGroup av = (ViewGroup)v.findViewById(R.id.animated_layout);
av.setLayoutTransition(itemLayoutTransition);

More info in LayoutTransition reference documentation.

Upvotes: 43

Related Questions