How to convert a Bitmap to Drawable in android?

2019-01-03 19:41发布

How can I convert a Bitmap image to Drawable ?

8条回答
小情绪 Triste *
2楼-- · 2019-01-03 20:11

I used with context

//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
查看更多
Ridiculous、
3楼-- · 2019-01-03 20:13

Just do this:

private void setImg(ImageView mImageView, Bitmap bitmap) {

    Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
    mImageView.setDrawable(mDrawable);
}
查看更多
甜甜的少女心
4楼-- · 2019-01-03 20:19

Sounds like you want to use BitmapDrawable

From the documentation:

A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You can create a BitmapDrawable from a file path, an input stream, through XML inflation, or from a Bitmap object.

查看更多
Emotional °昔
5楼-- · 2019-01-03 20:19

Having seen a large amount of issues with bitmaps incorrectly scaling when converted to a BitmapDrawable, the general way to convert should be:

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.

查看更多
▲ chillily
6楼-- · 2019-01-03 20:24

If you have a bitmap image and you want to use it in drawable, like

Bitmap contact_pic;    //a picture to show in drawable
drawable = new BitmapDrawable(contact_pic); 
查看更多
狗以群分
7楼-- · 2019-01-03 20:25

Try this it converts a Bitmap type image to Drawable

Drawable d = new BitmapDrawable(getResources(), bitmap);
查看更多
登录 后发表回答