小智 807

试试这个它将Bitmap类型图像转换为Drawable

Drawable d = new BitmapDrawable(getResources(), bitmap);
Run Code Online (Sandbox Code Playgroud)


Gra*_*can 258

听起来像你想要使用 BitmapDrawable

从文档:

一个Drawable包装位图,可以平铺,拉伸或对齐.您可以BitmapDrawable通过文件路径,输入流,XML通胀或Bitmap 对象创建.

  • 无用的帖子,无用的评论.如果你添加一些代码会更好 (76认同)
  • @Deprecated使用BitmapDrawable(参考资料,位图)确保drawable正确设置其目标密度. (21认同)

Zul*_*xia 144

看到转换为a时位图错误缩放的大量问题,转换BitmapDrawable的一般方法应该是:

Drawable d = new BitmapDrawable(getResources(), bitmap);
Run Code Online (Sandbox Code Playgroud)

如果没有Resources reference,则bitmap可能无法正常正确缩放时渲染,甚至.这里有很多问题,只需使用这种方法而不是仅使用bitmap参数的直接调用即可解决.

  • 至少要评论为什么你要投票.这是一个非常有效的答案,并带来额外的信息来解决所提供的其他答案可能出现的问题.直接从位图制作的Drawable通常具有缩放错误,而没有getResources()引用. (12认同)
  • 考虑到@Manoj中的一个被弃用,这是一个更准确的答案. (4认同)

Cri*_*vez 35

官方Bitmapdrawable 文档

这是关于如何将位图转换为drawable的示例

Bitmap bitmap;  
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(drawable);
Run Code Online (Sandbox Code Playgroud)

  • 你可以投票而不是写同样的答案. (4认同)

Sam*_*van 31

我用上下文

//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
Run Code Online (Sandbox Code Playgroud)

  • 答案重复. (3认同)

Pir*_*hah 20

如果你有一个位图图像,你想在drawable中使用它,比如

Bitmap contact_pic;    //a picture to show in drawable
drawable = new BitmapDrawable(contact_pic); 
Run Code Online (Sandbox Code Playgroud)

  • 现在已经弃用了.现在使用BitmapDrawable(Resources,Bitmap)构造函数. (20认同)
  • 这对你有好处,但是当Google最终杀死这个构造函数并且你必须重写所有内容时,它们无济于事. (2认同)

Joo*_*lah 11

这样做:

private void setImg(ImageView mImageView, Bitmap bitmap) {

    Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
    mImageView.setDrawable(mDrawable);
}
Run Code Online (Sandbox Code Playgroud)


San*_*inh 10

1) 位图到 Drawable :

Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
// mImageView.setDrawable(mDrawable);
Run Code Online (Sandbox Code Playgroud)

2)可绘制到位图:

Bitmap mIcon = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_resource);
// mImageView.setImageBitmap(mIcon);
Run Code Online (Sandbox Code Playgroud)