android - Android Market 中的 TextView 展开动画

标签 android animation textview expand

谁能指出如何在 Android Market 动画中制作 Description TextView 的展开动画的解决方案? TextView 包裹在 FrameLayout 中,单击“更多”标签后会展开。

最佳答案

解决方案:

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

关于android - Android Market 中的 TextView 展开动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5927977/

相关文章:

java - 为什么我在 Android 中需要 super() ?

ios - 如何在 Swift 3 中加载/删除带有动画的自定义 View

javascript - jQuery 淡入淡出文本

java - Android TextView水平滚动不起作用

android - Tablayout 文本颜色没有变化,android?

android - 在 android WebView 中读取安全 cookie

android - 使用协调器布局以编程方式隐藏工具栏

android - 有什么方法可以检测到 react native(expo) 中的 android 软导航栏?

javascript - JavaScript 不会调整 Div 的大小

java - 在 Activity 之间更改 TextView