11

Can anyone point to solution how to make expand animation of Description TextView in Android Market animation? There are TextView wrapped into FrameLayout which expands after clicking on 'More' label.

HighFlyer
  • 1,615
  • 2
  • 15
  • 22

1 Answers1

13

Solution:

private static int measureViewHeight( View view2Expand, View view2Measure ) {
    try {
        Method m = view2Measure.getClass().getDeclaredMethod("onMeasure", int.class, int.class);
        m.setAccessible(true);
        m.invoke(view2Measure,
                    MeasureSpec.makeMeasureSpec(view2Expand.getWidth(), MeasureSpec.AT_MOST),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    } catch (Exception e) {
        return -1;
    }

    int measuredHeight = view2Measure.getMeasuredHeight();
    return measuredHeight;
}

static public void expandOrCollapse( View view2Expand, View view2Measure,
        int collapsedHeight ) {
    if (view2Expand.getHeight() < collapsedHeight)
        return;

    int measuredHeight = measureViewHeight(view2Expand, view2Measure);

    if (measuredHeight < collapsedHeight)
        measuredHeight = collapsedHeight;

    final int startHeight = view2Expand.getHeight();
    final int finishHeight = startHeight <= collapsedHeight ?
            measuredHeight : collapsedHeight;

    view2Expand.startAnimation(new ExpandAnimation(view2Expand, startHeight, finishHeight));
}

class ExpandAnimation extends Animation {
    private final View _view;
    private final int _startHeight;
    private final int _finishHeight;

    public ExpandAnimation( View view, int startHeight, int finishHeight ) {
        _view = view;
        _startHeight = startHeight;
        _finishHeight = finishHeight;
        setDuration(220);
    }

    @Override
    protected void applyTransformation( float interpolatedTime, Transformation t ) {
        final int newHeight = (int)((_finishHeight - _startHeight) * interpolatedTime + _startHeight);
        _view.getLayoutParams().height = newHeight;
        _view.requestLayout();
    }

    @Override
    public void initialize( int width, int height, int parentWidth, int parentHeight ) {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    @Override
    public boolean willChangeBounds( ) {
        return true;
    }
};
angryITguy
  • 9,332
  • 8
  • 54
  • 82
HighFlyer
  • 1,615
  • 2
  • 15
  • 22
  • 3
    can you give more detail,how to callback measureViewHeight()?what is View view2Expand, View view2Measure ? – pengwang May 23 '11 at 02:19
  • 2
    Of course. In my case, view2Measure - is TextView. view2Expand - FrameLayout which contains TextView. We measure full height of text view and apply it to FrameLayout. FrameLayout is presented for fade effect when text is collapsed (see http://stackoverflow.com/questions/5947758/force-fading-edge-drawing) – HighFlyer May 29 '11 at 16:52
  • @HighFlyer Can you post more how you go about achieving all this I dint get how you did it – ingsaurabh Sep 30 '11 at 08:07
  • first you create a method with two parameters private static int measureViewHeight( View view2Expand, View view2Measure ) and then you call the same method by passing three parameters....measureViewHeight(view2Expand, view2Measure, context); can you explain why you have done that?????????? – Prativa Jan 07 '13 at 08:31
  • @Prativa its a typo. There are no need to pass Context into measureViewHeight – HighFlyer Jan 08 '13 at 11:57
  • anyway......the code shows anomalous behaviour......it only collapse but it does not expand on the click of the view......can you provide detail expalanation or some samples that you have already done ..... – Prativa Jan 08 '13 at 11:59
  • @Prativa Sorry, I do not have enough time at the moment for full example, you may provide your sources and I'll try to find mistake. – HighFlyer Jan 10 '13 at 11:15