欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【android】应用图标Drawable转Bitmap,适配安卓8.0

程序员文章站 2024-02-10 23:53:52
...

相信大家在加载应用图标的时候,经常会将得到的Drawable转成Bitmap,代码如下:

Drawable drawable = mPackageManager.getApplicationIcon(packageName);
return ((BitmapDrawable) drawable).getBitmap();

但是如果在Android 8.0上面就会崩溃,提示强转BitmapDrawable出错,具体的原因请参考上一篇文章,关于Android 8.0应用图标的改变,那我们该怎么去获取Bitmap呢,我参考了*上面的一篇文章,然后写成了如下工具类供大家参考:
AppIconHelper.java

package com.apusapps.booster.gm.launchpad.utils;

import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;

/**
 * Created by wangbifeng on 2018/3/14.
 * Description: This class is use to get app icon , it has also considered Android O
 */

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

AppIconHelperV26.java

package com.apusapps.booster.gm.launchpad.utils;

import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.AdaptiveIconDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.support.annotation.RequiresApi;

/**
 * Created by wangbifeng on 2018/3/14.
 * Description: This class is used to get app icon on Android O
 */

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

然后再代码中这样使用就可以啦

Bitmap bitmap = AppIconHelper.getAppIcon(mContext.getPackageManager(), packageName);

如果文章当中有任何不正确的地方,还请广大读者纠正,非常感谢!