FileDocCategorySizeDatePackage
BitmapUtils.javaAPI DocAndroid 5.1 API5201Thu Mar 12 22:22:50 GMT 2015com.android.bitmap.util

BitmapUtils

public abstract class BitmapUtils extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidcalculateCroppedSrcRect(int srcW, int srcH, int dstW, int dstH, int dstSliceH, int sampleSize, float vertSliceFrac, boolean absoluteFrac, float verticalMultiplier, android.graphics.Rect outRect)
Calculate a center-crop rectangle for the given input and output parameters. The output rectangle to use is written in the given outRect.

param
srcW the source width
param
srcH the source height
param
dstW the destination width
param
dstH the destination height
param
dstSliceH the height extent (in destination coordinates) to exclude when cropping. You would typically pass dstH, unless you are trying to normalize different items to the same vertical crop range.
param
sampleSize a scaling factor that rect calculation will only use if it's more aggressive than regular scaling
param
vertSliceFrac vertical slice fraction determines the vertical center point for the crop rect. Range is from [0.0, 1.0]. To perform a vertically centered crop, use 0.5. Otherwise, see absoluteFrac.
param
absoluteFrac determines how the vertSliceFrac affects the vertical center point. If this parameter is true, the vertical center of the resulting output rectangle will be exactly [vertSliceFrac * srcH], with care taken to keep the bounds within the source rectangle. If this parameter is false, the vertical center will be calculated so that the values of vertSliceFrac from 0.0 to 1.0 will linearly cover the entirety of the source rectangle.
param
verticalMultiplier an optional multiplier that will alter the output Rect's aspect ratio to be this much taller in the event that y is the limiting dimension
param
outRect a Rect to write the resulting crop coordinates into

        if (sampleSize < 1) {
            sampleSize = 1;
        }
        final float regularScale;
        final float wScale = (float) srcW / dstW;
        final float hScale = (float) srcH / dstH;
        if (hScale < wScale) {
            regularScale = hScale / verticalMultiplier;
        } else {
            regularScale = wScale;
        }

        final float scale = Math.min(sampleSize, regularScale);

        final int srcCroppedW = Math.round(dstW * scale);
        final int srcCroppedH = Math.round(dstH * scale);
        final int srcCroppedSliceH = Math.round(dstSliceH * scale);
        final int srcHalfSliceH = Math.min(srcCroppedSliceH, srcH) / 2;

        outRect.left = (srcW - srcCroppedW) / 2;
        outRect.right = outRect.left + srcCroppedW;

        final int centerV;
        if (absoluteFrac) {
            final int minCenterV = srcHalfSliceH;
            final int maxCenterV = srcH - srcHalfSliceH;
            centerV = Math.max(minCenterV, Math.min(maxCenterV, Math.round(srcH * vertSliceFrac)));
        } else {
            centerV = Math
                    .round(Math.abs(srcH - srcCroppedSliceH) * vertSliceFrac + srcHalfSliceH);
        }

        outRect.top = centerV - srcCroppedH / 2;
        outRect.bottom = outRect.top + srcCroppedH;
    
public static voidcalculateCroppedSrcRect(int srcW, int srcH, int dstW, int dstH, android.graphics.Rect outRect)

param
srcW
param
srcH
param
dstW
param
dstH
param
outRect

        calculateCroppedSrcRect(srcW, srcH, dstW, dstH, Integer.MAX_VALUE, outRect);
    
public static voidcalculateCroppedSrcRect(int srcW, int srcH, int dstW, int dstH, int sampleSize, android.graphics.Rect outRect)

        if (sampleSize < 1) {
            sampleSize = 1;
        }
        final float regularScale = Math.min(
                (float) srcW / dstW,
                (float) srcH / dstH);

        final float scale = Math.min(sampleSize, regularScale);

        final int srcCroppedW = Math.round(dstW * scale);
        final int srcCroppedH = Math.round(dstH * scale);

        outRect.left = (srcW - srcCroppedW) / 2;
        outRect.right = outRect.left + srcCroppedW;

        outRect.top = (srcH - srcCroppedH) / 2;
        outRect.bottom = outRect.top + srcCroppedH;