如何将位图图像转换为可绘制?


当前回答

如果你有一个位图图像,你想用它来绘图,比如

Bitmap contact_pic;    //a picture to show in drawable
drawable = new BitmapDrawable(contact_pic); 

其他回答

我使用了上下文

//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);

如果你有一个位图图像,你想用它来绘图,比如

Bitmap contact_pic;    //a picture to show in drawable
drawable = new BitmapDrawable(contact_pic); 

这是另一个:

Drawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);

已经看到了大量的问题与位图不正确缩放时,转换为BitmapDrawable,一般的转换方式应该是:

Drawable d = new BitmapDrawable(getResources(), bitmap);

如果没有资源引用,位图可能无法正确呈现,即使缩放正确。这里有许多问题可以通过使用这个方法而不是直接调用位图参数来解决。

只要这样做:

private void setImg(ImageView mImageView, Bitmap bitmap) {

    Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
    mImageView.setDrawable(mDrawable);
}