我有设置壁纸的代码(需要android.permission.SET_WALLPAPER)
// this is inside activity
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
wallpaperManager.setBitmap(bitmap);
我的问题是,如何调整此代码以便将壁纸设置为适合屏幕两种选项:
注意:在将其设置为壁纸之前,我已经尝试将位图缩放到屏幕的分辨率,但它实际上看起来比没有缩放更有价值。
答案 0 :(得分:0)
我制作了一个应用程序来设置壁纸,我尝试了很多东西......这个方法对我来说非常有效:
// Crop or inflate bitmap to desired device height and/or width
public Bitmap prepareBitmap(final Bitmap sampleBitmap,
final WallpaperManager wallpaperManager) {
Bitmap changedBitmap = null;
final int heightBm = sampleBitmap.getHeight();
final int widthBm = sampleBitmap.getWidth();
final int heightDh = wallpaperManager.getDesiredMinimumHeight();
final int widthDh = wallpaperManager.getDesiredMinimumWidth();
if (widthDh > widthBm || heightDh > heightBm) {
final int xPadding = Math.max(0, widthDh - widthBm) / 2;
final int yPadding = Math.max(0, heightDh - heightBm) / 2;
changedBitmap = Bitmap.createBitmap(widthDh, heightDh,
Bitmap.Config.ARGB_8888);
final int[] pixels = new int[widthBm * heightBm];
sampleBitmap.getPixels(pixels, 0, widthBm, 0, 0, widthBm, heightBm);
changedBitmap.setPixels(pixels, 0, widthBm, xPadding, yPadding,
widthBm, heightBm);
} else if (widthBm > widthDh || heightBm > heightDh) {
changedBitmap = Bitmap.createBitmap(widthDh, heightDh,
Bitmap.Config.ARGB_8888);
int cutLeft = 0;
int cutTop = 0;
int cutRight = 0;
int cutBottom = 0;
final Rect desRect = new Rect(0, 0, widthDh, heightDh);
Rect srcRect = new Rect();
if (widthBm > widthDh) { // crop width (left and right)
cutLeft = (widthBm - widthDh) / 2;
cutRight = (widthBm - widthDh) / 2;
srcRect = new Rect(cutLeft, 0, widthBm - cutRight, heightBm);
} else if (heightBm > heightDh) { // crop height (top and bottom)
cutTop = (heightBm - heightDh) / 2;
cutBottom = (heightBm - heightDh) / 2;
srcRect = new Rect(0, cutTop, widthBm, heightBm - cutBottom);
}
final Canvas canvas = new Canvas(changedBitmap);
canvas.drawBitmap(sampleBitmap, srcRect, desRect, null);
} else {
changedBitmap = sampleBitmap;
}
return changedBitmap;
}
来自this manual的代码,您可以在其中找到更多方法来正确调整图像大小。