View translation in sliding a ViewPager

前端 未结 4 1148
猫巷女王i
猫巷女王i 2021-02-13 11:26

I was reading an example on customizing ViewPager\'s sliding page animation that entails translating the page(View) to a certain amount. The example is

4条回答
  •  臣服心动
    2021-02-13 12:12

    As I re-read your question and this answer, I'm not sure I've really addressed your question. I think it may be of some help though so I'm posting it.

    According to this, position=0 means this page is front & center (& for this example, full screen). We only care about a page with position in the -1 to +1 range, otherwise it is too far out of view to care about. You can see this in the 1st & last conditions where alpha is set to 0, making the view fully transparent.

    I am unable to understand the statement,

    view.setTranslationX(horzMargin - vertMargin / 2);

    As I looked at it, I didn't see much value in this section of code as well. Since the margins are calculated based on the scaleFactor, & that is limited to 0.85 - 1.0, it doesn't make a lot of difference in the appearance of the transition. I'm sure that someone with a much better eye for design than me would disagree. However, I removed this portion of code as an experiment.

            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);
            }
    

    While I could see small differences depending on setting translationX if I looked closely, I'd never notice with casual use. More of a difference can be seen by setting MIN_SCALE to 0.5 (or smaller).

    what would be wrong with my calculation?

    Probably nothing as long as you limited it to a small result like the current code does. Keep in mind though that the ViewPager class is the primary controller of the animation rather than setTranslationX().

提交回复
热议问题