Example usage for android.graphics Canvas drawRect

List of usage examples for android.graphics Canvas drawRect

Introduction

In this page you can find the example usage for android.graphics Canvas drawRect.

Prototype

public void drawRect(float left, float top, float right, float bottom, @NonNull Paint paint) 

Source Link

Document

Draw the specified Rect using the specified paint.

Usage

From source file:Main.java

/**
 * Create a book cover with the specified bitmap. This method applies several
 * lighting effects to the original bitmap and returns a new decored bitmap.
 *
 * @param bitmap The bitmap to decor with lighting effects
 * @param width The target width of the decored bitmap
 * @param height The target height of the decored bitmap
 *
 * @return A new Bitmap based on the original bitmap
 *//*w w  w . j ava  2s.co m*/
public static Bitmap createBookCover(Bitmap bitmap, int width, int height) {
    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    final float scale = Math.min((float) width / (float) bitmapWidth, (float) height / (float) bitmapHeight);

    final int scaledWidth = (int) (bitmapWidth * scale);
    final int scaledHeight = (int) (bitmapHeight * scale);

    final Bitmap decored = createScaledBitmap(bitmap, scaledWidth, scaledHeight, SHADOW_RADIUS, true,
            SHADOW_PAINT);
    final Canvas canvas = new Canvas(decored);

    canvas.translate(SHADOW_RADIUS / 2.0f, SHADOW_RADIUS / 2.0f);
    canvas.drawRect(EDGE_START, 0.0f, EDGE_END, scaledHeight, EDGE_PAINT);
    canvas.drawRect(FOLD_START, 0.0f, FOLD_END, scaledHeight, FOLD_PAINT);
    //noinspection PointlessArithmeticExpression
    canvas.translate(scaledWidth - (EDGE_END - EDGE_START), 0.0f);
    canvas.drawRect(EDGE_START, 0.0f, EDGE_END, scaledHeight, END_EDGE_PAINT);

    return decored;
}

From source file:Main.java

public static Bitmap setupFrame(Bitmap bitmap, int width, int color) {
    if (bitmap.getWidth() <= width * 2 || bitmap.getHeight() <= width * 2) {
        return bitmap;
    }/*  w w  w  .  j a  va 2  s.c  om*/

    Bitmap bp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

    Canvas canvas = new Canvas(bp);
    canvas.drawBitmap(bitmap, 0, 0, new Paint());
    Paint paint = new Paint();
    paint.setColor(color);
    paint.setStrokeWidth(width);
    paint.setStyle(Style.STROKE);
    canvas.drawRect(0, 0, canvas.getWidth() - width, canvas.getHeight() - width, paint);

    bitmap.recycle();
    return bp;
}

From source file:Main.java

public static Bitmap createRoundCornerBitmap(Bitmap bitmap, float roundPx, boolean lt, boolean rt, boolean lb,
        boolean rb) {
    Bitmap roundCornerBitmap = createRoundCornerBitmap(bitmap, roundPx);
    Canvas canvas = new Canvas(roundCornerBitmap);
    canvas.drawARGB(0, 0, 0, 0);/*from  ww  w .  j  a  v  a2s. c o m*/
    int bw = bitmap.getWidth();
    int bh = bitmap.getHeight();
    int centerW = bw / 2;
    int centerH = bh / 2;

    Paint paint = new Paint();
    Paint defaultPaint = new Paint();
    int color = 0xff424242;
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    paint.setAntiAlias(true);
    paint.setColor(color);

    Bitmap cutBitmap = null;
    Canvas cutCanvas = null;
    if (!lt) {
        cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        cutCanvas = new Canvas(cutBitmap);
        cutCanvas.drawRect(0, 0, centerW, centerH, defaultPaint);
        cutCanvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
        cutBitmap.recycle();
    }
    if (!rt) {
        cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        cutCanvas = new Canvas(cutBitmap);
        cutCanvas.drawRect(centerW, 0, bw, centerH, defaultPaint);
        cutCanvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
        cutBitmap.recycle();
    }
    if (!lb) {
        cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        cutCanvas = new Canvas(cutBitmap);
        cutCanvas.drawRect(0, centerH, centerW, bh, defaultPaint);
        cutCanvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
        cutBitmap.recycle();
    }
    if (!rb) {
        cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        cutCanvas = new Canvas(cutBitmap);
        cutCanvas.drawRect(centerW, centerH, bw, bh, defaultPaint);
        cutCanvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
        cutBitmap.recycle();
    }
    return roundCornerBitmap;
}

