Why Should I call setTranslationX() in transformPage() , doesnt it effect the final postion of the View?

821 views Asked by At

I'm having trouble understanding the usage of transformPage() method in PageTransformer class.

Take the Official ZoomOut Transformer as an example:

public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();
        int pageHeight = view.getHeight();

        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.setAlpha(0);

        } else if (position <= 1) { // [-1,1]
            // Modify the default slide transition to shrink the page as well
            float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
            float vertMargin = pageHeight * (1 - scaleFactor) / 2;
            float horzMargin = pageWidth * (1 - scaleFactor) / 2;
            if (position < 0) {
                view.setTranslationX(horzMargin - vertMargin / 2);
            } else {
                view.setTranslationX(-horzMargin + vertMargin / 2);
            }

            // Scale the page down (between MIN_SCALE and 1)
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

            // Fade the page relative to its size.
            view.setAlpha(MIN_ALPHA +
                    (scaleFactor - MIN_SCALE) /
                    (1 - MIN_SCALE) * (1 - MIN_ALPHA));

        } else { // (1,+Infinity]
            // This page is way off-screen to the right.
            view.setAlpha(0);
        }
    }

I understand we need to consider two views, one is on left(-1--0) and the other on right hand side(0--1)

but i dont understand the view.setTranslationX() usage, seems like even if i delete those lines of code, the difference is just the gap between the two views when swiping, see the pics I upload(also I delete the scaling code, as you can see there are even overlapping)

after delete the scaling code view.setScaleX() and Y() , there's even overlapping, and its permanent effect,

So my question is why should we use the setTranslationX() method during swiping , the effect on the view's position is like forever.

1

There are 1 answers

0
Diomas On BEST ANSWER

view.setTranslationX() adds a shift to initial position of the page inside ViewPager. By default pages just go side-by-side. This code:

if (position < 0) {
    view.setTranslationX(horzMargin - vertMargin / 2);
} else {
    view.setTranslationX(-horzMargin + vertMargin / 2);
}

makes them slightly overlaps. Page from the left of the center of ViewPager (position = 0) is shifted to the right and vise versa.