Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adaptive Icon Scaling #181

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 113 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,117 @@
# Files and directories created by pub
.packages
# Miscellaneous
*.class
*.lock
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# Visual Studio Code related
.classpath
.project
.settings/
.vscode/

# Flutter repo-specific
/bin/cache/
/bin/internal/bootstrap.bat
/bin/internal/bootstrap.sh
/bin/mingit/
/dev/benchmarks/mega_gallery/
/dev/bots/.recipe_deps
/dev/bots/android_tools/
/dev/devicelab/ABresults*.json
/dev/docs/doc/
/dev/docs/flutter.docs.zip
/dev/docs/lib/
/dev/docs/pubspec.yaml
/dev/integration_tests/**/xcuserdata
/dev/integration_tests/**/Pods
/packages/flutter/coverage/
version
analysis_benchmark.json

# packages file containing multi-root paths
.packages.generated

# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
**/generated_plugin_registrant.dart
.packages
.pub-cache/
.pub/
.idea/
build/
# Remove the following pattern if you wish to check in your lock file
pubspec.lock
flutter_*.png
linked_*.ds
unlinked.ds
unlinked_spec.ds

# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java
**/android/key.properties
*.jks

# iOS/XCode related
**/ios/**/*.mode1v3
**/ios/**/*.mode2v3
**/ios/**/*.moved-aside
**/ios/**/*.pbxuser
**/ios/**/*.perspectivev3
**/ios/**/*sync/
**/ios/**/.sconsign.dblite
**/ios/**/.tags*
**/ios/**/.vagrant/
**/ios/**/DerivedData/
**/ios/**/Icon?
**/ios/**/Pods/
**/ios/**/.symlinks/
**/ios/**/profile
**/ios/**/xcuserdata
**/ios/.generated/
**/ios/Flutter/.last_build_id
**/ios/Flutter/App.framework
**/ios/Flutter/Flutter.framework
**/ios/Flutter/Flutter.podspec
**/ios/Flutter/Generated.xcconfig
**/ios/Flutter/app.flx
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/Flutter/flutter_export_environment.sh
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

# macOS
**/macos/Flutter/GeneratedPluginRegistrant.swift

# Coverage
coverage/

# Symbols
app.*.symbols

# Directory created by dartdoc
doc/api/
# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
!/dev/ci/**/Gemfile.lock
3 changes: 3 additions & 0 deletions example/android/local.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sdk.dir=C:\\Users\\yanco\\AppData\\Local\\Android\\Sdk
flutter.sdk=C:\\flutter
flutter.buildMode=debug
1 change: 1 addition & 0 deletions example/default_example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ flutter_icons:
ios: true # can specify file name here e.g. "My-Launcher-Icon"
adaptive_icon_background: "assets/images/christmas-background.png" # only available for Android 8.0 devices and above
adaptive_icon_foreground: "assets/images/icon-foreground-432x432.png" # only available for Android 8.0 devices and above
adaptive_icon_foreground_scale_factor: 1.0 # Test adaptive icon scaling

dev_dependencies:
flutter_test:
Expand Down
71 changes: 60 additions & 11 deletions lib/android.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import 'dart:io';
import 'dart:math';

import 'package:flutter_launcher_icons/constants.dart' as constants;
import 'package:flutter_launcher_icons/custom_exceptions.dart';
import 'package:flutter_launcher_icons/utils.dart';
import 'package:flutter_launcher_icons/xml_templates.dart' as xml_template;
import 'package:image/image.dart';
import 'package:flutter_launcher_icons/custom_exceptions.dart';
import 'package:flutter_launcher_icons/constants.dart' as constants;

