There are many questions regarding crossfading in Android, but they all include animation. My crossfade question is using OnPageChangeListener from ViewPager.
I have a ViewPager that can have an unlimited number of views, but in practice uses about 6 or 7 views. Not much going on there.
Each view in the ViewPager has a background bitmap that needs to be locked and focused with the background of the next (or previous) view instead of scrolling along with the rest of the view.
To do this, I separated the backgrounds and added them to the ArrayList and assigned them ImageViews later. But since I do not want to risk that my activity will end with numerous ImageViews, I thought about the following structure:
<FrameLayout android:id="@+id/backgroundContainer" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/bottomImage" android:layout_height="match_parent" android:layout_width="match_parent" android:scaleType="center" /> <ImageView android:id="@+id/middleImage" android:layout_height="match_parent" android:layout_width="match_parent" android:scaleType="center" /> <ImageView android:id="@+id/topImage" android:layout_height="match_parent" android:layout_width="match_parent" android:scaleType="center" /> </FrameLayout>
Then a OnPageChangeListener is assigned to the ViewPager to assign the backgrounds for ImageViews.
@Override public void onPageSelected(int position) { MyLog.i(TAG, "PAGE SELECTED: " + position); if(position == 0) { _bottomBackground.setImageBitmap(null); _topBackground.setImageBitmap(_backgroundStack.get(position+1)); } else if (position == NUM_ITEMS-1) { _bottomBackground.setImageBitmap(_backgroundStack.get(position-1)); _topBackground.setImageBitmap(null); } else { _bottomBackground.setImageBitmap(_backgroundStack.get(position-1)); _topBackground.setImageBitmap(_backgroundStack.get(position+1)); } _middleBackground.setImageBitmap(_backgroundStack.get(position)); // Make the top front background transparent _topBackground.setAlpha(0f); _currentBackgroundPosition = position; }
This works great if I just wanted to change the backgrounds. I want the backgrounds to intersect with each other while the user views the ViewPager. I have an extinction for working with forward scrolling, but I donβt understand why the disappearance of scrolling backwards somehow does not give a good result. During backward scrolling, the middle background should disappear in the background.
I'm afraid I missed something. I never change the background alpha, but the log results always show the same value for getAlpha() as for the middle background.
@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if(_currentBackgroundPosition == position) { // scroll forward _topBackground.setAlpha(positionOffset) } else { //scroll backward _middleBackground.setAlpha(positionOffset); } MyLog.i(TAG, "Bottom BackgroundAlpha: " + _bottomBackground.getAlpha()); MyLog.i(TAG, "Middle BackgroundAlpha: " + _middleBackground.getAlpha()); MyLog.i(TAG, "Top BackgroundAlpha: " + _topBackground.getAlpha()); }
And wait! There is one more thing that I really cannot figure out how to fix. Although the direct scroll scroll works. A very short flicker flashed in the background. I assume this is due to the way I configured the onPageSelected method.
Is there any other way I can create / fix this behavior?