How to convert a Drawable to a Bitmap?

前端 未结 20 2438
攒了一身酷
攒了一身酷 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: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);
    

提交回复
热议问题