I would like to set a certain Drawable
as the device\'s wallpaper, but all wallpaper functions accept Bitmap
s only. I cannot use WallpaperMan
A Drawable
can be drawn onto a Canvas
, and a Canvas
can be backed by a Bitmap
:
(Updated to handle a quick conversion for BitmapDrawable
s and to ensure that the Bitmap
created has a valid size)
public static Bitmap drawableToBitmap (Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();
}
int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}