4

Android has a way to set the home screen wallpaper. The user taps "menu" and then selects "wallpaper" to set a wallpaper from the system. The resulting wallpaper image is properly scaled in both portrait and landscape mode.

I did a small app that allows the home screen wallpaper to be changed. It works fine but I can't find out what the secret is to get the image to be the correct size after it's set as a wallpaper.

I did this with png images that are 1280x1084 and also tried the same thing with images that are 320x240 and they all are shown the same size when set as a home screen wallpaper.

I looked for tutorials and examples on how to set a wallpaper like they do, but couldn't find out how to do it. Can you show me a code sample showing me the secret to this so the resulting wallpaper is scaled correctly?

I'm sure there must be some kind of WallpaperManager setting to use but I don't know which one to use.

Thanks in advance.

Here's the code I'm using to set the wallpaper:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    WallpaperManager myWallpaperManager = WallpaperManager
            .getInstance(getApplicationContext());

    try {
        myWallpaperManager.setResource(R.drawable.kabanight1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Truly, Emad

Emad-ud-deen
  • 4,734
  • 21
  • 87
  • 152
  • how do you mean "shown the same size"? do you mean that both images are equal in size to each other, or do you mean that they are still the size they were before you set them as wallpaper? i.e. 1280x1024 and 320x240 respectively? – davogotland Oct 15 '11 at 13:02
  • When set as a wallpaper the image gets expanded to a size determined by Android if it's 320x240 and made smaller if it's 1280x1024. I would like to have Android scale the image just like it does when the user uses the "menu" -> "wallpaper" way of setting the wallpaper from the supplied system wallpapers. Maybe my images files need to be the same size as the system wallpapers images before the user runs my app to set the wallpaper? – Emad-ud-deen Oct 15 '11 at 13:12

1 Answers1

3

I was just looking at this. While you can observe actual file sizes from wallpaper images in the Launcher .apk, a better way is to query the WallpaperManager for the desired size (which is independent of orientation).

final WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
final int fullWidth = wallpaperManager.getDesiredMinimumWidth();
final int fullHeight = wallpaperManager.getDesiredMinimumHeight();

At this point, I create a new bitmap of the exact required size and write my own bitmap into it. The tricky part is calculating padding and scaling, as well as accounting for the status bar. However, once you write this properly-sized image as a wallpaper, it should work exactly like a system wallpaper.

Now the downside - that image (with dimensions larger than your screen) is scaled properly, but you'll note that it is cropped differently depending on your orientation.

I think you'd need a live wallpaper if you wanted to resize the image depending on orientation (to maximize the size of the image but not crop it).

ProjectJourneyman
  • 3,566
  • 1
  • 27
  • 37