您需要PageTransformer
使用AnimatorProxy
包装器来实现在视图上设置转换属性。
然后困难的部分是ViewPager
将忽略PageTransformer
较低的 API 版本。所以你需要修改ViewPager
自身才能使用PageTransformer
.
我在 GitHub 上有一个支持库的分叉版本,它允许这样做以及使用 NineOldAndroids 动画师进行自定义片段转换。使用 animator-transition 分支。这是一个 maven 项目,因此您可以从 v4 子目录构建它。
public class ZoomOutPageTransformer implements PageTransformer {
private static float MIN_SCALE = 0.85f;
private static float MIN_ALPHA = 0.5f;
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
int pageHeight = view.getHeight();
AnimatorProxy proxy = AnimatorProxy.wrap(view);
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
proxy.setAlpha(0);
} else if (position <= 1) { // [-1,1]
// Modify the default slide transition to shrink the page as well
float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
float vertMargin = pageHeight * (1 - scaleFactor) / 2;
float horzMargin = pageWidth * (1 - scaleFactor) / 2;
if (position < 0) {
proxy.setTranslationX(horzMargin - vertMargin / 2);
} else {
proxy.setTranslationX(-horzMargin + vertMargin / 2);
}
// Scale the page down (between MIN_SCALE and 1)
proxy.setScaleX(scaleFactor);
proxy.setScaleY(scaleFactor);
// Fade the page relative to its size.
proxy.setAlpha(MIN_ALPHA +
(scaleFactor - MIN_SCALE) /
(1 - MIN_SCALE) * (1 - MIN_ALPHA));
} else { // (1,+Infinity]
// This page is way off-screen to the right.
proxy.setAlpha(0);
}
}
}