如何将 Drawable 转换为位图?

新手上路,请多包涵

我想设置某个 Drawable 作为设备的壁纸,但所有壁纸功能只接受 Bitmap s。我不能使用 WallpaperManager 因为我是 2.1 之前的版本。

另外,我的drawables是从网上下载的,不驻留在 R.drawable 中。

原文由 Rob 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 628
2 个回答

这会将 BitmapDrawable 转换为 Bitmap。

 Drawable d = ImagesArrayList.get(0);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();

原文由 Rob 发布,翻译遵循 CC BY-SA 3.0 许可协议

Kotlin 中,最简单的方法是:

 Drawable.toBitmap(width: Int, height: Int, config: Bitmap.Config?): Bitmap

像这样:

 val bitmapResult = yourDrawable.toBitmap(1,1,null)

其中,只需要一个可绘制变量,无资源,无上下文,无 id

原文由 Mori 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题