Skip to content

Instantly share code, notes, and snippets.

@sujithkanna
Last active February 17, 2020 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sujithkanna/25f397b5ed9f4723ce159b4d7f19a103 to your computer and use it in GitHub Desktop.
Save sujithkanna/25f397b5ed9f4723ce159b4d7f19a103 to your computer and use it in GitHub Desktop.
// To evaluate value between x and y base on fraction(0 - 1)
private IntEvaluator mIntEvaluator = new IntEvaluator();
// Interpolator just for curvy animations.
private Interpolator mInterpolator = new AccelerateDecelerateInterpolator();
public void invalidateSlider(MenuViewHolder holder) {
SliderImageView imageCard = holder.imageCard;
// top defines the pixel position of the item in the screen
float top = holder.itemView.getTop() + holder.itemView.getMeasuredHeight();
float height = App.getInstance().getResources().getDisplayMetrics().heightPixels
+ holder.itemView.getMeasuredHeight();
float containerFraction = 0; // Defines the value from 0 - 1 according to the
// item position on screen
if (top > height / 2) { // Apply's value only if the view is above half of screen
containerFraction = mInterpolator.getInterpolation((top - height / 2)
/ (height / 2));
}
// These two variables are the start and end distance of the views in item.
// Direction of movement is based on the views(Image moves right and Card
// moves left while scrolling down).
int end = App.getInstance()
.getResources().getDimensionPixelSize(R.dimen.dp_25);
int start = (holder.itemView.getMeasuredWidth() / 2)
- (imageCard.getMeasuredWidth() / 2);
// This will move the content textView from center to left based on containerFraction
holder.contentView.setX(mIntEvaluator.evaluate(1f - containerFraction, start, end));
// The imageCard have to move opposite to the content but not out of the item,
// so having some calculations to move opposite to content
imageCard.setX(mIntEvaluator.evaluate(1f - containerFraction, start,
holder.itemView.getMeasuredWidth() - imageCard.getMeasuredWidth() - end));
// Pass the animation factor variable here to make the image slide, But the
// interesting part is that the image will slide backwards since we provided
// the fraction inversely(1 - fraction)
imageCard.setAnimationFactor(1f - containerFraction);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment