TextView expand animation like in Android Market

13,061

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;
    }
};
Share:
13,061

Related videos on Youtube

HighFlyer
Author by

HighFlyer

Updated on May 31, 2022

Comments

  • HighFlyer
    HighFlyer almost 2 years

    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.

  • pengwang
    pengwang almost 13 years
    can you give more detail,how to callback measureViewHeight()?what is View view2Expand, View view2Measure ?
  • HighFlyer
    HighFlyer almost 13 years
    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 stackoverflow.com/questions/5947758/force-fading-edge-drawin‌​g)
  • ingsaurabh
    ingsaurabh over 12 years
    @HighFlyer Can you post more how you go about achieving all this I dint get how you did it
  • Prativa
    Prativa over 11 years
    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??????????
  • HighFlyer
    HighFlyer over 11 years
    @Prativa its a typo. There are no need to pass Context into measureViewHeight
  • Prativa
    Prativa over 11 years
    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 .....
  • HighFlyer
    HighFlyer over 11 years
    @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.