Is it possible to install the wallpaper in the same way as it is done with the application that comes with Android?

Android has the ability to set desktop wallpaper. The user types a “menu” and then selects “wallpaper” to set the wallpaper from the system. The resulting image of the wallpaper is correctly scaled both in portrait and landscape modes.

I made a small application that allows you to change the desktop wallpaper. It works fine, but I can’t understand what the secret is for the image to be the right size after it has been set as wallpaper.

I did this with png images that were 1280x1084, and also tried the same thing with 320x240 images, and they all appear the same size when the desktop wallpaper is set.

I searched for tutorials and examples of how to set wallpapers, how they are, but could not learn how to do it. Can you show me a sample code showing me the secret of this so that the resulting wallpaper is properly scaled?

I am sure that some WallpaperManager parameter should be used, but I do not know which one to use.

Thanks in advance.

Here is the code I use 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(); } } 

Indeed, Imad

+4
source share
1 answer

I just looked at it. Although you can observe the actual file sizes of the wallpaper images in Launcher.apk, the best way is to request 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 required size and write my own bitmap into it. The hard part is calculating padding and scaling, as well as accounting for the status bar. However, once you draw this image with the correct size as the wallpaper, it should work just like the system wallpaper.

Now the flip side - this image (with sizes larger than your screen) scales correctly, but you will notice that it is cropped differently depending on your orientation.

I think you need live wallpapers if you want to resize the image depending on the orientation (to increase the size of the image, but not crop it).

+3
source

All Articles