如何为快捷方式制作圆形图标

4

我正在尝试将Android快捷方式添加到应用程序中,包括动态快捷方式。它们的图标将从位图创建。目前看起来是这样的:

enter image description here

正如您所看到的,动态快捷图标中心有一个正方形图像,但我需要它占据整个图标的空间,这样就不会有白色背景。 代码:
Bitmap interlocutorAvatar = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_conference);
ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(context, peer.getId())
                        .setLongLabel("Dynamic shortcut")
                        .setShortLabel("Dynamic")
                        .setIcon(Icon.createWithBitmap(interlocutorAvatar))
                        .setIntent(new Intent(Intent.ACTION_VIEW).setClass(context, VCEngine.appInfo().getActivity(ActivitySwitcher.ActivityType.CHAT))
                                .putExtra(CustomIntent.EXTRA_PEER_ID, peer.getId())
                                .putExtra(CustomIntent.EXTRA_CHAT_ID, peer.getId()))
                        .build();
2个回答

2
我认为我已经找到了一种可能的解决方案,那就是使用自适应图标。虽然它看起来有点奇怪,但只要它能正常工作就可以了。 我使用了AdaptiveIconDrawable,以下是操作步骤:
  1. 我们需要将快捷方式图标的位图转换为BitmapDrawable。
  2. 我们创建一个AdaptiveIconDrawable并将BitmapDrawable传递给它。
  3. 然后我们创建另一个位图,并在其画布上绘制AdaptiveIconDrawable,从而将AdaptiveIconDrawable转换回位图(我猜是自适应位图?)
  4. 最后,我们使用Icon.createWithAdaptiveBitmap方法设置快捷方式图标。
将位图转换为自适应位图的代码如下:
@RequiresApi(api = Build.VERSION_CODES.O)
    public static Bitmap convertBitmapToAdaptive(Bitmap bitmap, Context context) {
        Drawable bitmapDrawable = new BitmapDrawable(context.getResources(), bitmap);
        AdaptiveIconDrawable drawableIcon = new AdaptiveIconDrawable(bitmapDrawable, bitmapDrawable);
        Bitmap result = Bitmap.createBitmap(drawableIcon.getIntrinsicWidth(), drawableIcon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        drawableIcon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawableIcon.draw(canvas);
        return result;
    }

那么您可以像这样设置快捷方式的图标:

setIcon(Icon.createWithAdaptiveBitmap(convertBitmapToAdaptive(yourBitmap, context)))

1
将正在加载图像的imageView添加到您的xml文件中。
android:scaleType="centerCrop"

1
你可以使用Picasso库来处理图像 https://github.com/square/picasso - daniel.jbatiz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接