在recyclerview中的cardview阴影高程动画

时间:2017-12-11 07:26:30

标签: android animation android-cardview android-elevation

我希望cardview elevation动画就像谷歌在“PlayGames”动画中所做的那样。这是动画的gif示例。任何人都可以指导我如何做或参考任何图书馆。

sample gif animation

由于

1 个答案:

答案 0 :(得分:1)

怎么做? 使用Viewpager和阴影变换

1-为cardview创建PagerAdapter

2-在适配器内instantiateItem方法设置卡的高程,如此

 CardView cardView = (CardView) view.findViewById(R.id.cardView);

        if (mBaseElevation == 0) {
            mBaseElevation = cardView.getCardElevation();
        }

        cardView.setMaxCardElevation(mBaseElevation * MAX_ELEVATION_FACTOR);
        mViews.set(position, cardView);
        return view;

在该工具ViewPager.OnPageChangeListener之后 在onPageScrolled

        if (currentCard != null) {
            if (mScalingEnabled) {
                currentCard.setScaleX((float) (1 + 0.1 * (1 - realOffset)));
                currentCard.setScaleY((float) (1 + 0.1 * (1 - realOffset)));
            }
            currentCard.setCardElevation((baseElevation + baseElevation
                    * (CardAdapter.MAX_ELEVATION_FACTOR - 1) * (1 - realOffset)));
        }

        CardView nextCard = mAdapter.getCardViewAt(nextPosition);

        // We might be scrolling fast enough so that the next (or previous) card
        // was already destroyed or a fragment might not have been created yet
        if (nextCard != null) {
            if (mScalingEnabled) {
                nextCard.setScaleX((float) (1 + 0.1 * (realOffset)));
                nextCard.setScaleY((float) (1 + 0.1 * (realOffset)));
            }
            nextCard.setCardElevation((baseElevation + baseElevation
                    * (CardAdapter.MAX_ELEVATION_FACTOR - 1) * (realOffset)));
        }

完整代码ViewPagerCards

如果您想使用RecyclerView执行此操作 您可以获取中间项目并检查onBindViewHolder该项目是否为中间项目然后执行缩放动画 要知道中间检查这个答案: Get center visible item of RecycleView when scrolling