我正在尝试设置一个壁纸,但我得到了乐队。我有一个大的渐变JPG,它保存在我的设备上。我从文件中读取它,缩放它以使其高度与设备的高度相匹配然后我设置壁纸和壁纸提示。缩放步骤似乎是将其转换为RGB565格式而不是原始的ARGB888格式。此外,我似乎没有任何可能有助于缓解条带的抖动。
这是我的代码:
public class WallpaperSetter {
public static void setWallpaper(String url, Context context) throws IOException {
FileCache cache = new FileCache(context);
File f = cache.getFile(url);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = true;
Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int targetHeight = display.getHeight() > display.getWidth() ? display.getHeight() : display.getWidth() - 10;
int targetWidth = (int) ((float) targetHeight / (float) bmp.getHeight() * (float) bmp.getWidth());
Bitmap resizedBitmap = resize(bmp, targetHeight, targetWidth);
WallpaperManager manager = (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE);
manager.setBitmap(resizedBitmap);
int displayHeight = display.getHeight() > display.getWidth() ? display.getHeight() : display.getWidth();
int displayWidth = display.getHeight() > display.getWidth() ? display.getWidth() : display.getHeight();
int height = resizedBitmap.getHeight() > displayHeight ? resizedBitmap.getHeight() : displayHeight;
int width = resizedBitmap.getWidth() < displayWidth ? displayWidth : resizedBitmap.getWidth();
manager.suggestDesiredDimensions(width, height);
}
private static Bitmap resize(Bitmap bitmap, int targetHeight, int targetWidth) {
System.out.println("config start: " + bitmap.getConfig().name().toString());
Bitmap b = Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, false);
System.out.println("config: " + b.getConfig().name().toString());
return b;
}
}
我正在开发使用CyanogenMod的SGS2,如果这有所作为的话。
答案 0 :(得分:0)
我发现在渐变图像中添加一些噪点或微妙的随机性有助于减少条带,但我怀疑使用至少包含alpha像素的.png(奇怪但有效)和RGB565格式是可行的方法。此外,我发现使用'原始'资源文件夹而不是'drawable'文件夹阻止Android假设它可以在内部压缩图像。这是我用过的代码:
private void generateBackgroundGraphic() {
background = BitmapFactory.decodeResource(res, R.raw.gradient);
//create local copy of 'background' bitmap size
Bitmap tempB = Bitmap.createBitmap(background.getWidth(), background.getHeight(), Bitmap.Config.RGB_565);
backgroundPaint.setDither(true);
//wrap a canvas around 'background' bitmap
Canvas tempC = new Canvas (tempB);
tempC.drawBitmap(background, 0, 0, null);
background = Bitmap.createScaledBitmap(tempB, globalCanvasSize.x, globalCanvasSize.y, false);
tempB.recycle();
tempB = null;
tempC = null;
}
我希望这对你有用...... Android有时奇怪地处理图像: - /
克里斯
答案 1 :(得分:0)
即使我怀疑有一种更简单的方法可以做到这一点,我的最终代码也能正常运行:
public class WallpaperSetter {
public static void setWallpaper(String url, Context context) throws IOException {
FileCache cache = new FileCache(context);
File f = cache.getFile(url);
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int targetHeight = display.getHeight() > display.getWidth() ? display.getHeight() : display.getWidth() - 10;
int targetWidth = (int) ((float) targetHeight / (float) o.outHeight * (float) o.outWidth);
while (true) {
if (width_tmp / 2 < targetWidth || height_tmp / 2 < targetHeight)
break;
width_tmp /= 2;
height_tmp /= 2;
scale++;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
o2.inPreferredConfig = Bitmap.Config.ARGB_8888;
o2.inDither = false;
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
Bitmap b565 = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.RGB_565);
Canvas c = new Canvas(b565);
c.drawBitmap(b, 0, 0, null);
b = Bitmap.createScaledBitmap(b565, targetWidth, targetHeight, false);
b565.recycle();
b565 = null;
c = null;
WallpaperManager manager = (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE);
manager.setBitmap(b);
int displayHeight = display.getHeight() > display.getWidth() ? display.getHeight() : display.getWidth();
int displayWidth = display.getHeight() > display.getWidth() ? display.getWidth() : display.getHeight();
int height = b.getHeight() > displayHeight ? b.getHeight() : displayHeight;
int width = b.getWidth() < displayWidth ? displayWidth : b.getWidth();
manager.suggestDesiredDimensions(width, height);
}
}