Search code examples
androidandroid-bitmapandroid-wallpaper

android Scrollable wallpaper on screen's phone programmatically


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?

setImage_Wallpaper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = imageLoader.getDiscCache().get(urldisplay);
                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                WallpaperManager myWallpaperManager
                        = WallpaperManager.getInstance(mContext);
                try {
                    myWallpaperManager.setBitmap(bitmap);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

And I use from this code but don't work :

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

Solution

  • Post is old but anyway... You can try something similar to this

    public static void setWallpaper(Context context) {
        int wallpaperRId = getWallpaperImageRid(context);
    
        if (wallpaperRId == 0) {
            return;
        }
    
        Bitmap tempBmp = BitmapFactory.decodeResource(context.getResources(), wallpaperRId);
    
        // get size
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        int area = width * height / 1000;
    
        width *= 2;
        float scale = width / (float) tempBmp.getWidth();
        height = (int) (scale * tempBmp.getHeight());
    
        Bitmap bitmap = Bitmap.createScaledBitmap(tempBmp,width,height, true);
    
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
        wallpaperManager.setWallpaperOffsetSteps(1, 1);
        wallpaperManager.suggestDesiredDimensions(width, height);
    
        try {
            wallpaperManager.setBitmap(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }