在android中滑动扩展动画

时间:2010-04-24 15:56:59

标签: android animation android-listview expand

我在android中有一个简单的ListView列表结果。点击每个项目后,我希望它向下滑动展开并显示内容。在android中有一种简单的方法吗?

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:5)

以下是Udinic的例子。它有listview项目扩展动画,要求API级别只有4+ 基本上你需要一个动画类

/**
* This animation class is animating the expanding and reducing the size of a view.
* The animation toggles between the Expand and Reduce, depending on the current state of the view
* @author Udinic
*
*/
public class ExpandAnimation extends Animation {
    private View mAnimatedView;
    private LayoutParams mViewLayoutParams;
    private int mMarginStart, mMarginEnd;
    private boolean mIsVisibleAfter = false;
    private boolean mWasEndedAlready = false;

    /**
* Initialize the animation
* @param view The layout we want to animate
* @param duration The duration of the animation, in ms
*/
    public ExpandAnimation(View view, int duration) {

        setDuration(duration);
        mAnimatedView = view;
        mViewLayoutParams = (LayoutParams) view.getLayoutParams();

        // decide to show or hide the view
        mIsVisibleAfter = (view.getVisibility() == View.VISIBLE);

        mMarginStart = mViewLayoutParams.bottomMargin;
        mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0);

        view.setVisibility(View.VISIBLE);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);

        if (interpolatedTime < 1.0f) {

            // Calculating the new bottom margin, and setting it
            mViewLayoutParams.bottomMargin = mMarginStart
                    + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);

            // Invalidating the layout, making us seeing the changes we made
            mAnimatedView.requestLayout();

        // Making sure we didn't run the ending before (it happens!)
        } else if (!mWasEndedAlready) {
            mViewLayoutParams.bottomMargin = mMarginEnd;
            mAnimatedView.requestLayout();

            if (mIsVisibleAfter) {
                mAnimatedView.setVisibility(View.GONE);
            }
            mWasEndedAlready = true;
        }
    }
}

并使用此:

View toolbar = view.findViewById(R.id.toolbar);

                // Creating the expand animation for the item
                ExpandAnimation expandAni = new ExpandAnimation(toolbar, 500);

                // Start the animation on the toolbar
                toolbar.startAnimation(expandAni);

ExpandAnimationExample

答案 1 :(得分:3)

查看此answer。不仅仅是你必须使用花呢动画。查看ApiDemos/Animation2示例。并且还可以在ApiDemos中看到anim文件夹。它对我有很大帮助。根据你的问题,slide_top_to_bottom会有所帮助。

答案 2 :(得分:1)

最简单的方法是使用ObjectAnimator

ObjectAnimator animation = ObjectAnimator.ofInt(yourTextView, "maxLines", 40);
animation.setDuration(200).start();

这会将maxLines从TextView更改为40,超过200毫秒。

注意使用yourTextView.getLineCount()来确定要扩展到多少行,因为在布局通过之前它不会给出准确的数字。我建议您只需硬编码一个比您预期的文本更长的maxLines值。您还可以使用yourTextView.length()除以您每行所期望的最低字符数来估算它。