androidandroid-launcherandroid-wallpaper

Android animate wallpaper scroll with ViewPager using setWallpaperOffsets


I am making an android launcher and I want to make the wallpaper scroll when the page scroll so I add a onPageChangeListener to mViewPager and change the wallpaper offsets

mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){
            @Override
            public void onPageScrolled(int p1, float p2, int p3) {

            }

            @Override
            public void onPageSelected(int p1) {
               WallpaperManager.getInstance(c).setWallpaperOffsets(v.getWindowToken(),(float)p1/(myPageItemsCount-1), 0);
            }

            @Override
            public void onPageScrollStateChanged(int p1) {

            }
        });

The problem is that I want the wallpaper to smooth scroll when the page changed but in my above code , it will directly change the wallpaper to the corresponding position , please help.


Solution

  • Here's what I did and it works like a charm:

    pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                WallpaperManager.getInstance(getBaseContext()).setWallpaperOffsets(pager.getWindowToken(), (float) (position + positionOffset) / (myPageItemsCount-1), 0);
            }
    
            @Override
            public void onPageSelected(int position) {
            }
    
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }