How to set wallpaper in android that fit across all the screens without stretching, Zooming and cropping
Asked Answered
M

2

0

Note: Please don't mark it as duplicate as the same question is asked years back, which might have worked then, but not working now.

using below code my wallpaper is either stretching to screen 2 and 3 or getting cropped at the bottom of the screen based on the device. The below code is working on Samsung devices but not on other manufacturers devices.

I also tried using using the following methods for getting height and width, which didnt work(Wallpaper zoomed and stretched to other screens). getWallpaperDesiredMinimumHeight() and getWallpaperDesiredMinimumWidth()

public void setWallpaper() {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels;

    WallpaperManager wm = WallpaperManager.getInstance(this);
    Bitmap bmap = BitmapFactory.decodeResource(getResources(), R.drawable.myPicture);
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmap, width, height, true);
    wm.setWallpaperOffsetSteps(1, 1);
    wm.suggestDesiredDimensions(width, height);
    try {
        wm.setBitmap(scaledBitmap);
       } catch (IOException e) {

    }

}
Micelle answered 8/3, 2022 at 21:15 Comment(4)
How are you planning on handling varying aspect ratios without "stretching, Zooming [or] cropping"?Harms
@Harms I am only planning to use the wallpaper in portrait mode across all the devices. I don't mind the wallpaper stretching and fitting in a single screen and centered. 1. If the device has 3 virtual screens, I want my wallpaper to fit screen one and be centered, and the same should be shown on screens 2 and 3. 2. The problem I am facing is, if the wallpaper stretches a bit more than screen one, the image is not being centered and scrolling to screens 2 and 3.Micelle
"I am only planning to use the wallpaper in portrait mode across all the devices" -- that does not change the fact that there are many different aspect ratios across the tens of thousands of Android device models. "I don't mind the wallpaper stretching and fitting in a single screen and centered" -- your question does not agree.Harms
@Harms I am getting the device width and height using display metrics as written in code and scaling my wallpaper as per the current device width and height, and setting the scaled image as wallpaper. In the question, I tried to explain stretching and zooming into screens 2 and 3 but the limit of words didn't allow me. I hope you understand my problem now. Please suggest if there is any solution I can use to fix this issue.Micelle
M
0

Manufactures have different ways of setting up the wallpaper, Setting a wallpaper as movable or setting wallpaper as fixed.

On android wallpaper is zoomed, when we drag from the top of home screen, you can see the wallpaper getting zoomed out. So I scaled the height and width of my wallpaper as below, which helped me set the wallpaper correctly on my devices. Below is the updated code.

public void setWallpaper() {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels;
        height = (int) (height * (95.0f / 100.0f));
        width = (int) (width * (85.0f / 100.0f));

    WallpaperManager wm = WallpaperManager.getInstance(this);
    Bitmap bmap = BitmapFactory.decodeResource(getResources(), R.drawable.myPicture);
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmap, width, height, true);
    wm.setWallpaperOffsetSteps(1, 1);
    wm.suggestDesiredDimensions(width, height);
    try {
        wm.setBitmap(scaledBitmap);
       } catch (IOException e) {

    }

}
Micelle answered 21/7, 2022 at 20:51 Comment(0)
D
0

According to my experience (I have tested more than 10 devices) there are three kinds of background behavior of launchers:

  1. Scrollable;
  2. Static or scrollable (depends of the current bitmap size);
  3. Static (more than 75%).

Also launcher may be scrollable in portrait mode and static in landscape mode.

If you want to set wallpaper without "stretching, zooming and cropping" for kinds 2 or 3 you have to rescale and/or crop a bitmap to display size and call setBitmap method of WallpaperManager instance.

Else you have to get desired wallpaper size

WallpaperManager wm = WallpaperManager.getInstance(context);
int dx = wm.getDesiredMinimumWidth();
int dy = wm.getDesiredMinimumHeight();

rescale the bitmap to dx, dy and call wm.setBitmap(bm);

There is no way to detect which kind of launcher on the device. You can get equal dx and dy but launcher is not scrollable etc. (Meizu Lite 5 just shift a bitmap to the right, Honor 10 just center a bitmap ). So I recommend use an option.

Method suggestDesiredDimensions is useless in this case (use permission android.permission.SET_WALLPAPER_HINTS).

Method setWallpaperOffsetStep is ignored by launcher. It calculates step by fixed ratio and amount of virtual screens.

Delaine answered 9/9, 2022 at 13:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.