Let's say I have a vertical linearLayout with :
[v1]
[v2]
By default v1 has visibily = GONE. I would like to show v1 with an expand animation and push down v2 at the same time.
I tried something like this:
Animation a = new Animation()
{
int initialHeight;
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final int newHeight = (int)(initialHeight * interpolatedTime);
v.getLayoutParams().height = newHeight;
v.requestLayout();
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
initialHeight = height;
}
@Override
public boolean willChangeBounds() {
return true;
}
};
But with this solution, I have a blink when the animation starts. I think it's caused by v1 displaying full size before the animation is applied.
With javascript, this is one line of jQuery! Any simple way to do this with android?
Adding to Tom Esterez's excellent answer and Erik B's excellent update to it, I thought I'd post my own take, compacting the expand and contract methods into one. This way, you could for example have an action like this...
... which calls the method below and letting it figure out what to do after each onClick()...
Ok, I just found a VERY ugly solution :
Feel free to propose a better solution !
I would like to add something to the very helpful answer above. If you don't know the height you'll end up with since your views .getHeight() returns 0 you can do the following to get the height:
Where DUMMY_HIGH_DIMENSIONS is the width/height (in pixels) your view is constrained to ... having this a huge number is reasonable when the view is encapsulated with a ScrollView.
@Tom Esterez's answer, but updated to use view.measure() properly per Android getMeasuredHeight returns wrong values !
Here is my solution. I think it is simpler. It only expands the view but can easy be extended.
This was my solution, my
ImageView
grows from100%
to200%
and return to his original size, using two animation files insideres/anim/
folderanim_grow.xml
anim_shrink.xml
Send an
ImageView
to my methodsetAnimationGrowShrink()
setAnimationGrowShrink()
method: