繁体   English   中英

从 Android 上安装的应用程序中获取非自适应图标

[英]Getting non-adaptive icons from installed apps on Android

我正在创建一个应用程序,它会获取当前安装在您手机上的所有应用程序的列表,并且我正在尝试获取该应用程序的方形图标。 如果我执行以下操作:

getPackageManager().getApplicationIcon(thePackageName);

它获取图标,但它的显示取决于设备上当前设置的样式(圆形、squicle 等)。 如果我更改手机上的图标形状,它会获得应用了新形状的图标。 有没有办法只得到方形图标?

我也一直在尝试使用getDrawableForDensity() ,您可以将其与图标密度和我认为可能是我所追求的主题一起使用,但我仍然无法让它按预期工作。

我找到了一个解决方案,尽管它可能不是最好的方法。 如果没有自适应蒙版,我无法获得整个图标,因此我检查了 Drawable 是否是 AdaptiveIconDrawable,然后我提取背景和前景并将它们都绘制到 canvas 以返回。

    public Bitmap drawableToBitmap (Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        } else if ((Build.VERSION.SDK_INT >= 26)
                && (drawable instanceof AdaptiveIconDrawable)) {
            AdaptiveIconDrawable icon = ((AdaptiveIconDrawable)drawable);
            Drawable bg = icon.getBackground();
            Drawable fg = icon.getForeground();
            int w = icon.getIntrinsicWidth();
            int h = icon.getIntrinsicHeight();
            Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(result);
            icon.setBounds(0, 0, w, h);
            bg.draw(canvas);
            if (fg instanceof Drawable) {
                fg.draw(canvas);
            }
            return result;
        }
        float density = getBaseContext().getResources().getDisplayMetrics().density;
        int defaultWidth = (int)(48* density);
        int defaultHeight = (int)(48* density);
        return Bitmap.createBitmap(defaultWidth, defaultHeight, Bitmap.Config.ARGB_8888);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM