Home » Android » size – Android WallpaperManager crops image

size – Android WallpaperManager crops image

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am working on a simple wallpaper app of some images that I have. They are .png files in drawable folders.

Here are some code snippets:

WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext()); 
....
myWallpaperManager.setResource(R.drawable.image1);

No matter what size or resolution I seem to use, when the wallpaper is set it crops the original image. When I use the same image as a background it is the correct size and shows the entire image. I thought it might be a problem with my emulator so I have tried running it on an actual device (HTC eris) and I am having the same problem. I have made the image the screen size and resolution for the eris and it is still cropping it. I then made the image only one inch high and a resolution of 100 dpi. It was very pixelated on the eris, but still cropped the image.

Any help would be greatly appreciated.

I attempted to add some images to show the before and after, but as a newer user I was prevented from doing so.

How to&Answers:

Check the values returned by http://developer.android.com/reference/android/app/WallpaperManager.html#getDesiredMinimumWidth() and http://developer.android.com/reference/android/app/WallpaperManager.html#getDesiredMinimumHeight() and try to use these values as the dimensions of your wallpaper.

Answer:

I had the same problem. I made an image that is the size of the screen and adding padding to the image so that it is as large as the WallpaperManager getDesiredMinimumWidth() and getDesiredMinimumHeight().

It seemed better to have some code automatically add the padding so that is what I used below. Make sure the image is the same size as the screen.

private void setWallpaper() {
    try {
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
        //import non-scaled bitmap wallpaper
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;
        Bitmap wallpaper = BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper, options);

        if (wallpaperManager.getDesiredMinimumWidth() > wallpaper.getWidth() &&
                wallpaperManager.getDesiredMinimumHeight() > wallpaper.getHeight()) {
            //add padding to wallpaper so background image scales correctly
            int xPadding = Math.max(0, wallpaperManager.getDesiredMinimumWidth() - wallpaper.getWidth()) / 2;
            int yPadding = Math.max(0, wallpaperManager.getDesiredMinimumHeight() - wallpaper.getHeight()) / 2;
            Bitmap paddedWallpaper = Bitmap.createBitmap(wallpaperManager.getDesiredMinimumWidth(), wallpaperManager.getDesiredMinimumHeight(), Bitmap.Config.ARGB_8888);
            int[] pixels = new int[wallpaper.getWidth() * wallpaper.getHeight()];
            wallpaper.getPixels(pixels, 0, wallpaper.getWidth(), 0, 0, wallpaper.getWidth(), wallpaper.getHeight());
            paddedWallpaper.setPixels(pixels, 0, wallpaper.getWidth(), xPadding, yPadding, wallpaper.getWidth(), wallpaper.getHeight());

            wallpaperManager.setBitmap(paddedWallpaper);
        } else {
            wallpaperManager.setBitmap(wallpaper);
        }
    } catch (IOException e) {
        Log.e(TAG,"failed to set wallpaper");
    }
}

Answer:

Maybe I can help.


// 1. Get screen size.
DisplayMetrics metrics = new DisplayMetrics();
Display display = getWindowManager().getDefaultDisplay();
display.getMetrics(metrics);
final int screenWidth  = metrics.widthPixels;
final int screenHeight = metrics.heightPixels;

// 2. Make the wallpaperManager fit the screen size.
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(ViewWallpaperActivity.this);
wallpaperManager.suggestDesiredDimensions(screenWidth, screenHeight);

// 3. Get the desired size.
final int width = wallpaperManager.getDesiredMinimumWidth();
final int height = wallpaperManager.getDesiredMinimumHeight();

 // 4. Scale the wallpaper.
Bitmap bitmap = getBitmap(); // getBitmap(): Get the image to be set as wallpaper.
Bitmap wallpaper = Bitmap.createScaledBitmap(bitmap, width, height, true);

// 5. Set the image as wallpaper.
try {
  wallpaperManager.setBitmap(wallpaper);
} catch (IOException e) {
  e.printStackTrace();
}

Note that you should call suggestDesiredDimensions, then call getDesiredMinimumWidth and getDesiredMinimumHeight to get the size to be scaled to.