Android ViewPager - 此设计的平滑过渡

时间:2015-09-03 20:22:25

标签: android android-viewpager android-pagetransformer

我无法确定数学,以便为以下设计做平滑过渡。任何帮助将不胜感激:)。

在过渡期间,页面会上下跳跃,但我希望它们能够平滑。

设计

enter image description here

过渡

During transition, the pages are jumping up and down but I would like them to be smooth instead.

关联ViewPage代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(pagerAdapter);
    viewPager.setOffscreenPageLimit(3);
    viewPager.setPageMargin(100);
    viewPager.setPageTransformer(false, new ViewPager.PageTransformer() {
        @Override
        public void transformPage(View page, float position) {
            int pageWidth = viewPager.getMeasuredWidth() - viewPager.getPaddingLeft() - viewPager.getPaddingRight();
            int pageHeight = viewPager.getHeight();
            int paddingLeft = viewPager.getPaddingLeft();
            float transformPos = (float) (page.getLeft() - (viewPager.getScrollX() + paddingLeft)) / pageWidth;

            final float normalizedposition = Math.abs(Math.abs(transformPos) - 1);
            page.setAlpha(normalizedposition + 0.5f);

            if (transformPos < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                page.setTranslationY(0);
            } else if (transformPos <= 1) { // [-1,1]
                page.setTranslationY(-pageHeight / 10);

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


        }
    });

}

注意:&#39; transformPos&#39;用于解决此错误 - https://code.google.com/p/android/issues/detail?id=64046

我的ViewPager

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="260dp"
    android:layout_centerInParent="true"
    android:clipToPadding="false"
    android:paddingLeft="80dp"
    android:paddingRight="80dp"/>

完整来源 - https://github.com/krishnaraj/ViewPagerSample

3 个答案:

答案 0 :(得分:4)

试试这个

unsigned char *memory;
memory = (unsigned char *)malloc(MYSIZE*sizeof(unsigned char)); 

type* p1;
p1 = (type *)((int)memory + p1_offset);

type** p2;
p2 = (type **)((int)memory + p2_offset);
for (int i=0 ; i<p2_height ; i++)
{
    p2[i] = (type *)((int)p2 + p2_width*sizeof(type));
}

答案 1 :(得分:1)

将您的if更改为:

float upTranslation = -pageHeight / 10f;
if (transformPos < -1) {// [-Infinity,-1)
    page.setTranslationY(0);
} else if (transformPos < 0) { // [-1,0)
    float translationY = upTranslation * (transformPos + 1f);
    page.setTranslationY(translationY);
} else if (transformPos == 0) { // 0
    page.setTranslationY(upTranslation);
} else if (transformPos <= 1) { // (0,1]
    float translationY = upTranslation * (1f - transformPos);
    page.setTranslationY(translationY);
} else { // (1,+Infinity]
    page.setTranslationY(0);
}

答案 2 :(得分:0)

如果您真的不需要pageTransformer,请使用以下代码

    viewPager.setPageTransformer(false, new ViewPager.PageTransformer() {
    @Override
    public void transformPage(View page, float position) {
        if(position==1){

            int pageWidth = viewPager.getMeasuredWidth() - viewPager.getPaddingLeft() - viewPager.getPaddingRight();
            int pageHeight = viewPager.getHeight();
            int paddingLeft = viewPager.getPaddingLeft();
            float transformPos = (float) (page.getLeft() - (viewPager.getScrollX() + paddingLeft)) / pageWidth;

            final float normalizedposition = Math.abs(Math.abs(transformPos) - 1);
            page.setAlpha(normalizedposition + 0.5f);

            if (transformPos < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                page.setTranslationY(0);
            } else if (transformPos <= 1) { // [-1,1]
                page.setTranslationY(-pageHeight / 10);

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

然后再试一次......