为什么此动画不会在第一次加载时开始?

时间:2018-11-09 15:03:12

标签: android animation handler runnable

我有一个屏幕,其中同时显示了许多动画。在这里,入口动画不会在第一次加载时开始,而是在再次调用该方法后开始。

下面是我设置要显示哪个孩子并将其动画化的方法。

public void setPosition(final int position, final boolean animate) {

    for (int x = 0; x < tabsContainer.getChildCount(); x++) {
        scheduledBookingContainer.getChildAt(x).setVisibility(x == position ? View.VISIBLE : View.GONE);
        timeContainer.getChildAt(x).setVisibility(x == position ? View.VISIBLE : View.GONE);
        tabsContainer.getChildAt(x).setVisibility(x == position ? View.VISIBLE : View.GONE);
        if (x == position) {
            final LinearLayout cardContainer = (LinearLayout) tabsContainer.getChildAt(x);
            scheduledBookingContainer.getChildAt(x).startAnimation(this.position > position ?
                    AnimationUtil.inFromLeftAnimation() : AnimationUtil.inFromRightAnimation());
            timeContainer.getChildAt(x).startAnimation(this.position > position ?
                    AnimationUtil.inFromLeftAnimation() : AnimationUtil.inFromRightAnimation());
            if (animate) {
                animationRunnable = new Runnable() {
                    @Override
                    public void run() {
                        animateCardsIn(cardContainer);
                    }
                };
                new Handler().postDelayed(animationRunnable, 500);
            } else {
                showCards(cardContainer);
            }
        } else if (x == this.position) {
            LinearLayout currentCardContainer = (LinearLayout) tabsContainer.getChildAt(this.position);
            currentCardContainer.setVisibility(View.VISIBLE);
            Animation animation = loadAnimation(getContext(), R.anim.shrink_out);
            animateCardsOut(currentCardContainer, animation, (LinearLayout) tabsContainer.getChildAt(position));
            currentCardContainer.startAnimation(this.position > position ?
                    AnimationUtil.outToRightAnimation() : AnimationUtil.outToLeftAnimation());
            scheduledBookingContainer.getChildAt(x).startAnimation(this.position > position ?
                    AnimationUtil.outToRightAnimation() : AnimationUtil.outToLeftAnimation());
            timeContainer.getChildAt(x).startAnimation(this.position > position ?
                    AnimationUtil.outToRightAnimation() : AnimationUtil.outToLeftAnimation());
        }
    }
    this.position = position;
}

这是在第一次加载时不会启动的方法。

private void animateCardsIn(final LinearLayout childContainer) {
    int delayMillis = 0;
    for (int y = 0; y < childContainer.getChildCount(); y++) {
        final int finalY = y;
        ViewGroup child = (ViewGroup) childContainer.getChildAt(y);
        ViewGroup subChild = (ViewGroup) child.getChildAt(0);
        if (subChild != null) {
            if (subChild.getChildCount() > 0) {
                animationRunnable = new Runnable() {
                    @Override
                    public void run() {
                        Animation animation = loadAnimation(getContext(), R.anim.slide_in_top_right);
                        childContainer.setVisibility(View.VISIBLE);
                        childContainer.getChildAt(finalY).setVisibility(View.VISIBLE);
                        childContainer.getChildAt(finalY).startAnimation(animation);
                    }
                };
                new Handler().postDelayed(animationRunnable, delayMillis);
                delayMillis = delayMillis + 100;
            }
        }
    }
}

0 个答案:

没有答案