【问题标题】:How to make a round icon for shortcut如何为快捷方式制作圆形图标
【发布时间】:2019-12-19 19:25:14
【问题描述】:

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

如您所见,动态快捷方式图标在中心有一个方形图像,但我需要它占据图标的所有空间,因此不会有白色背景。 代码:

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();

【问题讨论】:

    标签: android android-static-shortcuts android-dynamic-shortcuts


    【解决方案1】:

    添加到正在加载 xml 文件中图像的 imageView

    android:scaleType="centerCrop"
    

    【讨论】:

    【解决方案2】:

    我想我找到了一种可能的解决方案,那就是使用自适应图标。对我来说这看起来有点奇怪,但是只要它有效。 我使用了 AdaptiveIconDrawable,这里是如何做到的:

    1. 我们需要将快捷图标的 Bitmap 转换为 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)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多