That can be done with Scenes and Transitions API.
Framework gives us three Transition
types out of the box: Fade
, Slide
and Explode
, but you can also create your custom type of transition extending Visibility
class and overriding appropriate methods.
So, having any ViewGroup
, we can do this:
viewGroup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
Transition t = null;
if (i == 1) {
t = new Fade();
} else if (i == 2) {
t = new Slide(Gravity.BOTTOM);
} else if (i == 3) {
t = TransitionInflater.from(v.getContext())
.inflateTransition(R.transition.my_transition);
}
Button button = new Button(v.getContext());
button.setText("My button " + i++);
TransitionManager.beginDelayedTransition(customLayout, t);
viewGroup.addView(button);
}
});
Where my_transition.xml is following:
<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:interpolator="@android:interpolator/fast_out_slow_in">
<fade/>
<slide android:slideEdge="bottom"/>
</transitionSet>
We ll get this result:
Note, that we have to perform TransitionManager.beginDelayedTransition()
before any change to layout has been made (i.e. before addView()
is called). Then framework will take care of the rest.
There is also another overload TransitionManager.beginDelayedTransition(ViewGroup)
, where you do not need to specify what exact transition you want to be applied, and system will perform AutoTransition
animation, which basically will fade and change boundaries of animated view.
Update sum up from conversation in comments
Framework s TransitionManager
is available from API 19, and fully supported from API 21 (by saying fully I mean e.g. Slide
transition is available from API 21). Although there is support package available, but it doesn t backport all the functionality. Alternatively, you can move to TransitionsEverywhere library, which backports everything up to Android 4.0.