static Bitmap applyCustomTransformations(List<Transformation> transformations, Bitmap result) { for (int i = 0, count = transformations.size(); i < count; i++) { Transformation transformation = transformations.get(i); Bitmap newResult = transformation.transform(result); if (newResult == null) { StringBuilder builder = new StringBuilder() // .append("Transformation ") .append(transformation.key()) .append(" returned null after ") .append(i) .append(" previous transformation(s).\n\nTransformation list:\n"); for (Transformation t : transformations) { builder.append(t.key()).append('\n'); } throw new NullPointerException(builder.toString()); } if (newResult == result && result.isRecycled()) { throw new IllegalStateException( "Transformation " + transformation.key() + " returned input Bitmap but recycled it."); } // If the transformation returned a new bitmap ensure they recycled the original. if (newResult != result && !result.isRecycled()) { throw new IllegalStateException( "Transformation " + transformation.key() + " mutated input Bitmap but failed to recycle the original."); } result = newResult; } return result; }
/** * Add a custom transformation to be applied to the image. * * <p>Custom transformations will always be run after the built-in transformations. */ public Builder transform(@NonNull Transformation transformation) { if (transformation == null) { throw new IllegalArgumentException("Transformation must not be null."); } if (transformation.key() == null) { throw new IllegalArgumentException("Transformation key must not be null."); } if (transformations == null) { transformations = new ArrayList<Transformation>(2); } transformations.add(transformation); return this; }
@Override public String toString() { final StringBuilder builder = new StringBuilder("Request{"); if (resourceId > 0) { builder.append(resourceId); } else { builder.append(uri); } if (transformations != null && !transformations.isEmpty()) { for (Transformation transformation : transformations) { builder.append(' ').append(transformation.key()); } } if (stableKey != null) { builder.append(" stableKey(").append(stableKey).append(')'); } if (targetWidth > 0) { builder.append(" resize(").append(targetWidth).append(',').append(targetHeight).append(')'); } if (centerCrop) { builder.append(" centerCrop"); } if (centerInside) { builder.append(" centerInside"); } if (rotationDegrees != 0) { builder.append(" rotation(").append(rotationDegrees); if (hasRotationPivot) { builder.append(" @ ").append(rotationPivotX).append(',').append(rotationPivotY); } builder.append(')'); } if (purgeable) { builder.append(" purgeable"); } if (config != null) { builder.append(' ').append(config); } builder.append('}'); return builder.toString(); }