Example usage for android.graphics RectF inset

List of usage examples for android.graphics RectF inset

Introduction

In this page you can find the example usage for android.graphics RectF inset.

Prototype

public void inset(float dx, float dy) 

Source Link

Document

Inset the rectangle by (dx,dy).

Usage

From source file:org.getlantern.firetweet.view.ShapedImageView.java

/**
 * Given the source bitmap and a canvas, draws the bitmap through a circular
 * mask. Only draws a circle with diameter equal to the destination width.
 *
 * @param bitmap The source bitmap to draw.
 * @param canvas The canvas to draw it on.
 * @param source The source bound of the bitmap.
 * @param dest   The destination bound on the canvas.
 *///from  w w w.  ja  v a  2 s.  co  m
public void drawBitmapWithCircleOnCanvas(Bitmap bitmap, Canvas canvas, RectF source, @NonNull RectF dest) {
    if (bitmap == null) {
        if (getStyle() == SHAPE_CIRCLE) {
            canvas.drawCircle(dest.centerX(), dest.centerY(), Math.min(dest.width(), dest.height()) / 2f,
                    mSolidColorPaint);
        } else {
            final float cornerRadius = getCalculatedCornerRadius();
            canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mSolidColorPaint);
        }
        return;
    }
    // Draw bitmap through shader first.
    final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    mMatrix.reset();

    switch (getScaleType()) {
    case CENTER_CROP: {
        final float srcRatio = source.width() / source.height();
        final float dstRatio = dest.width() / dest.height();
        if (srcRatio > dstRatio) {
            // Source is wider than destination, fit height
            mTempDestination.top = dest.top;
            mTempDestination.bottom = dest.bottom;
            final float dstWidth = dest.height() * srcRatio;
            mTempDestination.left = dest.centerX() - dstWidth / 2;
            mTempDestination.right = dest.centerX() + dstWidth / 2;
        } else if (srcRatio < dstRatio) {
            mTempDestination.left = dest.left;
            mTempDestination.right = dest.right;
            final float dstHeight = dest.width() / srcRatio;
            mTempDestination.top = dest.centerY() - dstHeight / 2;
            mTempDestination.bottom = dest.centerY() + dstHeight / 2;
        } else {
            mTempDestination.set(dest);
        }
        break;
    }
    default: {
        mTempDestination.set(dest);
        break;
    }
    }

    // Fit bitmap to bounds.
    mMatrix.setRectToRect(source, mTempDestination, ScaleToFit.CENTER);

    shader.setLocalMatrix(mMatrix);
    mBitmapPaint.setShader(shader);

    if (mBorderEnabled) {
        final float inset = mBorderPaint.getStrokeWidth() / 2;
        if (getStyle() == SHAPE_CIRCLE) {
            final float circleRadius = Math.min(dest.width(), dest.height()) / 2f - inset / 2;
            canvas.drawCircle(dest.centerX(), dest.centerY(), circleRadius, mBitmapPaint);
        } else {
            final float cornerRadius = getCalculatedCornerRadius();
            dest.inset(inset, inset);
            canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mBitmapPaint);
            dest.inset(-inset, -inset);
        }
    } else {
        if (getStyle() == SHAPE_CIRCLE) {
            final float circleRadius = Math.min(dest.width(), dest.height()) / 2f;
            canvas.drawCircle(dest.centerX(), dest.centerY(), circleRadius, mBitmapPaint);
        } else {
            final float cornerRadius = getCalculatedCornerRadius();
            canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mBitmapPaint);
        }
    }

}