gpt4 book ai didi

android - RecyclerView - Activity 开始时的 SlideIn 动画

转载 作者:行者123 更新时间:2023-11-29 17:26:43 24 4
gpt4 key购买 nike

如何在我的回收站 View 项目上一个接一个地添加动画幻灯片。就像 Activity 开始一样,回收器 View 的列表项一项一项地滑入。我正在使用 LinearLayoutManager

并非所有内容都应同时滑入。甚至在滚动时也不应如此。就在创建 Activity 时。

我搜索了但没有找到任何东西。

我想实现这样的目标:https://youtu.be/Q8TXgCzxEnw?t=30s

最佳答案

我在几个月前制作了一个示例应用程序,它在重新排列期间有一个连续的滑入滑出动画。提供演示视频 here .它应该会给您一些想法。

最相关类文件的链接是 here ,我将复制下面的代码。

public class AllNotesFragmentRecyclerView extends RecyclerView {

private static final int BASE_ANIMATION_TIME = 50;
private static final int MAX_ANIMATION_TIME_INCREMENT = 100;

private int screenWidth;
private int startX, finalX;

private int[] interpolatedAnimationTimes;

public AllNotesFragmentRecyclerView(Context context) {
super(context);
init(context);
}

public AllNotesFragmentRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}

public AllNotesFragmentRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}

private void init(Context context) {
calculateScreenWidth(context);

startX = 0;
finalX = -(screenWidth);
}

private void calculateScreenWidth(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
}

private int calculateInterpolatedAnimationTime(int currentIndex, int maxIndex) {
float percentage = ((float)currentIndex/(float)maxIndex);
float increment = (float) MAX_ANIMATION_TIME_INCREMENT * percentage;
return (int) (BASE_ANIMATION_TIME + increment);
}

public void updateListOrder() {
createAnimatorSet();
}

private void createAnimatorSet() {

AnimatorSet set = new AnimatorSet();
ArrayList<Animator> animArrayList = new ArrayList<>();

for (int i = 0; i < getChildCount(); i++) {
ObjectAnimator anim = ObjectAnimator
.ofFloat(getChildAt(i), "translationX", finalX);

int duration = calculateInterpolatedAnimationTime(i, getChildCount());

anim.setDuration(duration);
anim.addListener(new RowAnimationListener(i, duration, startX));
animArrayList.add(anim);
}
set.setInterpolator(new AccelerateInterpolator());
set.playSequentially(animArrayList);
set.start();
}

private void animateOn(int childPosition, int duration, int targetValue) {
ObjectAnimator animator = ObjectAnimator
.ofFloat(getChildAt(childPosition), "translationX", targetValue);
animator.setInterpolator(new DecelerateInterpolator());
animator.setDuration(duration);
animator.start();
}
//...

private class RowAnimationListener implements Animator.AnimatorListener {

private int position, duration, targetX;

public RowAnimationListener(int position, int duration, int targetX) {
this.position = position;
this.duration = duration;
this.targetX = targetX;
}

@Override
public void onAnimationStart(Animator animation) {
}

@Override
public void onAnimationEnd(Animator animation) {
int currentItem = getLinearLayoutManager().findFirstVisibleItemPosition() + position;
getAdapter().notifyItemChanged(currentItem);
notifyRowsPeripheralToVisibleItemsDataChanged(position);
animateOn(position, duration, targetX);
}

@Override
public void onAnimationCancel(Animator animation) { }

@Override
public void onAnimationRepeat(Animator animation) { }
}
}

关于android - RecyclerView - Activity 开始时的 SlideIn 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34361255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com