From source file:Main.java

public static Bitmap createReflectedImage(Bitmap originalImage) {
    // The gap we want between the reflection and the original image
    final int reflectionGap = 4;

    int width = originalImage.getWidth();
    int height = originalImage.getHeight();

    // This will not scale but will flip on the Y axis
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);//www .  j  av  a2 s  .  c  o m

    // Create a Bitmap with the flip matrix applied to it.
    // We only want the bottom half of the image
    Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix,
            false);

    // Create a new bitmap with same width but taller to fit reflection
    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    // Create a new Canvas with the bitmap that's big enough for
    // the image plus gap plus reflection
    Canvas canvas = new Canvas(bitmapWithReflection);
    // Draw in the original image
    canvas.drawBitmap(originalImage, 0, 0, null);
    // Draw in the gap
    Paint defaultPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
    // Draw in the reflection
    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    // Create a shader that is a linear gradient that covers the reflection
    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    // Set the paint to use this shader (linear gradient)
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

/**
 * Draw input bitmap in shadow color and offset. Assumes offset is positive.
 *
 * @param inBitmap/*from   w w  w .  j a v a  2 s . c  om*/
 * @param offsetX
 * @param offsetY
 * @param blurRadius        Not used
 * @param shadowColor
 * @return
 */
public static Bitmap shadowBitmap(Bitmap inBitmap, int offsetX, int offsetY, float blurRadius,
        int shadowColor) {

    int imgWidth = inBitmap.getWidth();
    int imgHeight = inBitmap.getHeight();

    Bitmap bottomBm = Bitmap.createBitmap(imgWidth, imgHeight, Bitmap.Config.ARGB_8888);
    Canvas bottomCanvas = new Canvas(bottomBm);
    // bottomCanvas.drawColor(shadowColor);

    Paint bottomPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    bottomPaint.setColor(shadowColor);
    bottomCanvas.drawRect(offsetX, offsetY, imgWidth, imgHeight, bottomPaint);

    //    MaskFilter filter = new BlurMaskFilter(Math.max(0.5f, blurRadius), BlurMaskFilter.Blur.NORMAL);
    bottomPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP));
    //    bottomPaint.setShadowLayer(blurRadius, offsetX, offsetX, shadowColor);
    //    bottomPaint.setMaskFilter(filter);
    bottomCanvas.drawBitmap(inBitmap, offsetX, offsetY, bottomPaint);

    return bottomBm;
}

From source file:Main.java

public static Bitmap toReflectionBitmap(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }// w  ww.  java  2 s  .c  o  m

    try {
        int reflectionGap = 1;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        // This will not scale but will flip on the Y axis
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);

        // Create a Bitmap with the flip matrix applied to it.
        // We only want the bottom half of the image
        Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

        // Create a new bitmap with same width but taller to fit
        // reflection
        Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2),
                Bitmap.Config.ARGB_8888);

        // Create a new Canvas with the bitmap that's big enough for
        // the image plus gap plus reflection
        Canvas canvas = new Canvas(bitmapWithReflection);
        // Draw in the original image
        canvas.drawBitmap(bitmap, 0, 0, null);
        // Draw in the gap
        Paint deafaultPaint = new Paint();
        canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
        // Draw in the reflection
        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
        // Create a shader that is a linear gradient that covers the
        // reflection
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
                bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff,
                Shader.TileMode.CLAMP);
        // Set the paint to use this shader (linear gradient)
        paint.setShader(shader);
        // Set the Transfer mode to be porter duff and destination in
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        // Draw a rectangle using the paint with our linear gradient
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

        bitmap = bitmapWithReflection;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap toReflectionBitmap(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }// w  w  w .j a va 2  s .  com

    try {
        int reflectionGap = 1;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        // This will not scale but will flip on the Y axis
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);

        // Create a Bitmap with the flip matrix applied to it.
        // We only want the bottom half of the image
        Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

        // Create a new bitmap with same width but taller to fit
        // reflection
        Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

        // Create a new Canvas with the bitmap that's big enough for
        // the image plus gap plus reflection
        Canvas canvas = new Canvas(bitmapWithReflection);
        // Draw in the original image
        canvas.drawBitmap(bitmap, 0, 0, null);
        // Draw in the gap
        Paint deafaultPaint = new Paint();
        canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
        // Draw in the reflection
        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
        // Create a shader that is a linear gradient that covers the
        // reflection
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
                bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
        // Set the paint to use this shader (linear gradient)
        paint.setShader(shader);
        // Set the Transfer mode to be porter duff and destination in
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
        // Draw a rectangle using the paint with our linear gradient
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

        bitmap = bitmapWithReflection;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap highlightSelectedFaceThumbnail(Bitmap originalBitmap) {
    Bitmap bitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);/* w  ww.  j  a va  2  s  . c  o  m*/
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.parseColor("#3399FF"));
    int stokeWidth = Math.max(originalBitmap.getWidth(), originalBitmap.getHeight()) / 10;
    if (stokeWidth == 0) {
        stokeWidth = 1;
    }
    bitmap.getWidth();
    paint.setStrokeWidth(stokeWidth);
    canvas.drawRect(0, 0, bitmap.getWidth(), bitmap.getHeight(), paint);

    return bitmap;
}

From source file:com.wareninja.opensource.gravatar4android.common.Utils.java

public static Bitmap getBitmapWithReflection(Bitmap originalImage) {

    //The gap we want between the reflection and the original image
    final int reflectionGap = 4;

    int width = originalImage.getWidth();
    int height = originalImage.getHeight();

    //This will not scale but will flip on the Y axis
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);/*from www .  jav  a2  s  .co  m*/

    //Create a Bitmap with the flip matrix applied to it.
    //We only want the bottom half of the image
    Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix,
            false);

    //Create a new bitmap with same width but taller to fit reflection
    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    //Create a new Canvas with the bitmap that's big enough for
    //the image plus gap plus reflection
    Canvas canvas = new Canvas(bitmapWithReflection);
    //Draw in the original image
    canvas.drawBitmap(originalImage, 0, 0, null);
    //Draw in the gap
    Paint deafaultPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
    //Draw in the reflection
    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    //Create a shader that is a linear gradient that covers the reflection
    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    //Set the paint to use this shader (linear gradient)
    paint.setShader(shader);
    //Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    //Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static void fillRect(Canvas canvas, int left, int top, int right, int bottom, boolean drawBorder,
        int fillColor, int borderColor, float borderSize) {
    if (borderSize == 0)
        borderSize = 0.5f;//w w  w.  ja va2  s.c  o m
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);
    canvas.save();
    canvas.clipRect(new Rect(left, top, right, bottom));
    if (drawBorder) {
        paint.setColor(borderColor);
        canvas.drawRect(left, top, right, bottom, paint);
        paint.setColor(fillColor);
        canvas.drawRect(left + borderSize, top + borderSize, right - (borderSize * 2),
                bottom - (borderSize * 2), paint);
    } else {
        paint.setColor(fillColor);
        canvas.drawRect(left, top, right, bottom, paint);
    }
    canvas.restore();

}