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(@NonNull Rect r, @NonNull Paint paint) 

Source Link

Document

Draw the specified Rect using the specified Paint.

Usage

From source file:Main.java

private static void clearCanvas(Canvas canvas, RectF rect) {
    if (rect.width() <= 0 || rect.height() <= 0) {
        return;/*from  ww  w.  j  a  v  a 2 s. co  m*/
    }
    canvas.drawRect(rect, PAINT);
}

From source file:Main.java

public static void drawCropRect(Canvas canvas, RectF bounds) {
    Paint p = new Paint();
    p.setStyle(Paint.Style.STROKE);
    p.setColor(Color.WHITE);//from   w ww.j  av  a  2s  .  co  m
    p.setStrokeWidth(3);
    canvas.drawRect(bounds, p);
}

From source file:Main.java

public static void erase(Canvas canvas, RectF rectf) {
    Paint paint = new Paint();
    paint.setColor(0);//from   ww  w  .jav  a2 s .  c  o  m
    paint.setDither(true);
    paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR));
    canvas.drawRect(rectf, paint);
}

From source file:Main.java

public static Bitmap createFramedImage(Drawable imageDrawable, int borderThickness) {
    int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight());
    Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    RectF outerRect = new RectF(0, 0, size, size);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);/*from w  w  w.java 2s.  c o  m*/
    canvas.drawRect(outerRect, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    imageDrawable.setBounds(0, 0, size, size);

    // Save the layer to apply the paint
    canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
    imageDrawable.draw(canvas);
    canvas.restore();

    // FRAMING THE PHOTO
    float border = size / 15f;

    // 1. Create offscreen bitmap link: http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1035s
    Bitmap framedOutput = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas framedCanvas = new Canvas(framedOutput);
    // End of Step 1

    // Start - TODO IMPORTANT - this section shouldn't be included in the final code
    // It's needed here to differentiate step 2 (red) with the background color of the activity
    // It's should be commented out after the codes includes step 3 onwards
    // Paint squaredPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // squaredPaint.setColor(Color.BLUE);
    // framedCanvas.drawRoundRect(outerRect, 0f, 0f, squaredPaint);
    // End

    // 2. Draw an opaque rounded rectangle link:
    RectF innerRect = new RectF(border, border, size - border, size - border);

    Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    innerPaint.setColor(Color.RED);
    //      framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius, outerPaint);
    framedCanvas.drawRect(innerRect, innerPaint);

    // 3. Set the Power Duff mode link:
    Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));

    // 4. Draw a translucent rounded rectangle link:
    outerPaint.setColor(Color.argb(255, 255, 255, 255));
    //      framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, outerPaint);
    framedCanvas.drawRect(outerRect, outerPaint);

    // 5. Draw the frame on top of original bitmap
    canvas.drawBitmap(framedOutput, 0f, 0f, null);

    return output;
}

From source file:Main.java

public static void drawShade(Canvas canvas, RectF bounds) {
    int w = canvas.getWidth();
    int h = canvas.getHeight();
    Paint p = new Paint();
    p.setStyle(Paint.Style.FILL);
    p.setColor(Color.BLACK & 0x88000000);

    RectF r = new RectF();
    r.set(0, 0, w, bounds.top);/*from  ww  w  .ja  v a2s .c  o m*/
    canvas.drawRect(r, p);
    r.set(0, bounds.top, bounds.left, h);
    canvas.drawRect(r, p);
    r.set(bounds.left, bounds.bottom, w, h);
    canvas.drawRect(r, p);
    r.set(bounds.right, bounds.top, w, bounds.bottom);
    canvas.drawRect(r, p);
}

From source file:com.daycle.daycleapp.custom.swipelistview.itemmanipulation.dragdrop.BitmapUtils.java

/**
 * Returns a bitmap showing a screenshot of the view passed in.
 *//*w w  w  .  jav  a  2 s.  com*/
@NonNull
static Bitmap getBitmapFromView(@NonNull final View v) {
    Bitmap bitmap = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint p = new Paint();
    p.setColor(ContextCompat.getColor(v.getContext(), R.color.colorAccent));
    p.setAlpha(50);
    //v.setBackgroundColor(ContextCompat.getColor(v.getContext(), android.R.color.transparent));
    v.draw(canvas);
    //canvas.drawCircle((float)(v.getWidth() / 2), (float)(v.getHeight() / 2), 10, p);
    canvas.drawRect(new Rect(0, 0, v.getWidth(), v.getHeight()), p);
    return bitmap;
}

