Class DownsampleStrategy
DownsampleStrategy does not provide any guarantees about output sizes. Behavior will
differ depending on the ResourceDecoder using the strategy and
the version of Android the code runs on. Use DownsampleStrategy as an optimization to
improve memory efficiency only. If you need a particular size or shape output, use an Transformation either instead or in addition to a
DownsampleStrategy.
Some differences between versions of Android and ResourceDecoders are listed below, but the list is not comprehensive
because DownsampleStrategy only 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 Transformation to
scale precisely without a loss in quality, all but AT_MOST will prefer to downsample to
between 1x and 2x the requested size.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumIndicates whether to prefer to prefer downsampling or scaling to prefer lower memory usage or higher quality. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DownsampleStrategyDownsamples 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 final DownsampleStrategyDownsamples 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 final DownsampleStrategyIdentical toFIT_CENTER, but never upscales.static final DownsampleStrategyScales, 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 final DownsampleStrategyDefault strategy, currentlyCENTER_OUTSIDE.static final DownsampleStrategyScales, 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 final DownsampleStrategyPerforms no downsampling or scaling.static final Option<DownsampleStrategy>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. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetSampleSizeRounding(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 Details
-
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
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
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
Identical toFIT_CENTER, but never upscales. -
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
Performs no downsampling or scaling. -
DEFAULT
Default strategy, currentlyCENTER_OUTSIDE. -
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.
-
-
Constructor Details
-
DownsampleStrategy
public DownsampleStrategy()
-
-
Method Details
-
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.
-