I have encountered a really strange behaviour in one of my apps, and im really hoping that you guys can give me some hints on how to solve this.
Setup:
Im developing kind of a presentation application. Multiple images and/or videos should be displayed repeatedly in an endless slideshow which is working pretty good so far. I achieved this by implementing an "endless" ViewPager. This basically keeps track of a custom item position and repeatedly displays the same fragemens.
In addition to the default slide transition, the app should provide a cross-fade transition (slides not moving, only fade-in/fade-out). This, taken for itself, is working, too. It basically disables the pages movenent by adding an inverted X translation based on the current position and the pages width.
Adapter of the endless 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;
}
Custom fade animation:
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);
}
}
Problem:
Now the strage part. When the app has been running for some time, lets say a couple hundred repetitions, the fade animation starts to glitch. The fading pages are not immobile anymore, but begin to shake a bit. It seems that the computed x-translation is not accurate enough. Slide transitions on the other hand still work well.
Possible reason:
I started logging what exactly is happening during the fade transition. Since the given position value seemed to be corrent, so i started to log the pages x-position, wich resulted in this:
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
When regarding that the pages witdh is 1920px, it seems that the pages "new" pages x-position seems the be incremented by 1920px every time. This means that after some time, the pages x-position would be in the millions.
Since the x-translation is made in relation to the pages real position, im assuming that the value gets more and more inaccurate the higher the x-position gets.
My question
Is my assumption correct? Has anybody an idea how i can fix this? Im really out of ideas here...