【问题标题】:Creating a shortcut: how can I work with a drawable as Icon?创建快捷方式:如何将可绘制对象用作图标?
【发布时间】:2011-03-18 22:53:59
【问题描述】:

以下是我创建所选应用程序快捷方式的代码。我真的没有问题,应用程序运行良好。

问题是我可以使用我的应用程序中的资源创建快捷方式:

    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));

但我真的很想要一个自定义的可绘制对象。 (Drawable myDrawable=.....)

我该怎么办?

   ResolveInfo launchable=adapter.getItem(position);
   final Intent shortcutIntent = new Intent();
    ActivityInfo activity=launchable.activityInfo;
    ComponentName name=new ComponentName(activity.applicationInfo.packageName,activity.name);       
    shortcutIntent.setComponent(name);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    final Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    // Sets the custom shortcut's title
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, launchable.loadLabel(pm));
    // Set the custom shortcut icon
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));

    // add the shortcut
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(intent);
    finish();

非常感谢任何线索

【问题讨论】:

标签: android icons android-intent shortcut drawable


【解决方案1】:

终于找到了解决办法;使用 Intent.EXTRA_SHORTCUT_ICON_RESOURCE 是我的愚蠢:

这是正确的代码:

Drawable iconDrawable = (....); 
BitmapDrawable bd = (BitmapDrawable) iconDrawable;
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bd.getBitmap());

【讨论】:

    【解决方案2】:

    由于我只有 23 名声望,我无法发表评论,但我使用了您的代码,它很有用。 但是我的图片在快捷方式上没有正确缩放,所以我找到了两个可以完成解决方案的有趣主题:

    要获得正确大小的快捷方式图标: https://stackoverflow.com/a/19003905/3741926

    缩放你的drawable(使用你用前一个函数计算的大小) https://stackoverflow.com/a/10703256/3741926

    我得到了一个带有正确缩放图标的快捷方式,希望它会有所帮助

    【讨论】:

      猜你喜欢
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 2012-08-20
      相关资源
      最近更新 更多