简体   繁体   English

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

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

I'm creating an app which gets a list of all the apps currently installed on your phone and I'm trying to get the app's square icon.我正在创建一个应用程序,它会获取当前安装在您手机上的所有应用程序的列表,并且我正在尝试获取该应用程序的方形图标。 If I do the following:如果我执行以下操作:

getPackageManager().getApplicationIcon(thePackageName);

which gets the icon but it's displayed depending on the style currently set on the device (round, squicle, etc).它获取图标,但它的显示取决于设备上当前设置的样式(圆形、squicle 等)。 If I change the icon shape on the phone it gets the icon with the new shape applied.如果我更改手机上的图标形状,它会获得应用了新形状的图标。 Is there any way of getting just the square icon?有没有办法只得到方形图标?

I've also been trying to use getDrawableForDensity() which you can use with the icon density and the theme which I thought could be what I was after but I still couldn't get it to work as expected.我也一直在尝试使用getDrawableForDensity() ,您可以将其与图标密度和我认为可能是我所追求的主题一起使用,但我仍然无法让它按预期工作。

I've found a solution although it might not be the best way to do it.我找到了一个解决方案,尽管它可能不是最好的方法。 I couldn't get the icon as a whole without the Adaptive mask so I've checked if the Drawable is an AdaptiveIconDrawable then I extract both the background & foreground and paint them both to the canvas to be returned.如果没有自适应蒙版,我无法获得整个图标,因此我检查了 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