Skip to content

Instantly share code, notes, and snippets.

@yuriyskulskiy
Created August 19, 2020 20:04
AnimatedLayout part 5: update LinearLayout with parallaxValue
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler,
RecyclerView.State state) {
...
if (parentLayout != null) {
animatedLayout = parentLayout.findViewById(R.id.transformItem);
float translateValue = computeCurrentParallaxOffset(parentLayout);
animatedLayout.applyParallax(translateValue);
}
...
}
private float computeCurrentParallaxOffset(View animatedItem) {
float parallaxOffsetMax = animatedItem.getHeight() / 8f;
float centerRecyclerViewY = computeRecyclerViewCenterY();
float fulOffsetFromCenterPx = animatedItem.getHeight() / 2f + centerRecyclerViewY;
float centerItemY = computeItemCenterY(animatedItem);
float currentOffsetFromCenterPx = centerRecyclerViewY - centerItemY;
float normalizedCurrentScrollOffset = currentOffsetFromCenterPx / fulOffsetFromCenterPx;
float translateFromCenterY = normalizedCurrentScrollOffset * parallaxOffsetMax;
return translateFromCenterY;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment