Android 如何在卷轴上的回收器视图项目上创建视差效果?

Android 如何在卷轴上的回收器视图项目上创建视差效果?,android,animation,android-recyclerview,Android,Animation,Android Recyclerview,嘿,我有一个recycler视图,recycler视图的每个项目都包含ImageView,它位于FrameLayout中,图像拉伸以适合所有项目的大小 我想做什么来创建回收器视图项目图像上的视差效果 当我上下滚动时,我可以移动图像以显示隐藏的部分 像斯威夫特这样 我在“回收者视图”卷轴上做了什么 events_list.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override pub

嘿,我有一个recycler视图,recycler视图的每个项目都包含ImageView,它位于FrameLayout中,图像拉伸以适合所有项目的大小
我想做什么来创建回收器视图项目图像上的视差效果

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

像斯威夫特这样

我在“回收者视图”卷轴上做了什么

  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);
                }
            }


        }
    });
events\u list.addOnScrollListener(新的RecyclerView.OnScrollListener(){
@凌驾
CrollStateChanged上的公共无效(RecyclerView RecyclerView,int newState){
super.onScrollStateChanged(recyclerView、newState);
}
@凌驾
已填空的公共空间(RecyclerView RecyclerView、int dx、int dy){
super.onScrolled(recyclerView、dx、dy);
最终的dynamicmageview图像视图=(dynamicmageview)recyclerView.findviewbyd(R.id.item_图像);
if(imageView.getVisibility()==View.VISIBLE){
如果(dy>0){
动画a=新动画(){
@凌驾
受保护的无效应用转换(浮点插值时间,转换t){
FrameLayout.LayoutParams params=(FrameLayout.LayoutParams)imageView.getLayoutParams();
参数topMargin=(int)(-50*插值时间);
imageView.setLayoutParams(参数);
}
};
a、 setDuration(100);//以毫秒为单位
imageView.startAnimation(a);
}else if(dy<0){
动画a=新动画(){
@凌驾
受保护的无效应用转换(浮点插值时间,转换t){
FrameLayout.LayoutParams params=(FrameLayout.LayoutParams)imageView.getLayoutParams();
参数topMargin=(int)(50*插值时间);
imageView.setLayoutParams(参数);
}
};
a、 setDuration(100);//以毫秒为单位
imageView.startAnimation(a);
}
}
}
});
我知道这段代码不是最佳的解决方案,它还需要检查所有可见的项目不是第一个,但我的问题是,我更改了图像视图的边距,我在项目之间获得了一个自由空间,动画无法完美地工作
有什么帮助吗?

我在这里找到了我想要的

它是如何使用的

<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"
        />

我发现这正是我想要的

它是如何使用的

<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"
        />


您可能只想创建自己的布局/视图,而不想使用滚动侦听器。覆盖
onLayout
方法即可设置。然后,根据视图位置,您可以自己绘制图像视图,对其进行视差处理hanks@bleeding182您是对的我发现的库以这种方式工作再次感谢您可能只想创建自己的布局/视图,而不想使用滚动侦听器。覆盖
onLayout
方法即可设置。然后,你可以根据视图的位置,自己绘制图像视图,对其进行视差处理hanks@bleeding182你是对的,我发现这个库是用这种方式工作的,再次感谢