mobile/android/base/widget/AnimatedHeightLayout.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/widget/AnimatedHeightLayout.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.widget;
     1.9 +
    1.10 +import org.mozilla.gecko.animation.HeightChangeAnimation;
    1.11 +
    1.12 +import android.content.Context;
    1.13 +import android.util.AttributeSet;
    1.14 +import android.view.animation.Animation;
    1.15 +import android.view.animation.DecelerateInterpolator;
    1.16 +import android.widget.RelativeLayout;
    1.17 +
    1.18 +public class AnimatedHeightLayout extends RelativeLayout {
    1.19 +    private static final String LOGTAG = "GeckoAnimatedHeightLayout";
    1.20 +    private static final int ANIMATION_DURATION = 100;
    1.21 +    private boolean mAnimating = false;
    1.22 +
    1.23 +    public AnimatedHeightLayout(Context context) {
    1.24 +        super(context, null);
    1.25 +    }
    1.26 +
    1.27 +    public AnimatedHeightLayout(Context context, AttributeSet attrs) {
    1.28 +        super(context, attrs, 0);
    1.29 +    }
    1.30 +
    1.31 +    public AnimatedHeightLayout(Context context, AttributeSet attrs, int defStyle) {
    1.32 +        super(context, attrs, defStyle);
    1.33 +    }
    1.34 +
    1.35 +    @Override
    1.36 +    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    1.37 +        int oldHeight = getMeasuredHeight();
    1.38 +        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    1.39 +        int newHeight = getMeasuredHeight();
    1.40 +
    1.41 +        if (!mAnimating && oldHeight != 0 && oldHeight != newHeight) {
    1.42 +            mAnimating = true;
    1.43 +            setMeasuredDimension(getMeasuredWidth(), oldHeight);
    1.44 +
    1.45 +            // Animate the difference of suggestion row height
    1.46 +            Animation anim = new HeightChangeAnimation(this, oldHeight, newHeight);
    1.47 +            anim.setDuration(ANIMATION_DURATION);
    1.48 +            anim.setInterpolator(new DecelerateInterpolator());
    1.49 +            anim.setAnimationListener(new Animation.AnimationListener() {
    1.50 +                @Override
    1.51 +                public void onAnimationStart(Animation animation) {}
    1.52 +                @Override
    1.53 +                public void onAnimationRepeat(Animation animation) {}
    1.54 +                @Override
    1.55 +                public void onAnimationEnd(Animation animation) {
    1.56 +                    post(new Runnable() {
    1.57 +                        @Override
    1.58 +                        public void run() {
    1.59 +                            finishAnimation();
    1.60 +                        }
    1.61 +                    });
    1.62 +                }
    1.63 +            });
    1.64 +            startAnimation(anim);
    1.65 +        }
    1.66 +    }
    1.67 +
    1.68 +    @Override
    1.69 +    protected void onDetachedFromWindow() {
    1.70 +        super.onDetachedFromWindow();
    1.71 +        finishAnimation();
    1.72 +    }
    1.73 +
    1.74 +    private void finishAnimation() {
    1.75 +        if (mAnimating) {
    1.76 +            getLayoutParams().height = LayoutParams.WRAP_CONTENT;
    1.77 +            mAnimating = false;
    1.78 +        }
    1.79 +    }
    1.80 +}

mercurial