An Alternative Fragment Transition Animations Implementation
A common pattern is emerging for doing a view translation animation in #Android
based on percentage using the new property-based animators. This comes from a desire to mimic the "precent relative" behavior from the old animation system. It seems that, most often, the use case is tied to fragment transitions.
The crux of this pattern is to subclass the root view of your fragment's hierarchy and provide additional setters/getters for a new view "property" called X/Yfraction. Then you can declare a basic ObjectAnimator
with the new propertyName
in XML. This works well, and I've used this technique also.
However, sometimes the requirement of adding a subclass for every ViewGroup
type you want to use in a fragment may not fit well with your code. What I've provided in the linked gist is a method of using custom Property
elements to provide the same functionality to any View
So, then, how would we attach these new properties to the fragment transition? We can't reference new property types in animation XML (yet), so we have to add a little code via Fragment.onCreateAnimator()
to return the appropriate animation for the transition using our custom property."Uh, Dave...that still requires subclassing."
True, but in almost all cases you already subclassed Fragment
, you may not have already created custom views.
The other drawback to this approach is that we can't define the Animator
elements in XML, we have to do this in Java code to attach an instance to our new Property
I won't argue this method as better or worse than what you've already seen. However, if making custom root views doesn't fit your application's paradigm, here's an alternative that perhaps you will find useful :) #AndroidDev