|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.widget; |
|
6 |
|
7 import org.mozilla.gecko.animation.HeightChangeAnimation; |
|
8 |
|
9 import android.content.Context; |
|
10 import android.util.AttributeSet; |
|
11 import android.view.animation.Animation; |
|
12 import android.view.animation.DecelerateInterpolator; |
|
13 import android.widget.RelativeLayout; |
|
14 |
|
15 public class AnimatedHeightLayout extends RelativeLayout { |
|
16 private static final String LOGTAG = "GeckoAnimatedHeightLayout"; |
|
17 private static final int ANIMATION_DURATION = 100; |
|
18 private boolean mAnimating = false; |
|
19 |
|
20 public AnimatedHeightLayout(Context context) { |
|
21 super(context, null); |
|
22 } |
|
23 |
|
24 public AnimatedHeightLayout(Context context, AttributeSet attrs) { |
|
25 super(context, attrs, 0); |
|
26 } |
|
27 |
|
28 public AnimatedHeightLayout(Context context, AttributeSet attrs, int defStyle) { |
|
29 super(context, attrs, defStyle); |
|
30 } |
|
31 |
|
32 @Override |
|
33 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
|
34 int oldHeight = getMeasuredHeight(); |
|
35 super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
|
36 int newHeight = getMeasuredHeight(); |
|
37 |
|
38 if (!mAnimating && oldHeight != 0 && oldHeight != newHeight) { |
|
39 mAnimating = true; |
|
40 setMeasuredDimension(getMeasuredWidth(), oldHeight); |
|
41 |
|
42 // Animate the difference of suggestion row height |
|
43 Animation anim = new HeightChangeAnimation(this, oldHeight, newHeight); |
|
44 anim.setDuration(ANIMATION_DURATION); |
|
45 anim.setInterpolator(new DecelerateInterpolator()); |
|
46 anim.setAnimationListener(new Animation.AnimationListener() { |
|
47 @Override |
|
48 public void onAnimationStart(Animation animation) {} |
|
49 @Override |
|
50 public void onAnimationRepeat(Animation animation) {} |
|
51 @Override |
|
52 public void onAnimationEnd(Animation animation) { |
|
53 post(new Runnable() { |
|
54 @Override |
|
55 public void run() { |
|
56 finishAnimation(); |
|
57 } |
|
58 }); |
|
59 } |
|
60 }); |
|
61 startAnimation(anim); |
|
62 } |
|
63 } |
|
64 |
|
65 @Override |
|
66 protected void onDetachedFromWindow() { |
|
67 super.onDetachedFromWindow(); |
|
68 finishAnimation(); |
|
69 } |
|
70 |
|
71 private void finishAnimation() { |
|
72 if (mAnimating) { |
|
73 getLayoutParams().height = LayoutParams.WRAP_CONTENT; |
|
74 mAnimating = false; |
|
75 } |
|
76 } |
|
77 } |