class AndroidIconTemplate {
AndroidIconTemplate({required this.size, required this.directoryName});
Expand Down Expand Up @@ -68,24 +70,71 @@ bool isAndroidIconNameCorrectFormat(String iconName) {
return true;
}

/// Rescales image by creating a different sized canvas and copying image centered onto the new canvas
Image rescaleImage(Image image, double scaleFactor, {int fillColor = 0}) {
printStatus('Rescaling icon by $scaleFactor');
final int scaledDimension =
(max(image.width, image.height) * 1 / scaleFactor).floor();
if (image.width != image.height) {
printStatus(
'Foreground image is not square! Scaled canvas will be made square '
'to meet Adaptive Icon Specifications');
}
final Image scaledImage = Image(scaledDimension, scaledDimension);
scaledImage.fill(fillColor);
copyInto(
scaledImage,
image,
dstX: ((scaledDimension - image.width) / 2).floor(),
dstY: ((scaledDimension - image.height) / 2).floor(),
);
return scaledImage;
}

void createAdaptiveIcons(
Map<String, dynamic> flutterLauncherIconsConfig, String? flavor) {
printStatus('Creating adaptive icons Android');

// Retrieve the necessary Flutter Launcher Icons configuration from the pubspec.yaml file
// Retrieve the necessary Flutter Launcher Icons configuration from the yaml file
final String backgroundConfig =
flutterLauncherIconsConfig['adaptive_icon_background'];
final String foregroundImagePath =
flutterLauncherIconsConfig['adaptive_icon_foreground'];
final Image? foregroundImage = decodeImageFile(foregroundImagePath);
if (foregroundImage == null) {
return;
}
final Image? foregroundImage =
decodeImage(File(foregroundImagePath).readAsBytesSync());
final double? foregroundScaleFactor =
flutterLauncherIconsConfig['adaptive_icon_foreground_scale_factor'];
final String? foregroundScaleFillColor =
flutterLauncherIconsConfig['adaptive_icon_foreground_scale_fill_color'];

// Create adaptive icon foreground images
for (AndroidIconTemplate androidIcon in adaptiveForegroundIcons) {
overwriteExistingIcons(androidIcon, foregroundImage,
constants.androidAdaptiveForegroundFileName, flavor);
final bool rescale = foregroundScaleFactor != null &&
foregroundScaleFactor > 0 &&
foregroundImage != null;

// Scales the foreground image prior to converting to icon. This is mainly for scaling down to match adaptive icon spec
if (rescale) {
Image rescaledImage;

int _getColorFromHex(String hexColor) {
//Converts hex string to int
hexColor = hexColor.toUpperCase().replaceAll('#', '');
return int.parse(hexColor, radix: 16);
}

rescaledImage = rescaleImage(foregroundImage, foregroundScaleFactor,
fillColor: foregroundScaleFillColor != null &&
foregroundScaleFillColor.isNotEmpty
? _getColorFromHex(foregroundScaleFillColor)
: 0);

// Create adaptive icon foreground images
for (AndroidIconTemplate androidIcon in adaptiveForegroundIcons) {
overwriteExistingIcons(
androidIcon,
rescale ? rescaledImage : foregroundImage,
constants.androidAdaptiveForegroundFileName,
flavor);
}
}

// Create adaptive icon background
Expand Down
3 changes: 2 additions & 1 deletion lib/ios.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'dart:convert';
import 'dart:io';

import 'package:flutter_launcher_icons/constants.dart';
import 'package:flutter_launcher_icons/utils.dart';
import 'package:image/image.dart';
import 'package:flutter_launcher_icons/constants.dart';

/// File to handle the creation of icons for iOS platform
class IosIconTemplate {
Expand Down
6 changes: 3 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'dart:io';

import 'package:args/args.dart';
import 'package:path/path.dart' as path;
import 'package:yaml/yaml.dart';
import 'package:flutter_launcher_icons/android.dart' as android_launcher_icons;
import 'package:flutter_launcher_icons/ios.dart' as ios_launcher_icons;
import 'package:flutter_launcher_icons/constants.dart';
import 'package:flutter_launcher_icons/custom_exceptions.dart';
import 'package:flutter_launcher_icons/ios.dart' as ios_launcher_icons;
import 'package:path/path.dart' as path;
import 'package:yaml/yaml.dart';

const String fileOption = 'file';
const String helpFlag = 'help';
Expand Down