问题
I'm trying to make an app that changes the system wallpaper. In my code I get an image that has the desired minimum dimensions (from WallpaperManager). For example on the Nexus One the desired minimum dimensions are 884x800. When I get my image andset it as the wallpaper it automatically "left aligns" it so that I can see only the left side of the 884x800 image (The Nexus One's screen res is 480x800).
Is there a way I can set the wallpaper so it is "centered"?
I am setting the wallpaper like this:
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
Log.e("Error", e.toString());
}
NOTE: If I get the image as 480x800 it blows it up so I can only see the top left corner when it is the wallpaper.
Here is an example of an image that is 884x800:
Here is an example of what it looks like when I set it as the wallpaper:
Here is an example of what it looks like when I use a 480x800 image:
回答1:
Have you tried to switch between virtual home screens? Aren't you at the leftmost virtual home screen?
回答2:
MY SOLUTION
In the end I got the image in the screen size (480x800 for the Nexus One) and then copied it into a bitmap that was the desired dimensions (884x800 for the Nexus One).
//Getting the image
Display display = getWindowManager().getDefaultDisplay();
screenSize = new Point();
display.getSize(screenSize);
new LoadImageTask(screenSize.x, screenSize.y, this).execute();
...
// In the response listener
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
Point desiredSize = new Point(
wallpaperManager.getDesiredMinimumWidth(),
wallpaperManager.getDesiredMinimumHeight());
Bitmap wallpaper = Bitmap.createBitmap(desiredSize.x, desiredSize.y, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(wallpaper);
canvas.drawBitmap(bitmap, 0, 0, null);
try {
wallpaperManager.setBitmap(wallpaper);
} catch (IOException e) {
Log.e("Error", e.toString());
}
And it works on all virtual screens
来源:https://stackoverflow.com/questions/18499133/center-android-wallpaper