Is it possible to set the wallpaper just like it's done with the app that comes with Android?
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 i开发者_Go百科mage 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
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).
精彩评论