I have recently replaced my ViewPager with a ViewPager2, however, the animation when swiping pages seems like it's robotic and non-linear, unlike the old one with a much smoother animation. Is there a way I can get the old one back?
I have recently replaced my ViewPager with a ViewPager2, however, the animation when swiping pages seems like it's robotic and non-linear, unlike the old one with a much smoother animation. Is there a way I can get the old one back?
Well, the Developer guide would definitely show you how to implement the viewpager in your code, but in order to get more smoother effect on you animation try implementing below code.
public class ParallaxPageTransformer implements ViewPager.PageTransformer {
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(1);
} else if (position <= 1) { // [-1,1]
dummyImageView.setTranslationX(-position * (pageWidth / 2)); //Half the normal speed
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(1);
}
}
}
This would provide you an animation like this :
You can also go through following links which will help you more on this :
http://andraskindler.com/blog/2013/create-viewpager-transitions-a-pagertransformer-example/
http://developer.android.com/training/animation/screen-slide.html#pagetransformer
The canonical way to customize animation in ViewPager2 appears to be through PageTransformer. There aren't included drop-in replacements, but you can create your own transitions:
class MyPageTransformer : ViewPager2.PageTransformer {
override fun transformPage(view: View, position: Float) {
// position is the offset from center, negative being left
// make changes to view based on pages position
// e.g. view.alpha = min(0f, 1f - abs(position))
}
}
...
viewPager.setPageTransformer(MyPageTransformer())
See examples in the Developer Guide for ViewPager2 transitions.