Docs
API Docs

Transform

Transform tool

Our transform tool unifies cropping, flipping and rotation operations in one feature. The PhotoEditor SDK holds various preset crop ratios (e.g. 16:9) that can easily be complemented by any crop ratio you deem necessary.

The backend settings are implemented in the TransformSettings class and displayed using the TransformToolPanel. If you want to customize the appearance of this tool, take a look at the styling section.

As an example, you could create the following configuration:

// Obtain the asset config from you settingsList
AssetConfig assetConfig = settingsList.getConfig();

// Clear defaults and add aspect assets to the backend
assetConfig.getAssetMap(CropAspectAsset.class).clear().add(
  CropAspectAsset.FREE_CROP,
  new CropAspectAsset("my_crop_1_1",1, 1, false),
  new CropAspectAsset("my_crop_16_9",16, 9, false),
  new CropAspectAsset("my_crop_9_16",9, 16, false),
  new CropAspectAsset("my_crop_4_3",4, 3, false),
  new CropAspectAsset("my_crop_3_4",3, 4, false),
  new CropAspectAsset("my_crop_3_2",3, 2, false),
  new CropAspectAsset("my_crop_2_3",2, 3, false)
);

// Obtain the ui config from you settingsList
UiConfigAspect uiConfigAspect = settingsList.getSettingsModel(UiConfigAspect.class);

// Add aspect items to UI
uiConfigAspect.setAspectList(
  new CropResetItem(),
  new CropAspectItem("my_crop_free", R.string.pesdk_transform_button_freeCrop),
  new CropAspectItem("my_crop_1_1", R.string.pesdk_transform_button_squareCrop),
  new CropAspectItem("my_crop_16_9"),
  new CropAspectItem("my_crop_9_16"),
  new CropAspectItem("my_crop_4_3"),
  new CropAspectItem("my_crop_3_4"),
  new CropAspectItem("my_crop_3_2"),
  new CropAspectItem("my_crop_2_3")
);
// Add aspect assets to backend
settingsList.config.apply {
    getAssetMap(CropAspectAsset::class.java).clear().add(
      CropAspectAsset.FREE_CROP,
      CropAspectAsset("my_crop_1_1", 1, 1, false),
      CropAspectAsset("my_crop_16_9", 16, 9, false),
      CropAspectAsset("my_crop_9_16", 9, 16, false),
      CropAspectAsset("my_crop_4_3", 4, 3, false),
      CropAspectAsset("my_crop_3_4", 3, 4, false),
      CropAspectAsset("my_crop_3_2", 3, 2, false),
      CropAspectAsset("my_crop_2_3", 2, 3, false)
    )
}

// Add Items to UI
  settingsList.getSettingsModel(UiConfigAspect::class.java).apply {
    setAspectList(
      CropAspectItem("my_crop_free", R.string.pesdk_transform_button_freeCrop),
      CropAspectItem("my_crop_1_1"),
      CropAspectItem("my_crop_16_9"),
      CropAspectItem("my_crop_9_16"),
      CropAspectItem("my_crop_4_3"),
      CropAspectItem("my_crop_3_4"),
      CropAspectItem("my_crop_3_2"),
      CropAspectItem("my_crop_2_3")
    )
}

Forcing specific ratios

Per default the SDK chooses the best matching aspect for the input photo. This is in general the FREE_CROP. In order to force your users to crop their image to one of the available crop ratios, you need to remove the FREE_CROP option from the assets, to ensure that a user can’t remove the forced crop ratio afterward.

// Remove default Assets and add your own aspects
settingsList.getSettingsModel(AssetConfig.class).getAssetMap(CropAspectAsset.class)
  .clear().add(
  new CropAspectAsset("aspect_1_1", 1, 1, false),
  new CropAspectAsset("aspect_16_9", 16, 9, false),
  new CropAspectAsset("aspect_9_16", 9, 16, false)
);

// Add your own Asset to UI config and select the Force crop Mode.
settingsList.getSettingsModel(UiConfigAspect.class).setAspectList(
  new CropAspectItem("aspect_1_1"),
  new CropAspectItem("aspect_16_9"),
  new CropAspectItem("aspect_9_16")
).setForceCropMode(
  // This prevents that the Transform tool opens at start.
  UiConfigAspect.ForceCrop.SHOW_TOOL_NEVER
);
// Remove default Assets and add your own aspects
settingsList.getSettingsModel(AssetConfig::class.java).apply {
    getAssetMap(CropAspectAsset::class.java)
      .clear().add(
        CropAspectAsset("aspect_1_1", 1, 1, false),
        CropAspectAsset("aspect_16_9", 16, 9, false),
        CropAspectAsset("aspect_9_16", 9, 16, false)
      )
}

// Add your own Asset to UI config and select the Force crop Mode.
settingsList.getSettingsModel(UiConfigAspect::class.java).apply {
    setAspectList(
      CropAspectItem("aspect_1_1"),
      CropAspectItem("aspect_16_9"),
      CropAspectItem("aspect_9_16")
    )
    forceCropMode = UiConfigAspect.ForceCrop.SHOW_TOOL_NEVER
}

You can also force a specific aspect for portrait and landscape images. (In this case you do not need to removing the FREE_CROP option)

// Set force crop by asset id, make sure you have added that asset.
settingsList.getSettingsModel(TransformSettings.class)
  .setForceCrop("aspect_16_9", "aspect_9_16");
// Set force crop by asset id, make sure you have added that asset.
settingsList.getSettingsModel(TransformSettings::class.java).apply {
    setForceCrop("aspect_16_9", "aspect_9_16")
}