I\'m trying to animate the transition between fragments. I got the answer from the following
Android Fragments and animation
FragmentTransaction ft = g
You need to use the new android.animation
framework (object animators) with FragmentTransaction.setCustomAnimations
as well as FragmentTransaction.setTransition
.
Here's an example on using setCustomAnimations
from ApiDemos' FragmentHideShow.java:
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
and here's the relevant animator XML from res/animator/fade_in.xml:
Note that you can combine multiple animators using
, just as you could with the older animation framework.
EDIT: Since folks are asking about slide-in/slide-out, I'll comment on that here.
You can of course animate the translationX
, translationY
, x
, and y
properties, but generally slides involve animating content to and from off-screen. As far as I know there aren't any transition properties that use relative values. However, this doesn't prevent you from writing them yourself. Remember that property animations simply require getter and setter methods on the objects you're animating (in this case views), so you can just create your own getXFraction
and setXFraction
methods on your view subclass, like this:
public class MyFrameLayout extends FrameLayout {
...
public float getXFraction() {
return getX() / getWidth(); // TODO: guard divide-by-zero
}
public void setXFraction(float xFraction) {
// TODO: cache width
final int width = getWidth();
setX((width > 0) ? (xFraction * width) : -9999);
}
...
}
Now you can animate the 'xFraction' property, like this:
res/animator/slide_in.xml:
Note that if the object you're animating in isn't the same width as its parent, things won't look quite right, so you may need to tweak your property implementation to suit your use case.