Josh Laird Josh Laird - 1 month ago 15
Android Question

Android: PageTransformer applying correct scale but not alpha before first touch

I have a

PageTransformer
that correctly displays my
ViewPager
with the second element correctly scaled, but there is no alpha applied to the second element. However, upon dragging the
ViewPager
a miniscule amount, the next view is properly displayed. The code for both my alpha and scale is identical, I'm not sure what the issue here is.

I have tried calling code from a handler once the
ViewPagerAdapter
has things added to it (such as
viewPager.scrollBy()
and
pager.beginFakeDrag()
) but these didn't help.

viewPager.setOffscreenPageLimit(2)
is set. I have tried setting alpha to the views programmatically when instantiated in the
Adapter
but it has no effect. If the scale is correctly being applied then the alpha should be as well, surely.

@Override
public void transformPage(View page, float position)
{
float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
float alphaFactor = Math.max(MIN_ALPHA, 1 - Math.abs(position));

page.setScaleX(scaleFactor);
page.setScaleY(scaleFactor);

page.setAlpha(alphaFactor);
}

Answer

I had android:animateLayoutChanges=true set on the ViewPager. Once I removed this, it loaded correctly. Don't ask me why it was there in the first place.

Comments