向后兼容的 PageTransformer

Backwards compatible PageTransformer(向后兼容的 PageTransformer)
本文介绍了向后兼容的 PageTransformer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 ViewPager 中的项目设置动画,而 PageTransformer 符合要求.我希望它向后兼容 Android 2.2,所以我使用 support v4 库.不过……

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...

由于仅从 Android 3.0 及更高版本开始支持属性动画,因此在早期平台版本上的 > ViewPager 上设置 PageTransformer 将被忽略.

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

我正在使用 Jake Wharton 的 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 来设置视图的转换属性.

You need to implement the PageTransformer using the AnimatorProxy wrapper to set the transformation properties on the views.

那么困难的部分是 ViewPager 将忽略较低 API 版本中的 PageTransformer.所以你需要修改ViewPager本身来使用PageTransformer.

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.

我有一个 GitHub 上支持库的分叉版本,它允许这样做以及使用 NineOldAndroids 动画师进行自定义片段过渡.使用 animator-transition 分支.这是一个 maven 项目,因此您可以从 v4 子目录构建它.

I have a forked version of the support library on GitHub which allows this as well as using NineOldAndroids animators for custom fragment transitions. 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);
        }
    }
}

这篇关于向后兼容的 PageTransformer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)