How to convert a Drawable to a Bitmap?

前端 未结 20 2479
攒了一身酷
攒了一身酷 2020-11-21 22:46

I would like to set a certain Drawable as the device\'s wallpaper, but all wallpaper functions accept Bitmaps only. I cannot use WallpaperMan

相关标签:
20条回答
  • 2020-11-21 23:11
    public static Bitmap drawableToBitmap (Drawable drawable) {
        Bitmap bitmap = null;
    
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if(bitmapDrawable.getBitmap() != null) {
                return bitmapDrawable.getBitmap();
            }
        }
    
        if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        }
    
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }
    
    0 讨论(0)
  • 2020-11-21 23:12

    This converts a BitmapDrawable to a Bitmap.

    Drawable d = ImagesArrayList.get(0);  
    Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
    
    0 讨论(0)
  • 2020-11-21 23:13

    1) drawable to Bitmap :

    Bitmap mIcon = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon);
    // mImageView.setImageBitmap(mIcon);
    

    2) bitmap to Drawable :

    Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
    // mImageView.setDrawable(mDrawable);
    
    0 讨论(0)
  • 2020-11-21 23:18

    So after looking (and using) of the other answers, seems they all handling ColorDrawable and PaintDrawable badly. (Especially on lollipop) seemed that Shaders were tweaked so solid blocks of colors were not handled correctly.

    I am using the following code now:

    public static Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }
    
        // We ask for the bounds if they have been set as they would be most
        // correct, then we check we are  > 0
        final int width = !drawable.getBounds().isEmpty() ?
                drawable.getBounds().width() : drawable.getIntrinsicWidth();
    
        final int height = !drawable.getBounds().isEmpty() ?
                drawable.getBounds().height() : drawable.getIntrinsicHeight();
    
        // Now we check we are > 0
        final Bitmap bitmap = Bitmap.createBitmap(width <= 0 ? 1 : width, height <= 0 ? 1 : height,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
    
        return bitmap;
    }
    

    Unlike the others, if you call setBounds on the Drawable before asking to turn it into a bitmap, it will draw the bitmap at the correct size!

    0 讨论(0)
  • 2020-11-21 23:19

    ImageWorker Library can convert bitmap to drawable or base64 and vice versa.

    val bitmap: Bitmap? = ImageWorker.convert().drawableToBitmap(sourceDrawable)
    

    Implementation

    In Project Level Gradle

    allprojects {
            repositories {
                ...
                maven { url 'https://jitpack.io' }
            }
        }
    

    In Application Level Gradle

    dependencies {
                implementation 'com.github.1AboveAll:ImageWorker:0.51'
        }
    

    You can also store and retrieve bitmaps/drawables/base64 images from external.

    Check here. https://github.com/1AboveAll/ImageWorker/edit/master/README.md

    0 讨论(0)
  • 2020-11-21 23:25

    Maybe this will help someone...

    From PictureDrawable to Bitmap, use:

    private Bitmap pictureDrawableToBitmap(PictureDrawable pictureDrawable){ 
        Bitmap bmp = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888); 
        Canvas canvas = new Canvas(bmp); 
        canvas.drawPicture(pictureDrawable.getPicture()); 
        return bmp; 
    }
    

    ... implemented as such:

    Bitmap bmp = pictureDrawableToBitmap((PictureDrawable) drawable);
    
    0 讨论(0)
提交回复
热议问题