Android市场中的TextView扩展动画

11

有人能指出如何制作 Android 市场中描述文本视图的扩展动画解决方案吗?其中 TextView 包含在 FrameLayout 中,点击“更多”标签后会展开。

1个回答

13

解决方案:

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;
    }
};

3
你能提供更多细节吗?如何回调measureViewHeight()函数?view2Expand和view2Measure是什么视图? - pengwang
2
当然。在我的情况下,view2Measure是TextView。view2Expand是包含TextView的FrameLayout。我们测量文本视图的完整高度,并将其应用于FrameLayout。当文本折叠时,FrameLayout呈现为淡出效果(请参见http://stackoverflow.com/questions/5947758/force-fading-edge-drawing)。 - HighFlyer
首先,您需要创建一个带有两个参数的方法:private static int measureViewHeight(View view2Expand, View view2Measure)。然后,通过传递三个参数来调用相同的方法...measureViewHeight(view2Expand, view2Measure, context)。您能解释一下为什么要这样做吗? - Prativa
@Prativa 这是一个打字错误。不需要将 Context 传递给 measureViewHeight 函数。 - HighFlyer
@Prativa 抱歉,我现在没有足够的时间提供完整的示例,您可以提供您的资源,我会尽力找到错误。 - HighFlyer
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接