如何在android中将位图转换为Drawable?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2415619/
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 Bitmap to Drawable in android?
提问by Farha Ansari
How can I convert a Bitmap image to Drawable ?
如何将位图图像转换为 Drawable ?
采纳答案by Graeme Duncan
Sounds like you want to use BitmapDrawable
听起来你想使用 BitmapDrawable
From the documentation:
从文档:
A
Drawable
that wraps a bitmap and can be tiled, stretched, or aligned. You can create aBitmapDrawable
from a file path, an input stream, through XML inflation, or from aBitmap
object.
一个
Drawable
一个包装了位图,可以平铺,拉伸,或对齐。您可以BitmapDrawable
从文件路径、输入流、通过 XML 膨胀或从Bitmap
对象创建 。
回答by Manoj
Try this it converts a Bitmap
type image to Drawable
试试这个它将Bitmap
类型图像转换为Drawable
Drawable d = new BitmapDrawable(getResources(), bitmap);
回答by Zulaxia
Having seen a large amount of issues with bitmaps incorrectly scaling when converted to a BitmapDrawable
, the general way to convert should be:
在转换为 a 时BitmapDrawable
,看到大量位图缩放不正确的问题,转换的一般方法应该是:
Drawable d = new BitmapDrawable(getResources(), bitmap);
Without the Resources reference
, the bitmap
may not render properly, even when scaled correctly. There are numerous questions on here which would be solved simply by using this method rather than a straight call with only the bitmap
argument.
如果没有Resources reference
,则bitmap
可能无法正常正确缩放时渲染,甚至。这里有许多问题可以通过使用此方法而不是仅使用bitmap
参数的直接调用来解决。
回答by Cristiana Chavez
Offical Bitmapdrawable documentation
官方 Bitmapdrawable文档
This is sample on how to convert bitmap to drawable
Bitmap bitmap;
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(drawable);
回答by Samuel Ivan
I used with context
我与上下文一起使用
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
回答by Pir Fahim Shah
If you have a bitmap image and you want to use it in drawable, like
如果你有一个位图图像并且你想在 drawable 中使用它,比如
Bitmap contact_pic; //a picture to show in drawable
drawable = new BitmapDrawable(contact_pic);
回答by Joolah
Just do this:
只需这样做:
private void setImg(ImageView mImageView, Bitmap bitmap) {
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
mImageView.setDrawable(mDrawable);
}
回答by Sanjayrajsinh
1) bitmap to Drawable :
1) 位图到 Drawable :
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
// mImageView.setDrawable(mDrawable);
2) drawable to Bitmap :
2)可绘制到位图:
Bitmap mIcon = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_resource);
// mImageView.setImageBitmap(mIcon);
回答by SolidSnake
here's another one:
这是另一个:
Drawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
回答by amosu dona
covert bit map to drawable in sketchware app using code
使用代码将位图隐藏到可在 Sketchware 应用程序中绘制
android.graphics.drawable.BitmapDrawable d = new android.graphics.drawable.BitmapDrawable(getResources(), bitmap);