【问题标题】:cardview shadow elevation animation in recyclerviewrecyclerview中的cardview阴影高程动画
【发布时间】:2017-12-11 07:26:30
【问题描述】:

我想要 cardview elevation 动画,就像 Google 在“PlayGames”动画中所做的那样。这是动画的示例gif。谁能指导我如何做或参考任何图书馆。

sample gif animation

谢谢

【问题讨论】:

标签: android animation android-cardview android-elevation


【解决方案1】:

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

1- 为卡片视图创建一个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

【讨论】:

  • 此解决方案效果很好,但我只能使用 viewpager 一次滚动一个位置。我无法处理这就是为什么我想用 recyclerview 来做这件事
  • @HiteshBisht 我已经更新了答案以使其与 RececlerView 一起使用,如果对您有帮助,请告诉我!
  • 我已经在 android 中使用 SnapHelper 类执行此操作,但现在我还希望缩放和提升在用户滚动时工作,就像我在示例 GIF 中显示的那样。谢谢
  • 您可以使用上面的cardview代码在bindview上进行缩放和提升,缺少什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-26
  • 2017-09-10
  • 2018-02-05
  • 2018-08-11
  • 1970-01-01
相关资源
最近更新 更多