/** * Scales the image uniformly (maintaining the image's aspect ratio) so that one of the dimensions of the image * will be equal to the given dimension and the other will be less than the given dimension. */ publicclassFitCenterextendsBitmapTransformation { @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { return TransformationUtils.fitCenter(toTransform, pool, outWidth, outHeight); } }
publicfinalclassTransformationUtils { /** * An expensive operation to resize the given Bitmap down so that it fits within the given dimensions maintain * the original proportions. * * @param toFit The Bitmap to shrink. * @param pool The BitmapPool to try to reuse a bitmap from. * @param width The width in pixels the final image will fit within. * @param height The height in pixels the final image will fit within. * @return A new Bitmap shrunk to fit within the given dimensions, or toFit if toFit's width or height matches the * given dimensions and toFit fits within the given dimensions */ publicstatic Bitmap fitCenter(Bitmap toFit, BitmapPool pool, int width, int height) { if (toFit.getWidth() == width && toFit.getHeight() == height) { return toFit; } finalfloatwidthPercentage= width / (float) toFit.getWidth(); finalfloatheightPercentage= height / (float) toFit.getHeight(); finalfloatminPercentage= Math.min(widthPercentage, heightPercentage);
// take the floor of the target width/height, not round. If the matrix // passed into drawBitmap rounds differently, we want to slightly // overdraw, not underdraw, to avoid artifacts from bitmap reuse. finalinttargetWidth= (int) (minPercentage * toFit.getWidth()); finalinttargetHeight= (int) (minPercentage * toFit.getHeight());
Bitmap.Configconfig= getSafeConfig(toFit); BitmaptoReuse= pool.get(targetWidth, targetHeight, config); if (toReuse == null) { toReuse = Bitmap.createBitmap(targetWidth, targetHeight, config); } // We don't add or remove alpha, so keep the alpha setting of the Bitmap we were given. TransformationUtils.setAlpha(toFit, toReuse);
/** * Transform resources with the given {@link Transformation}s. Replaces any existing transformation or * transformations. * * @param transformations the transformations to apply in order. * @return This request builder. */ public GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> transform( Transformation<ResourceType>... transformations) { isTransformationSet = true; if (transformations.length == 1) { transformation = transformations[0]; } else { transformation = newMultiTransformation<ResourceType>(transformations); }
returnthis; }
/** * Removes the current {@link com.bumptech.glide.load.Transformation}. * * @return This request builder. */ @SuppressWarnings("unchecked") public GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> dontTransform() { Transformation<ResourceType> transformation = UnitTransformation.get(); return transform(transformation); }
public Target<TranscodeType> into(ImageView view) { Util.assertMainThread(); if (view == null) { thrownewIllegalArgumentException("You must pass in a non null View"); }
if (!isTransformationSet && view.getScaleType() != null) { switch (view.getScaleType()) { case CENTER_CROP: applyCenterCrop(); break; case FIT_CENTER: case FIT_START: case FIT_END: applyFitCenter(); break; //$CASES-OMITTED$ default: // Do nothing. } }