Reputation: 621
Let's say I have View X
on top of a ViewPager. X has a width and height of 50dp. Now when I scroll the ViewPager from page one to page two,
I would like for X to lower it's width and height relative to the current scroll position,
and end up with 20dp height and width at minimum after the ViewPager is fully scrolled to the second page.
I'm trying to use the positionOffset
value from onPageScrolled
, but I can't seem to figure it out how it works exactly, and convert it to pixels.
ViewPager.addOnPageListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
System.out.println(positionOffset);
}
});
Thanks, help is appreciated.
Upvotes: 1
Views: 259
Reputation: 36
positionOffset
change its value from 0 to 1 for each page (position
). Based on your example, you can change the scale of your view from 100% to 40%. Using positionOffset
as x, the function will be y=-0.6x+1.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if(position == 0) {
yourView.setScaleX((float)(-0.6 * positionOffset) + 1);
yourView.setScaleY((float)(-0.6 * positionOffset) + 1);
} else {
yourView.setScaleX(0.4f);
yourView.setScaleY(0.4f);
}
}
[...]
});
Upvotes: 2