I know it's really late for answering this, but I'll just tell the way I chose to animate layout changes for someone in need.
Android has a special Animation class named ScaleAnimation
where we can smoothly expand or collapse views.
Show view by expanding diagonally:
ScaleAnimation expand = new ScaleAnimation(
0, 1.0f,
0, 1.0f,
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0);
expand.setDuration(250);
view.startAnimation(expand)
where the constructor used is:
ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
So you can change values accordingly.
For example, the below example would animate view horizontally:
ScaleAnimation expand = new ScaleAnimation(
0, 1.1f,
1f, 1f,
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0);
expand.setDuration(250);
You can change fromX
,toX
, fromY
& toY
according to the need.
For example if view is shown and you have to just expand it, put fromX
and fromY
to 1.0f
, and toX
, toY
according to the need.
Now, using the same class you can create a more cool effect for showing view by expanding view a little bit extra and then shrinking it to original size. For this purpose, AnimationSet
will be used. So it would create a kind of bubble effect.
Below example is for creating bubble effect for showing the view:
AnimationSet expandAndShrink = new AnimationSet(true);
ScaleAnimation expand = new ScaleAnimation(
0, 1.1f,
0, 1.1f,
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0);
expand.setDuration(250);
ScaleAnimation shrink = new ScaleAnimation(
1.1f, 1f,
1.1f, 1f,
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0);
shrink.setStartOffset(250);
shrink.setDuration(120);
expandAndShrink.addAnimation(expand);
expandAndShrink.addAnimation(shrink);
expandAndShrink.setFillAfter(true);
expandAndShrink.setInterpolator(new AccelerateInterpolator(1.0f));
view.startAnimation(expandAndShrink);