主要的就是一个PageTransformer
class ScaleInTrasformer implements ViewPager.PageTransformer {
private float sideScale;
public ScaleInTrasformer(float sideScale) {
this.sideScale = sideScale;
}
@Override
public void transformPage(View page, float position) {
int pageWidth = page.getWidth();
int pageHeight = page.getHeight();
page.setPivotY(pageHeight / 2);
page.setPivotX(pageWidth / 2);
if (position < -1) {
page.setScaleX(sideScale);
page.setScaleY(sideScale);
page.setPivotX(pageWidth);
} else if (position <= 1) {
if (position < 0) {
float scaleFactor = (1 + position) * (1 - sideScale) + sideScale;
page.setScaleX(scaleFactor);
page.setScaleY(scaleFactor);
page.setPivotX(pageWidth * (0.5f + (0.5f * -position)));
} else {
float scaleFactor = (1 - position) * (1 - sideScale) + sideScale;
page.setScaleX(scaleFactor);
page.setScaleY(scaleFactor);
page.setPivotX(pageWidth * ((1 - position) * 0.5f));
}
} else {
page.setPivotX(0);
page.setScaleX(sideScale);
page.setScaleY(sideScale);
}
}
}
要使两边的Item漏出来还需要在ViewPager根试图添加属性
android:clipChildren="false"
最后设置几个重要的属
viewPager.setOffscreenPageLimit(3);
viewPager.setPageMargin(100);
viewPager.setPageTransformer(false, new ScaleInTransformer(0.7f));
最终效果如下