3

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

设置:
我正在开发一种演示应用程序。多个图像和/或视频应该在无尽的幻灯片中重复显示,到目前为止效果很好。我通过实现“无尽”的 ViewPager 实现了这一点。这基本上跟踪自定义项目位置并重复显示相同的片段。
除了默认的幻灯片过渡,应用程序应该提供交叉淡入淡出过渡(幻灯片不移动,只有淡入/淡出)。就其本身而言,这也在起作用。它基本上通过基于当前位置和页面宽度添加反向 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

当考虑到页面宽度为 1920 像素时,似乎页面“新”页面 x 位置似乎每次都增加 1920 像素。这意味着一段时间后,页面 x 位置将达到数百万。
由于 x 平移是相对于页面实际位置进行的,因此我假设 x 位置越高,该值变得越来越不准确。

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

4

0 回答 0