在Android O预览中将AdaptiveIconDrawable转换为Bitmap

时间:2017-06-08 23:28:15

标签: android bitmap

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

Drawable drawable = mPackageManager.getApplicationIcon(packageName);

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

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

此部分不再有效。还有其他方法可以将AdaptiveIconDrawable转换为Bitmap吗?提前谢谢。

2 个答案:

答案 0 :(得分:22)

这是更通用的解决方案:

@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;
}

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

您可以在任何Drawable上使用它,也可以在VectorDrawable中使用它。 它也适用于BitmapDrawable,只是getBitmap()会更有效。

答案 1 :(得分:3)

我们可以通过以下方式实现,

AppIconHelper.java

public class AppIconHelper {

    public static Bitmap getAppIcon(PackageManager mPackageManager, String packageName) {

        if (Build.VERSION.SDK_INT >= 26) {
            return AppIconHelperV26.getAppIcon(mPackageManager, packageName);
        }

        try {
            Drawable drawable = mPackageManager.getApplicationIcon(packageName);

            return ((BitmapDrawable) drawable).getBitmap();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

对于支持Android O(> = 26)的设备,我们可以调用AppIconHelperV26类来获取AppIcon,在此,如果它的drawable是AdaptiveIconDrawable,我们正在执行以下步骤 1.获取前景和背景可绘制并创建为可绘图层 2.使用Canvas我们可以将Layer drawable转换为Bitmap

AppIconHelperV26.java

public class AppIconHelperV26 {

    @RequiresApi(api = Build.VERSION_CODES.O)
    public static Bitmap getAppIcon(PackageManager mPackageManager, String packageName) {

        try {
            Drawable drawable = mPackageManager.getApplicationIcon(packageName);

            if (drawable instanceof BitmapDrawable) {
                return ((BitmapDrawable) drawable).getBitmap();
            } else if (drawable instanceof AdaptiveIconDrawable) {
                Drawable backgroundDr = ((AdaptiveIconDrawable) drawable).getBackground();
                Drawable foregroundDr = ((AdaptiveIconDrawable) drawable).getForeground();

                Drawable[] drr = new Drawable[2];
                drr[0] = backgroundDr;
                drr[1] = foregroundDr;

                LayerDrawable layerDrawable = new LayerDrawable(drr);

                int width = layerDrawable.getIntrinsicWidth();
                int height = layerDrawable.getIntrinsicHeight();

                Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

                Canvas canvas = new Canvas(bitmap);

                layerDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
                layerDrawable.draw(canvas);

                return bitmap;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }
}

现在您可以调用AppIconHelper.getAppIcon来获取位图

AppListAdapter.java

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    //... your code here
    Bitmap appIcon = AppIconHelper.getAppIcon(packageManager, packageName);
    imageView.setImageBitmap(appIcon);

}