我试图创建一个动画,就像snapchat的viewpager一样,在这里,左、右片段的背景淡入,但内容在滑动。所有这些都是在相机碎片(中心碎片)保持静止时发生的。
就像左边和右边的碎片重叠在中间的碎片上。
我看过很多教程,我能找到的最接近的是这个链接:OnPageChangeListener alpha crossfading
问题是,只有当正确的片段出现在视图中时,它才起作用。而左侧碎片的背景与中心碎片不重叠。
代码:

public class CustomPageTransformer implements ViewPager.PageTransformer {
public void transformPage(View view, float position) {
    int pageWidth = view.getWidth();
    View imageView = view.findViewById(R.id.image_view);
    View contentView = view.findViewById(R.id.content_area);

    if (position < -1) { // [-Infinity,-1)
        // This page is way off-screen to the left
    } else if (position <= 0) { // [-1,0]
        // This page is moving out to the left

        // Counteract the default swipe
        view.setTranslationX(pageWidth * -position);
        if (contentView != null) {
            // But swipe the contentView
            contentView.setTranslationX(pageWidth * position);
        }
        if (imageView != null) {
            // Fade the image in
            imageView.setAlpha(1 + position);
        }

    } else if (position <= 1) { // (0,1]
        // This page is moving in from the right

        // Counteract the default swipe
        view.setTranslationX(pageWidth * -position);
        if (contentView != null) {
            // But swipe the contentView
            contentView.setTranslationX(pageWidth * position);
        }
        if (imageView != null) {
            // Fade the image out
            imageView.setAlpha(1 - position);
        }
    } else { // (1,+Infinity]
        // This page is way off-screen to the right
    }
}

例子:
Official Snapchat App
上面的示例表明,相机是静态的(不移动),而发现页的背景与相机片段重叠(中间片段),但发现页的内容正在滑入(也与相机片段重叠)
如果你们中有人知道怎么做,我会非常感激的。

最佳答案

您可以使用此库实现与snapchat相同的ui。此库还提供自定义功能,如显示多于3个选项卡,还具有许多功能,可帮助您构建类似快照的viewpager。
SnapTabLayout
android - 类似于Snapchat的Viewpager动画-LMLPHP
android - 类似于Snapchat的Viewpager动画-LMLPHP

关于android - 类似于Snapchat的Viewpager动画,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51122011/

10-12 18:52