michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.widget; michael@0: michael@0: import org.mozilla.gecko.animation.HeightChangeAnimation; michael@0: michael@0: import android.content.Context; michael@0: import android.util.AttributeSet; michael@0: import android.view.animation.Animation; michael@0: import android.view.animation.DecelerateInterpolator; michael@0: import android.widget.RelativeLayout; michael@0: michael@0: public class AnimatedHeightLayout extends RelativeLayout { michael@0: private static final String LOGTAG = "GeckoAnimatedHeightLayout"; michael@0: private static final int ANIMATION_DURATION = 100; michael@0: private boolean mAnimating = false; michael@0: michael@0: public AnimatedHeightLayout(Context context) { michael@0: super(context, null); michael@0: } michael@0: michael@0: public AnimatedHeightLayout(Context context, AttributeSet attrs) { michael@0: super(context, attrs, 0); michael@0: } michael@0: michael@0: public AnimatedHeightLayout(Context context, AttributeSet attrs, int defStyle) { michael@0: super(context, attrs, defStyle); michael@0: } michael@0: michael@0: @Override michael@0: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { michael@0: int oldHeight = getMeasuredHeight(); michael@0: super.onMeasure(widthMeasureSpec, heightMeasureSpec); michael@0: int newHeight = getMeasuredHeight(); michael@0: michael@0: if (!mAnimating && oldHeight != 0 && oldHeight != newHeight) { michael@0: mAnimating = true; michael@0: setMeasuredDimension(getMeasuredWidth(), oldHeight); michael@0: michael@0: // Animate the difference of suggestion row height michael@0: Animation anim = new HeightChangeAnimation(this, oldHeight, newHeight); michael@0: anim.setDuration(ANIMATION_DURATION); michael@0: anim.setInterpolator(new DecelerateInterpolator()); michael@0: anim.setAnimationListener(new Animation.AnimationListener() { michael@0: @Override michael@0: public void onAnimationStart(Animation animation) {} michael@0: @Override michael@0: public void onAnimationRepeat(Animation animation) {} michael@0: @Override michael@0: public void onAnimationEnd(Animation animation) { michael@0: post(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: finishAnimation(); michael@0: } michael@0: }); michael@0: } michael@0: }); michael@0: startAnimation(anim); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: protected void onDetachedFromWindow() { michael@0: super.onDetachedFromWindow(); michael@0: finishAnimation(); michael@0: } michael@0: michael@0: private void finishAnimation() { michael@0: if (mAnimating) { michael@0: getLayoutParams().height = LayoutParams.WRAP_CONTENT; michael@0: mAnimating = false; michael@0: } michael@0: } michael@0: }