我最近下载了google sheets应用程序,我对第一个解释应用程序细节的屏幕非常感兴趣,所以我试图重新创建它。
经过一些研究,我决定它是ViewPager并且我实现了它。
但是result并不是我所期望的。
在sheets应用程序中,颜色变化是渐进的,几乎看不到,但在我的例子中,转换是清晰的(使用的颜色方案与sheets应用程序中的相同)。
Sheets应用程序中应用的动画类型是什么?我如何复制它?

最佳答案

最简单的方法是:
Fragments中移除背景(我假设您在ViewPagerFragments中使用ViewPager's。)如果没有,请告诉我)。我说的“删除背景”是指将其设置为Adapter
"@android:color/transparent"本身移除背景(也可将其颜色设置为ViewPager)。
在你的transparent下面(z-wise)放置一个View作为改变的背景。例如(不带参数):

<FrameLayout>
    <View android:id="@+id/animated_color_view"/>
    <android.support.v4.view.ViewPager/>
</FrameLayout>

创建一种设置ViewPager背景颜色动画的方法。您可以使用REG1 in the comment (edit: the comment has been deleted, but this link points to the same thread)链接的线程中包含的方法之一。例如(方法取自this post):
int[] pageColors = new int[]{Color.RED, Color.GREEN};
int currentColor = pageColors[0];
ValueAnimator colorAnimation;

public void animateToColor(int colorTo) {
    if (colorAnimation != null) {
        colorAnimation.cancel();
    }
    colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), currentColor, colorTo);
    colorAnimation.setDuration(250); // milliseconds
    colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

        @Override
        public void onAnimationUpdate(ValueAnimator animator) {
            currentColor = (int) animator.getAnimatedValue();
            animatedColorView.setBackgroundColor(currentColor);
        }

    });
    colorAnimation.start();
}

添加一个animated_color_view,它将在每次选择页面时调用此动画方法。
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        animateToColor(pageColors[position]);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
});

还有一点小提示:记住OnPageChangeListener数组的大小必须等于pageColors中的页数。

10-07 18:52
查看更多