How to convert a Drawable to a Bitmap?

前端 未结 20 2444
攒了一身酷
攒了一身酷 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:06

    BitmapFactory.decodeResource() automatically scales the bitmap, so your bitmap may turn out fuzzy. To prevent scaling, do this:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;
    Bitmap source = BitmapFactory.decodeResource(context.getResources(),
                                                 R.drawable.resource_name, options);
    

    or

    InputStream is = context.getResources().openRawResource(R.drawable.resource_name)
    bitmap = BitmapFactory.decodeStream(is);
    

提交回复
热议问题