【发布时间】:2017-12-11 07:26:30
【问题描述】:
我想要 cardview elevation 动画,就像 Google 在“PlayGames”动画中所做的那样。这是动画的示例gif。谁能指导我如何做或参考任何图书馆。
谢谢
【问题讨论】:
标签: android animation android-cardview android-elevation
我想要 cardview elevation 动画,就像 Google 在“PlayGames”动画中所做的那样。这是动画的示例gif。谁能指导我如何做或参考任何图书馆。
谢谢
【问题讨论】:
标签: android animation android-cardview android-elevation
怎么做? 使用 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
【讨论】: