您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

讓同一支 App 在桌面顯示多個圖示做視覺對比

編輯:iOS 開發綜合

設計 App 圖示時有個常見需求:新版與舊版想同時放在桌面上對照看效果,在意圖上又希望兩顆都能開啟同一支 App。

為什麼一個 App 只有一個圖示

App 本身的圖示在 AndroidManifest.xml 的 <application> 標籤中透過 android:icon 屬性設定,一個 App 就是一個。但 Android 的 Intent 機制提供了 shortcut 能力:可以把「啟動某個意圖的方式」連同自訂圖片存到桌面。微信裡「添加到桌面」的功能,背後大概就是這套 shortcut intent。

於是只要送出兩次 INSTALL_SHORTCUT 廣播、各自帶不同圖片與名稱,桌面上就會出現兩顆圖示,點下去開的都是同一支 App。

建立捷徑的程式碼

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent shortcutIntent = new Intent(MainActivity.this, MainActivity.this.getClass());

    final Intent icon1 = new Intent();
    icon1.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    icon1.putExtra(Intent.EXTRA_SHORTCUT_NAME, "原圖標");
    icon1.putExtra(Intent.EXTRA_SHORTCUT_ICON,
                   BitmapFactory.decodeResource(getResources(), R.drawable.icon1));
    icon1.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(icon1);

    final Intent icon2 = new Intent();
    icon2.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    icon2.putExtra(Intent.EXTRA_SHORTCUT_NAME, "新圖標");
    icon2.putExtra(Intent.EXTRA_SHORTCUT_ICON,
                   BitmapFactory.decodeResource(getResources(), R.drawable.icon2));
    icon2.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(icon2);
}

權限宣告

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

實測結果與注意事項

以微信 5.0 與 5.2 的圖示對照來看,5.2 的版本明顯更扁平,整體色調也偏暗一些。

要提醒的是,這個做法只適合「內部視覺比對」這類一次性用途,不該出現在正式版的功能裡。原因有幾個:Android 8.0 之後 launcher 對捷徑建立了需使用者確認的機制, INSTALL_SHORTCUT 廣播不會再無條件生效;桌面上殘留的測試捷徑需要使用者手動刪除;同一支 App 反覆送出多個捷徑也會讓使用者困惑。真要比較設計稿,用 Android Studio 的 Adaptive Icon 預覽或直接把 PNG 放到桌面壁紙上看,會乾淨得多。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved