且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Android O 预览版中将 AdaptiveIconDrawable 转换为位图

更新时间:2023-01-23 10:25:06

这是一个更通用的解决方案:

This is much more universal solution:

@NonNull
private Bitmap getBitmapFromDrawable(@NonNull Drawable drawable) {
    final Bitmap bmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bmp);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bmp;
}

如果您不想使用自己的形状,它也会在系统定义的形状中渲染背景.

It will also render background in the system defined shape, if you don't want to use own.

您可以在任何 Drawable 上使用它,也可以在 VectorDrawable 上使用它.它也适用于 BitmapDrawable,只是 getBitmap() 会更节省内存.

You can use it on any Drawable, so also for VectorDrawable. It will works also on BitmapDrawable, just getBitmap() will be more memory efficient.