首页 文章

如何在Android中将Bitmap转换为Drawable?

提问于
浏览
400

如何将Bitmap图像转换为Drawable?

8 回答

  • 0

    听起来像你想用BitmapDrawable

    从文档:

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

  • 10

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

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

    这是另一个:

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

    官方Bitmapdrawable documentation

    这是关于如何转换bitmap to drawable的示例

    Bitmap bitmap;  
    //Convert bitmap to drawable
    Drawable drawable = new BitmapDrawable(getResources(), bitmap);
    imageView.setImageDrawable(drawable);
    
  • 778

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

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

    我用上下文

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

    这样做:

    private void setImg(ImageView mImageView, Bitmap bitmap) {
    
        Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
        mImageView.setDrawable(mDrawable);
    }
    
  • 20

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

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

    如果没有 Resources referencebitmap 可能无法正确渲染,即使缩放正确也是如此 . 这里有很多问题,只需使用这种方法就可以解决,而不是仅使用 bitmap 参数的直接调用 .

相关问题