Слайд развернуть анимацию в андроид - PullRequest
5 голосов
/ 24 апреля 2010

У меня есть простой ListView список результатов в Android. При нажатии на каждый элемент я бы хотел, чтобы он скользнул вниз, развернул и показал содержимое. Есть ли простой способ сделать это в Android?

Любая помощь будет оценена.

Ответы [ 3 ]

5 голосов
/ 21 сентября 2012

Вот пример из Удинич. Элемент списка был расширен анимацией и требует только уровня 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

3 голосов
/ 24 апреля 2010

проверить этот ответ . более того, вы должны использовать твидовую анимацию. проверьте ApiDemos / Animation2 Примеры. а также увидеть папку с анимом в ApiDemos. это мне очень помогает. по вашему вопросу поможет slide_top_to_bottom.

1 голос
/ 15 апреля 2015

Самый простой способ - использовать ObjectAnimator

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

. Это изменит maxLines с вашего TextView на 40, более 200 миллисекунд.

Остерегайтесь использования yourTextView.getLineCount (), чтобы определить, сколько строк нужно расширить, потому что оно не даст точного значения до окончания макета.Я рекомендую вам просто жестко кодировать значение maxLines, которое длиннее, чем вы ожидаете, что текст когда-либо будет.Вы также можете оценить его, используя yourTextView.length (), деленный на наименьшее количество символов, которое вы когда-либо ожидали на строку.

...