在 Android O 预览版中将 AdaptiveIconDrawable 转换为位图 [英] Convert AdaptiveIconDrawable to Bitmap in Android O preview

查看:30
本文介绍了在 Android O 预览版中将 AdaptiveIconDrawable 转换为位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天发布了新的预览版,我开始将我的应用更新到这个版本.我的应用程序的一部分是列出带有关联图标的已安装应用程序.

With the new preview release yesterday I started to update my app to this version. Part of my app is to list installed applications with the associated icons.

Drawable drawable = mPackageManager.getApplicationIcon(packageName);

应用程序的这一部分仍然有效.时钟、计算器或消息等应用已经支持新版本的图标,因此它们会返回 AdaptiveIconDrawable.

This part of the app still works. Apps like the clock, calculator or messages already support the new version of icons, so they return a AdaptiveIconDrawable.

Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

这部分不再起作用.有没有其他方法可以将 AdaptiveIconDrawable 转换为 Bitmap?提前致谢.

This part doesn't work anymore. Is there any other way to convert AdaptiveIconDrawable to a Bitmap? Thanks in advance.

推荐答案

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

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.

这篇关于在 Android O 预览版中将 AdaptiveIconDrawable 转换为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