在无休止的ViewPager

时间:2016-05-30 15:26:29

标签: android android-viewpager cross-fade android-pagetransformer

我在其中一个应用程序中遇到了一个非常奇怪的行为,我真的希望你们能给我一些如何解决这个问题的提示。

设定:
我正在开发一种演示应用程序。多个图像和/或视频应该以无限的幻灯片形式重复显示,到目前为止工作得非常好。我通过实施"无尽的"来实现这一目标。 ViewPager。这基本上跟踪自定义项目位置并重复显示相同的fragemens 除了默认的幻灯片转换,应用程序还应提供交叉淡入淡出过渡(幻灯片不移动,仅淡入/淡出)。这本身就是有效的。它基本上通过添加基于当前位置和页面宽度的反转X平移来禁用页面移动。

无尽的Viewpager的适配器:

@Override
public Fragment getItem(int position) {

    // Get the next position
    // The ViewPager will increment the position continously
    // We will keep track of that, and will deliver the same slides in an
    // endless loop
    if (position >= slideList.size() - 1) {
        position = 0;
    }
    else {
        position++;
    }

    Slide newSlide = mySlidesList.get(position);

    Fragment frag = new Fragment();
    frag = ImageSlideFragment.newInstance(newSlide);
    return frag;

}

@Override
public int getCount() {
    return Integer.MAX_VALUE;
}

自定义淡入淡出动画:

private void performFadeTransition(View page, float position){

    //------------------------------------------------------------------------------------
    // position | what does it mean
    //------------------------------------------------------------------------------------
    // 0        | view is positioned in the center and fully visible to the user.
    // -1       | view is positioned in the left and not visible to the user.
    // 1        | view is positioned in the right and not visible to the user.
    // >-1 & <0 | view is being scrolled towards left and is partially visible.
    // >0 & <1  | view is being scrolled towards right and is partially visible.
    // ------------------------------------------------------------------------------------

    // Get page width
    int pageWidth = page.getWidth();

    // Determine if the page is moving in or out of the Viewpager
    boolean isMovingOut = position > -1 && position <= 0;
    boolean isMovingIn = position >= 0 && position < 1;

    // current fade out, next fades in.
    // Change the pages visibility, the nearer it is to the center, the more visible
    // it gets.
    if (isMovingOut || isMovingIn) {
        page.setAlpha(1.0F - Math.abs(position));
        page.setTranslationX(pageWidth * -position);
    }

    // If the translation has finished, we have to reset the non visible pages
    // on the far left and far right. Otherwise their invisibility would collide
    // when an other transition type is used afterwards
    else {
        page.setAlpha(1.0F);
        page.setTranslationX(0);
    }

}


问题:
现在的策略部分。当应用程序运行一段时间后,让我们说几百次重复,淡入淡出的动画开始出现故障。褪色的页面不再是不动的,而是开始动摇一下。似乎计算的x平移不够准确。另一方面,幻灯片切换仍然可以正常工作。


可能的原因:
我开始记录淡入淡出过渡期间究竟发生了什么。由于给定的位置值似乎是正确的,所以我开始记录页面x位置,这导致了这个:

E/TPT: ------------------
E/TPT: position: 1.0
E/TPT: page.getX() before: 334080.0
E/TPT: ------------------
E/TPT: position: 1.0
E/TPT: page.getX() before: 336000.0
E/TPT: ------------------
E/TPT: position: 1.0
E/TPT: page.getX() before: 337920.0
E/TPT: ------------------
E/TPT: position: 1.0
E/TPT: page.getX() before: 339840.0
E/TPT: ------------------
E/TPT: position: 1.0
E/TPT: page.getX() before: 341760.0

当认为页面为1920px时,似乎页面&#34; new&#34;页面x位置似乎每次增加1920px。这意味着经过一段时间后,页面x位置将达到数百万 由于x转换是相对于页面实际位置进行的,因此假设该值变得越来越不准确,x位置越高。

我的问题
我的假设是否正确?有谁知道如何解决这个问题?我真的没有想法......

0 个答案:

没有答案