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

查看:867
本文介绍了在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天全站免登陆