0

I am developing a wallpaper application in Android and i am finding a right way to set scrollable wallpaper for my app. Now, my code can set wallpaper from bitmap but it was cropped to fit with one page and just stayed only on one page (i have 5 pages in home screen). That means the content in each page can scroll through the wallpaper but the wallpaper was not scroll.

I want to set a scrollable wallpaper. I tried some codes from internet but they did not help. Do you guys have any idea?

This is my code

WallpaperManager wm = WallpaperManager.getInstance(mActivity.getContext());
    try {
        wm.setBitmap(mCropImageView.getCroppedImage());
    } catch (IOException e) {
        e.printStackTrace();
    }

3 Answers3

3

Try this, it worked for me on api>11

//get screen height
Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    screenHeight = size.y;

 wallPaperBitmap= ... //your bitmap resource

//adjust the aspect ratio of the Image
//this is the main part

int width = wallPaperBitmap.getWidth();
        width = (width * screenHeight) / wallPaperBitmap.getHeight();

//set the wallpaper
//this may not be the most efficent way but it worked for me

wallpaperManager.setBitmap(Bitmap.createScaledBitmap(wallPaperBitmap, width, height, true));
harsh_v
  • 3,193
  • 3
  • 34
  • 53
0

I wanted to have a scrollable wallpaper in my app as a background. (home screen replacement app)

NOT set a wallpaper (which can scroll) from my app.

But this question comes up as the first SO post on google when searching for scrollable wallpaper android stackoverflow.

So I decided to answer that question instead.

I made a demo app located here using kotlin

And it uses this method to scroll the wallpaper.

Which uses WallpaperManager.setWallpaperOffsets

addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
    override fun onPageScrollStateChanged(state: Int) {}

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
        val xOffset = (position + positionOffset) / (pageModels.size - 1)
        val wallpaperManager = WallpaperManager.getInstance(applicationContext)
        wallpaperManager.setWallpaperOffsets(viewPager.windowToken, xOffset, 0.0f)
    }
}

And a demo of the app I made video

Phani Rithvij
  • 4,030
  • 3
  • 25
  • 60
0

Try this, Its working.

final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
wallpaperManager.setWallpaperOffsetSteps(0F, 0F);


ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this, pageCount);
viewPager2.setAdapter(viewPagerAdapter);

viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        float xOffset = (position + positionOffset) / (pageCount - 1);
        wallpaperManager.setWallpaperOffsets(viewPager2.getWindowToken(), xOffset, 0.0f);
        super.onPageScrolled(position, positionOffset, positionOffsetPixels);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        super.onPageScrollStateChanged(state);
    }
});
Amit Bodaliya
  • 128
  • 2
  • 4