From source file:Main.java

/**
 * Draw the face rect in the Image// w ww.  j  ava2s.  com
 * @param canvas
 * @param face
 * @param width
 * @param height
 * @param frontCamera
 */
static public void drawFaceRect(Canvas canvas, Rect rect, int width, int height, boolean frontCamera) {

    if (canvas == null)
        return;

    Paint paint = new Paint();
    paint.setColor(Color.rgb(57, 138, 243));
    int len = (rect.bottom - rect.top) / 8;
    if (len / 8 >= 2)
        paint.setStrokeWidth(len / 8);
    else
        paint.setStrokeWidth(2);

    if (frontCamera) {
        int left = rect.left;
        rect.left = width - rect.right;
        rect.right = width - left;
    }

    paint.setStyle(Style.STROKE);
    canvas.drawRect(rect, paint);
}

From source file:Main.java

/**
 * Return rounded bitmap with top left corner as rounded using specified bitmap and bitmap size in pixels.
 * // w ww  . j ava  2 s. co m
 * @param bitmap to make rounded
 * @param pixels size of bitmap
 * @return slightly rounded bitmap.
 */
public static Bitmap getRoundedTopLeftCornerBitmap(Bitmap bitmap, int pixels) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;
    final Rect topRightRect = new Rect(bitmap.getWidth() / 2, 0, bitmap.getWidth(), bitmap.getHeight() / 2);
    final Rect bottomRect = new Rect(0, bitmap.getHeight() / 2, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    // Fill in upper right corner
    canvas.drawRect(topRightRect, paint);
    // Fill in bottom corners
    canvas.drawRect(bottomRect, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:Main.java

public static Bitmap getMosaic(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int radius = 10;

    Bitmap mosaicBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(mosaicBitmap);

    int horCount = (int) Math.ceil(width / (float) radius);
    int verCount = (int) Math.ceil(height / (float) radius);

    Paint paint = new Paint();
    paint.setAntiAlias(true);//from  w w  w . j  av a 2 s  .  c om

    for (int horIndex = 0; horIndex < horCount; ++horIndex) {
        for (int verIndex = 0; verIndex < verCount; ++verIndex) {
            int l = radius * horIndex;
            int t = radius * verIndex;
            int r = l + radius;
            if (r > width) {
                r = width;
            }
            int b = t + radius;
            if (b > height) {
                b = height;
            }
            int color = bitmap.getPixel(l, t);
            Rect rect = new Rect(l, t, r, b);
            paint.setColor(color);
            canvas.drawRect(rect, paint);
        }
    }
    canvas.save();

    return mosaicBitmap;
}

From source file:Main.java

public static Bitmap getColorPreviewBitmap(final Context context, final int color, final boolean border) {
    if (context == null)
        return null;
    final float density = context.getResources().getDisplayMetrics().density;
    final int width = (int) (32 * density), height = (int) (32 * density);

    final Bitmap bm = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    final Canvas canvas = new Canvas(bm);

    final int rectangleSize = (int) (density * 5);
    final int numRectanglesHorizontal = (int) Math.ceil(width / rectangleSize);
    final int numRectanglesVertical = (int) Math.ceil(height / rectangleSize);
    final Rect r = new Rect();
    boolean verticalStartWhite = true;
    for (int i = 0; i <= numRectanglesVertical; i++) {

        boolean isWhite = verticalStartWhite;
        for (int j = 0; j <= numRectanglesHorizontal; j++) {

            r.top = i * rectangleSize;// w  w w .j  a v a  2 s.  c  o  m
            r.left = j * rectangleSize;
            r.bottom = r.top + rectangleSize;
            r.right = r.left + rectangleSize;
            final Paint paint = new Paint();
            paint.setColor(isWhite ? Color.WHITE : Color.GRAY);

            canvas.drawRect(r, paint);

            isWhite = !isWhite;
        }

        verticalStartWhite = !verticalStartWhite;

    }
    canvas.drawColor(color);
    if (border) {
        final Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(1f * density);
        final float[] points = new float[] { 0, 0, width, 0, 0, 0, 0, height, width, 0, width, height, 0,
                height, width, height };
        canvas.drawLines(points, paint);
    }
    return bm;
}