如何将Bitmap图像转换为Drawable?
听起来像你想用BitmapDrawable
从文档:
包含位图的Drawable,可以平铺,拉伸或对齐 . 您可以从文件路径,输入流,XML通胀或Bitmap对象创建BitmapDrawable .
如果你有一个位图图像,你想在drawable中使用它,比如
Bitmap contact_pic; //a picture to show in drawable drawable = new BitmapDrawable(contact_pic);
这是另一个:
Drawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
官方Bitmapdrawable documentation
这是关于如何转换bitmap to drawable的示例
Bitmap bitmap; //Convert bitmap to drawable Drawable drawable = new BitmapDrawable(getResources(), bitmap); imageView.setImageDrawable(drawable);
试试这个它将 Bitmap 型图像转换为 Drawable
Bitmap
Drawable
Drawable d = new BitmapDrawable(getResources(), bitmap);
我用上下文
//Convert bitmap to drawable Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
这样做:
private void setImg(ImageView mImageView, Bitmap bitmap) { Drawable mDrawable = new BitmapDrawable(getResources(), bitmap); mImageView.setDrawable(mDrawable); }
看到转换为 BitmapDrawable 时位图错误缩放的大量问题,转换的一般方法应该是:
BitmapDrawable
如果没有 Resources reference , bitmap 可能无法正确渲染,即使缩放正确也是如此 . 这里有很多问题,只需使用这种方法就可以解决,而不是仅使用 bitmap 参数的直接调用 .
Resources reference
bitmap
8 回答
听起来像你想用BitmapDrawable
从文档:
如果你有一个位图图像,你想在drawable中使用它,比如
这是另一个:
官方Bitmapdrawable documentation
这是关于如何转换bitmap to drawable的示例
试试这个它将
Bitmap
型图像转换为Drawable
我用上下文
这样做:
看到转换为
BitmapDrawable
时位图错误缩放的大量问题,转换的一般方法应该是:如果没有
Resources reference
,bitmap
可能无法正确渲染,即使缩放正确也是如此 . 这里有很多问题,只需使用这种方法就可以解决,而不是仅使用bitmap
参数的直接调用 .