向后兼容PageTransformerPageTransformer

2023-09-06 01:25:23 作者:留不住的快滚

我想动画在ViewPager项目和PageTransformer符合该法案。我希望它这样现在用的是支持V4库向后兼容到Android 2.2。但是...

I'm trying to animate items in a ViewPager and the PageTransformer fits the bill. I want it to be backwards compatible to Android 2.2 so am using the support v4 library. However...

由于属性动画仅支持为安卓3.0和前瞻性的,设置在一个PageTransformer> ViewPager在较早版本的平台将被忽略。

As property animation is only supported as of Android 3.0 and forward, setting a PageTransformer on a > ViewPager on earlier platform versions will be ignored.

所以PageTransformer将无法正常工作在旧版本的

so PageTransformer won't work on older versions

我使用杰克沃顿商学院的NineOldAndroids 库,所以我可以用它的API,但我不知道如何做动画的ViewPager。

I'm using Jake Wharton's NineOldAndroids library so I could use that API, but I'm not sure how to do animation for a ViewPager.

我怎么能这样做呢?

推荐答案

您需要使用AnimatorProxy包装设定的视点的变换属性来实现PageTransformer。然后艰难的部分是,ViewPager将忽略较低的API版本的PageTransformer。所以,你需要修改ViewPager本身使用PageTransformer。我已经在GitHub上的支持库,让这个问题,以及使用NineOldAndroids动画自定义片段转换的分支版本。 http://www.github.com/kedzie/support_v4_nineoldandroids使用动画过渡分支。这是一个Maven项目,所以您可以从V4子目录中生成它。

You need to implement the PageTransformer using the AnimatorProxy wrapper to set the transformation properties on the views. Then the tough part is that the ViewPager will ignore the PageTransformer in lower api versions. So you need to modify the ViewPager itself to use the PageTransformer. I have a forked version of the support library on GitHub which allows this as well as using NineOldAndroids animators for custom fragment transitions. http://www.github.com/kedzie/support_v4_nineoldandroids Use the animator-transition branch. It is a maven project so you can build it from the v4 subdirectory.

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);
        }
    }
}
 
精彩推荐
图片推荐