Android 如何将 Drawable 转换为位图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3035692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a Drawable to a Bitmap?
提问by Rob
I would like to set a certain Drawable
as the device's wallpaper, but all wallpaper functions accept Bitmap
s only. I cannot use WallpaperManager
because I'm pre 2.1.
我想设置某个Drawable
作为设备的壁纸,但所有壁纸功能Bitmap
只接受s。我无法使用,WallpaperManager
因为我是 2.1 之前的版本。
Also, my drawables are downloaded from the web and do not reside in R.drawable
.
另外,我的 drawables 是从网上下载的,不存在于R.drawable
.
采纳答案by Rob
This converts a BitmapDrawable to a Bitmap.
这会将 BitmapDrawable 转换为 Bitmap。
Drawable d = ImagesArrayList.get(0);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
回答by Praveen
This piece of code helps.
这段代码有帮助。
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_resource);
Here a version where the image gets downloaded.
这是下载图像的版本。
String name = c.getString(str_url);
URL url_value = new URL(name);
ImageView profile = (ImageView)v.findViewById(R.id.vdo_icon);
if (profile != null) {
Bitmap mIcon1 =
BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
profile.setImageBitmap(mIcon1);
}
回答by André
public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
回答by kabuko
A Drawable
can be drawn onto a Canvas
, and a Canvas
can be backed by a Bitmap
:
ADrawable
可以绘制到 a 上Canvas
,并且 aCanvas
可以由 a 支持Bitmap
:
(Updated to handle a quick conversion for BitmapDrawable
s and to ensure that the Bitmap
created has a valid size)
(更新以处理BitmapDrawable
s的快速转换并确保Bitmap
创建的具有有效大小)
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;
}
回答by Keyur
METHOD 1: Either you can directly convert to bitmap like this
方法1:您可以像这样直接转换为位图
Bitmap myLogo = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
METHOD 2: You can even convert the resource into the drawable and from that you can get bitmap like this
方法 2:您甚至可以将资源转换为可绘制对象,从中您可以获得像这样的位图
Bitmap myLogo = ((BitmapDrawable)getResources().getDrawable(R.drawable.logo)).getBitmap();
For API > 22getDrawable
method moved to the ResourcesCompat
class so for that you do something like this
对于API > 22getDrawable
方法移动到ResourcesCompat
类中,因此您可以执行以下操作
Bitmap myLogo = ((BitmapDrawable) ResourcesCompat.getDrawable(context.getResources(), R.drawable.logo, null)).getBitmap();
回答by Erfan Bagheri
very simple
很简单
Bitmap tempBMP = BitmapFactory.decodeResource(getResources(),R.drawable.image);
回答by Chris.Jenkins
So after looking (and using) of the other answers, seems they all handling ColorDrawable
and PaintDrawable
badly. (Especially on lollipop) seemed that Shader
s were tweaked so solid blocks of colors were not handled correctly.
所以其他的答案中寻找(并使用)之后,他们似乎都处理ColorDrawable
和PaintDrawable
严重。(特别是在棒棒糖上)似乎对Shader
s 进行了调整,因此无法正确处理纯色块。
I am using the following code now:
我现在使用以下代码:
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
// We ask for the bounds if they have been set as they would be most
// correct, then we check we are > 0
final int width = !drawable.getBounds().isEmpty() ?
drawable.getBounds().width() : drawable.getIntrinsicWidth();
final int height = !drawable.getBounds().isEmpty() ?
drawable.getBounds().height() : drawable.getIntrinsicHeight();
// Now we check we are > 0
final Bitmap bitmap = Bitmap.createBitmap(width <= 0 ? 1 : width, height <= 0 ? 1 : height,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
Unlike the others, if you call setBounds
on the Drawable
before asking to turn it into a bitmap, it will draw the bitmap at the correct size!
不像其他人,如果你拨打setBounds
的Drawable
要求把它变成一个位图前,将以此为位图以正确的尺寸!
回答by Mauro
Maybe this will help someone...
也许这会帮助某人......
From PictureDrawable to Bitmap, use:
从 PictureDrawable 到 Bitmap,使用:
private Bitmap pictureDrawableToBitmap(PictureDrawable pictureDrawable){
Bitmap bmp = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawPicture(pictureDrawable.getPicture());
return bmp;
}
... implemented as such:
... 实现如下:
Bitmap bmp = pictureDrawableToBitmap((PictureDrawable) drawable);
回答by Dawid Drozd
Here is better resolution
这是更好的分辨率
public static Bitmap drawableToBitmap (Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
public static InputStream bitmapToInputStream(Bitmap bitmap) {
int size = bitmap.getHeight() * bitmap.getRowBytes();
ByteBuffer buffer = ByteBuffer.allocate(size);
bitmap.copyPixelsToBuffer(buffer);
return new ByteArrayInputStream(buffer.array());
}
Code from How to read drawable bits as InputStream
回答by tasomaniac
Here is the nice Kotlin version of the answer provided by @Chris.Jenkins here: https://stackoverflow.com/a/27543712/1016462
这是@Chris.Jenkins 在这里提供的答案的 Kotlin 版本:https://stackoverflow.com/a/27543712/1016462
fun Drawable.toBitmap(): Bitmap {
if (this is BitmapDrawable) {
return bitmap
}
val width = if (bounds.isEmpty) intrinsicWidth else bounds.width()
val height = if (bounds.isEmpty) intrinsicHeight else bounds.height()
return Bitmap.createBitmap(width.nonZero(), height.nonZero(), Bitmap.Config.ARGB_8888).also {
val canvas = Canvas(it)
setBounds(0, 0, canvas.width, canvas.height)
draw(canvas)
}
}
private fun Int.nonZero() = if (this <= 0) 1 else this