generateAndroidIcon method

void generateAndroidIcon(
  1. Uint8List imageBytes,
  2. Directory targetDir,
  3. Color? backgroundColor
)

產生android app icon的圖標 imageBytes - 原本的圖標bytes targetDir - 目標資料夾: 通常指向到 android/app/src/main/res/ 檔案名稱固定為ic_launcher.png 產生的size列表 mipmap-mdpi => 48x48 mipmap-hdpi => 72x72 mipmap-xhdpi => 96x96 mipmap-xxhdpi => 144x144 mipmap-xxxhdpi => 192x192

Implementation

void generateAndroidIcon(
  Uint8List imageBytes,
  Directory targetDir,
  Color? backgroundColor,
) {
  final sizeList = [
    AndroidIconSize(size: 48, name: 'mipmap-mdpi'),
    AndroidIconSize(size: 72, name: 'mipmap-hdpi'),
    AndroidIconSize(size: 96, name: 'mipmap-xhdpi'),
    AndroidIconSize(size: 144, name: 'mipmap-xxhdpi'),
    AndroidIconSize(size: 192, name: 'mipmap-xxxhdpi'),
  ];

  for (var size in sizeList) {
    final newImageBytes = MxImageUtil.resizeImage(
      imageBytes,
      size.size,
      size.size,
      removeAlpha: false,
      backgroundColor: backgroundColor,
    );

    // 先創建資料夾, 若有需要
    Directory('${targetDir.path}/${size.name}').createSync(recursive: true);

    // 把圖片寫入檔案
    File('${targetDir.path}/${size.name}/ic_launcher.png')
        .writeAsBytesSync(newImageBytes);
  }
}