Class DownsampleStrategy
- java.lang.Object
-
- com.bumptech.glide.load.resource.bitmap.DownsampleStrategy
-
public abstract class DownsampleStrategy extends java.lang.ObjectIndicates the algorithm to use when downsampling images.DownsampleStrategydoes not provide any guarantees about output sizes. Behavior will differ depending on theResourceDecoderusing the strategy and the version of Android the code runs on. UseDownsampleStrategyas an optimization to improve memory efficiency only. If you need a particular size or shape output, use anTransformationeither instead or in addition to aDownsampleStrategy.Some differences between versions of Android and
ResourceDecoders are listed below, but the list is not comprehensive becauseDownsampleStrategyonly controls its output scale value, not how that output value is used.On some versions of Android, precise scaling is not possible. In those cases, the strategies can only pick between downsampling to between 1x the requested size and 2x the requested size and between 0.5x the requested size and 1x the requested size because only power of two downsampling is supported. To preserve the potential for a
Transformationto scale precisely without a loss in quality, all butAT_MOSTwill prefer to downsample to between 1x and 2x the requested size.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDownsampleStrategy.SampleSizeRoundingIndicates whether to prefer to prefer downsampling or scaling to prefer lower memory usage or higher quality.
-
Field Summary
Fields Modifier and Type Field Description static DownsampleStrategyAT_LEASTDownsamples so the image's smallest dimension is between the given dimensions and 2x the given dimensions, with no size restrictions on the image's largest dimension.static DownsampleStrategyAT_MOSTDownsamples so the image's largest dimension is between 1/2 the given dimensions and the given dimensions, with no restrictions on the image's smallest dimension.static DownsampleStrategyCENTER_INSIDEIdentical toFIT_CENTER, but never upscales.static DownsampleStrategyCENTER_OUTSIDEScales, maintaining the original aspect ratio, so that one of the image's dimensions is exactly equal to the requested size and the other dimension is greater than or equal to the requested size.static DownsampleStrategyDEFAULTDefault strategy, currentlyCENTER_OUTSIDE.static DownsampleStrategyFIT_CENTERScales, maintaining the original aspect ratio, so that one of the image's dimensions is exactly equal to the requested size and the other dimension is less than or equal to the requested size.static DownsampleStrategyNONEPerforms no downsampling or scaling.static Option<DownsampleStrategy>OPTIONIndicates theDownsampleStrategyoption that will be used to calculate the sample size to use to downsample an image given the original and target dimensions of the image.
-
Constructor Summary
Constructors Constructor Description DownsampleStrategy()
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description abstract DownsampleStrategy.SampleSizeRoundinggetSampleSizeRounding(int sourceWidth, int sourceHeight, int requestedWidth, int requestedHeight)Returns a non-nullDownsampleStrategy.SampleSizeRoundingto use to resolve rounding errors and conflicts between scaling for the width and the height of the image.abstract floatgetScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth, int requestedHeight)Returns a float (0, +infinity) indicating a scale factor to apply to the source width and height when displayed in the requested width and height.
-
-
-
Field Detail
-
AT_LEAST
public static final DownsampleStrategy AT_LEAST
Downsamples so the image's smallest dimension is between the given dimensions and 2x the given dimensions, with no size restrictions on the image's largest dimension.Does not upscale if the requested dimensions are larger than the original dimensions.
-
AT_MOST
public static final DownsampleStrategy AT_MOST
Downsamples so the image's largest dimension is between 1/2 the given dimensions and the given dimensions, with no restrictions on the image's smallest dimension.Does not upscale if the requested dimensions are larger than the original dimensions.
-
FIT_CENTER
public static final DownsampleStrategy FIT_CENTER
Scales, maintaining the original aspect ratio, so that one of the image's dimensions is exactly equal to the requested size and the other dimension is less than or equal to the requested size.This method will upscale if the requested width and height are greater than the source width and height. To avoid upscaling, use
AT_LEAST,AT_MOSTorCENTER_INSIDE.On pre-KitKat devices,
FIT_CENTERwill downsample by a power of two only so that one of the image's dimensions is greater than or equal to the requested size. No guarantees are made about the second dimensions. This is NOT the same asAT_LEASTbecause only one dimension, not both, are greater than or equal to the requested dimensions, the other may be smaller.
-
CENTER_INSIDE
public static final DownsampleStrategy CENTER_INSIDE
Identical toFIT_CENTER, but never upscales.
-
CENTER_OUTSIDE
public static final DownsampleStrategy CENTER_OUTSIDE
Scales, maintaining the original aspect ratio, so that one of the image's dimensions is exactly equal to the requested size and the other dimension is greater than or equal to the requested size.This method will upscale if the requested width and height are greater than the source width and height. To avoid upscaling, use
AT_LEAST,AT_MOST, orCENTER_INSIDE.On pre-KitKat devices,
Downsamplertreats this as equivalent toAT_LEASTbecause only power of two downsampling can be used.
-
NONE
public static final DownsampleStrategy NONE
Performs no downsampling or scaling.
-
DEFAULT
public static final DownsampleStrategy DEFAULT
Default strategy, currentlyCENTER_OUTSIDE.
-
OPTION
public static final Option<DownsampleStrategy> OPTION
Indicates theDownsampleStrategyoption that will be used to calculate the sample size to use to downsample an image given the original and target dimensions of the image.
-
-
Method Detail
-
getScaleFactor
public abstract float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth, int requestedHeight)Returns a float (0, +infinity) indicating a scale factor to apply to the source width and height when displayed in the requested width and height.The returned scale factor will be split into a power of two sample size applied via
BitmapFactory.Options.inSampleSizeand a float scale factor applied after downsampling viaBitmapFactory.Options.inTargetDensityandBitmapFactory.Options.inDensity. Because of rounding errors the scale factor may not be applied precisely.The float scaling factor will only be applied on KitKat+. Prior to KitKat, only the power of two downsampling will be applied.
- Parameters:
sourceWidth- The width in pixels of the image to be downsampled.sourceHeight- The height in pixels of the image to be downsampled.requestedWidth- The width in pixels of the view/target the image will be displayed in.requestedHeight- The height in pixels of the view/target the image will be displayed in.
-
getSampleSizeRounding
public abstract DownsampleStrategy.SampleSizeRounding getSampleSizeRounding(int sourceWidth, int sourceHeight, int requestedWidth, int requestedHeight)
Returns a non-nullDownsampleStrategy.SampleSizeRoundingto use to resolve rounding errors and conflicts between scaling for the width and the height of the image.- Parameters:
sourceWidth- The width in pixels of the image to be downsampled.sourceHeight- The height in pixels of the image to be downsampled.requestedWidth- The width in pixels of the view/target the image will be displayed in.requestedHeight- The height in pixels of the view/target the image will be displayed in.
-
-