gpt4 book ai didi

android - 如何在 Recycler View Item on Scroll 上创建视差效果?

转载 作者:行者123 更新时间:2023-11-29 14:13:59 24 4
gpt4 key购买 nike

嘿,我有一个回收 View ,回收 View 的每个项目都包含 FrameLayout 中的 ImageView 和拉伸(stretch)图像以适应所有项目的大小
我想做什么来在 Recycler View Item 图像上创建视差效果

当我上下滚动时,我可以移动图像以显示它的隐藏部分

像这样Tutorial迅速

我在 Recycler View 滚动条上做了什么

  events_list.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
final DynamicImageView imageView = (DynamicImageView) recyclerView.findViewById(R.id.item_image);
if (imageView.getVisibility() == View.VISIBLE) {
if (dy > 0) {
Animation a = new Animation() {

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) imageView.getLayoutParams();
params.topMargin = (int)(-50 * interpolatedTime);
imageView.setLayoutParams(params);
}
};
a.setDuration(100); // in ms
imageView.startAnimation(a);

} else if (dy < 0) {
Animation a = new Animation() {

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) imageView.getLayoutParams();
params.topMargin = (int)(50 * interpolatedTime);
imageView.setLayoutParams(params);
}
};
a.setDuration(100); // in ms
imageView.startAnimation(a);
}
}


}
});

我知道这段代码不是最佳解决方案它还需要检查所有可见项目而不是第一个但是我的问题是我改变了 ImageView 的边距我在项目之间获得了空闲空间并且动画没有'完美运行
有帮助吗?

最佳答案

我找到了这个Library实现我真正想要的

如何使用

<com.fmsirvent.ParallaxEverywhere.PEWImageView
android:id="@+id/grid_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
pew:block_parallax_x="true"
pew:reverse="reverseY"

android:scaleType="centerCrop"
/>

关于android - 如何在 Recycler View Item on Scroll 上创建视差效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34242289/

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