Android: Expand/collapse animation -
let's have vertical linearlayout :
[v1] [v2]
by default v1 has visibily = gone. show v1 expand animation , push down v2 @ same time.
i tried this:
animation = 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 solution, have blink when animation starts. think it's caused v1 displaying full size before animation applied.
with javascript, 1 line of jquery! simple way android?
i see question became popular post actual solution. main advantage don't have know expanded height apply animation , once view expanded, adapts height if content changes. works great me.
public static void expand(final view v) { v.measure(layoutparams.match_parent, layoutparams.wrap_content); final int targetheight = v.getmeasuredheight(); // older versions of android (pre api 21) cancel animations views height of 0. v.getlayoutparams().height = 1; v.setvisibility(view.visible); animation = new animation() { @override protected void applytransformation(float interpolatedtime, transformation t) { v.getlayoutparams().height = interpolatedtime == 1 ? layoutparams.wrap_content : (int)(targetheight * interpolatedtime); v.requestlayout(); } @override public boolean willchangebounds() { return true; } }; // 1dp/ms a.setduration((int)(targetheight / v.getcontext().getresources().getdisplaymetrics().density)); v.startanimation(a); } public static void collapse(final view v) { final int initialheight = v.getmeasuredheight(); animation = new animation() { @override protected void applytransformation(float interpolatedtime, transformation t) { if(interpolatedtime == 1){ v.setvisibility(view.gone); }else{ v.getlayoutparams().height = initialheight - (int)(initialheight * interpolatedtime); v.requestlayout(); } } @override public boolean willchangebounds() { return true; } }; // 1dp/ms a.setduration((int)(initialheight / v.getcontext().getresources().getdisplaymetrics().density)); v.startanimation(a); }
Comments
Post a Comment