Skip to content

Instantly share code, notes, and snippets.

@jayrambhia
Last active September 7, 2016 12:53
Show Gist options
  • Save jayrambhia/16f92f6d5e644ea8c365c381a816a94b to your computer and use it in GitHub Desktop.
Save jayrambhia/16f92f6d5e644ea8c365c381a816a94b to your computer and use it in GitHub Desktop.
Animations
public class CustomAnimationUtils {
public static void animateY(@NonNull View view, int startY, int endY, int duration) {
animateY(view, startY, endY, duration, new AccelerateDecelerateInterpolator());
}
public static void animateY(@NonNull View view, int startY, int endY, int duration,
@NonNull Interpolator interpolator) {
view.clearAnimation();
view.setTranslationY(startY);
ViewCompat.animate(view).translationY(endY)
.setInterpolator(interpolator)
.setDuration(duration)
.withLayer()
.setListener(null)
.start();
}
}
private void animateIn() {
// Get view from PagerAdapter or ViewPager -> viewPager.getChildAt(0)
View page0 = adapter.getViewAt(0);
View page1 = adapter.getViewAt(1);
// Make second page appear slower than the first one
// so that user notices there are more pages
CustomAnimationUtils.animateY(page0, 2000, 0, 500);
CustomAnimationUtils.animateY(page1, 3000, 0, 700);
}
private void animateOut() {
int position = viewPager.getCurrentItem();
View currentPage = adapter.getViewAt(position);
CustomAnimationUtils.animateY(currentPage, 0, 2000, 400, new AnticipateInterpolator(1));
if (position != 0) {
View previousPage = adapter.getViewAt(position - 1);
CustomAnimationUtils.animateY(previousPage, 0, 2000, 500);
}
if (position < adapter.getCount() - 1) {
View nextPage = adapter.getViewAt(position + 1);
CustomAnimationUtils.animateY(nextPage, 0, 2000, 500);
}
}
private void animateIn() {
// start translationY - 2000
// final translationY - 0
// Duration - 1 sec
CustomAnimationUtils.animateY(viewPager, 2000, 0, 1000);
}
private void animateOut() {
// start translationY - 0
// final translationY - 2000
// Duration - 1 sec
CustomAnimationUtils.animateY(viewPager, 0, 2000, 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment