Example usage for android.graphics RectF RectF

List of usage examples for android.graphics RectF RectF

Introduction

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

Prototype

public RectF(float left, float top, float right, float bottom) 

Source Link

Document

Create a new rectangle with the specified coordinates.

Usage

From source file:Main.java

public static Bitmap toCircleBitmap(Bitmap bitmap) {

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int r = width < height ? width : height;

    Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(buffer);
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*  w w w  . ja v  a2 s  .  c  o  m*/
    RectF rect = new RectF(0, 0, r, r);

    canvas.drawCircle(r / 2, r / 2, r / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, null, rect, paint);
    bitmap.recycle();
    return buffer;
}

From source file:Main.java

private static void drawAntiRoundRect(Canvas canvas, Paint paint, int radius, RectF rect, int direction) {
    if (direction == 1) {
        Path path = new Path();
        path.moveTo(rect.left, rect.top);
        path.lineTo(rect.left, rect.top + radius);
        path.addArc(new RectF(rect.left, rect.top, rect.left + radius * 2, rect.top + radius * 2), 180, 90);
        path.lineTo(rect.left, rect.top);
        path.close();//  w w w.  jav a 2  s .c  om
        canvas.drawPath(path, paint);
    } else if (direction == 2) {
        Path path = new Path();
        path.moveTo(rect.right, rect.top);
        path.lineTo(rect.right - radius, rect.top);
        path.addArc(new RectF(rect.right - 2 * radius, rect.top, rect.right, rect.top + radius * 2), 270, 90);
        path.lineTo(rect.right, rect.top);
        path.close();
        canvas.drawPath(path, paint);
    } else if (direction == 3) {
        Path path = new Path();
        path.moveTo(rect.right, rect.bottom);
        path.lineTo(rect.right, rect.bottom - radius);
        path.addArc(new RectF(rect.right - 2 * radius, rect.bottom - 2 * radius, rect.right, rect.bottom), 0,
                90);
        path.lineTo(rect.right, rect.bottom);
        path.close();
        canvas.drawPath(path, paint);
    } else if (direction == 4) {
        Path path = new Path();
        path.moveTo(rect.left, rect.bottom);
        path.lineTo(rect.left + radius, rect.bottom);
        path.addArc(new RectF(rect.left, rect.bottom - 2 * radius, rect.left + 2 * radius, rect.bottom), 90,
                90);
        path.lineTo(rect.left, rect.bottom);
        path.close();
        canvas.drawPath(path, paint);
    }
}

From source file:Main.java

public static List<RectF> createBoundLines(RectF baseRect, final float graphSideZoom) {
    Log.d("GammaGraph", "function createBoundLines");
    List<RectF> boundLines = new ArrayList<RectF>();
    final float drawRectSide = baseRect.right - baseRect.left;
    final float rectSideQuadDiv = drawRectSide / 4;
    for (int i = 1; i <= 3; i++) {
        final float linePos = i * rectSideQuadDiv;
        RectF boundLine1 = new RectF(baseRect.left + linePos, baseRect.top + 1, baseRect.left + linePos,
                baseRect.bottom - 1);/*from ww w  .  ja v a  2 s  .c om*/
        RectF boundLine2 = new RectF(baseRect.left + 1, baseRect.top + linePos, baseRect.right - 1,
                baseRect.top + linePos);
        boundLines.add(boundLine1);
        boundLines.add(boundLine2);
    }
    return boundLines;
}

From source file:Main.java

public static RectF zoom(final Rect rect, final float zoom) {
    return new RectF(zoom * rect.left, zoom * rect.top, zoom * rect.right, zoom * rect.bottom);
}

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.  ja  v  a  2  s  .co  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 Bitmap roundCorners(final Bitmap source, final float radius) {
    int width = source.getWidth();
    int height = source.getHeight();

    Paint paint = new Paint();
    paint.setAntiAlias(true);/*  www .  j a  va  2s  . c o m*/
    paint.setColor(Color.WHITE);

    Bitmap clipped = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(clipped);
    canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

    Bitmap rounded = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    canvas = new Canvas(rounded);
    canvas.drawBitmap(source, 0, 0, null);
    canvas.drawBitmap(clipped, 0, 0, paint);

    source.recycle();
    clipped.recycle();

    return rounded;
}

From source file:Main.java

/**
 * Scale a bitmap and correct the dimensions
 *
 * @param bitmap      Bitmap to scale/*from  www.j  a v  a 2  s . c om*/
 * @param width       width for scaling
 * @param height      height for scaling
 * @param orientation Current orientation of the Image
 * @return Scaled bitmap
 */
public static Bitmap getScaledBitmap(Bitmap bitmap, int width, int height, int orientation) {
    Matrix m = new Matrix();
    m.setRectToRect(new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()), new RectF(0, 0, width, height),
            Matrix.ScaleToFit.CENTER);
    if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
        m.postRotate(ORIENTATION_90);
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
        m.postRotate(ORIENTATION_180);
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
        m.postRotate(ORIENTATION_270);
    }
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
}

From source file:Main.java

public static Bitmap createRoundedFramedImage(Drawable imageDrawable, int borderThickness) {
    int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight());
    //      Drawable imageDrawable = (image != null) ? new BitmapDrawable(image) : placeHolder;

    Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    RectF outerRect = new RectF(0, 0, size, size);
    float cornerRadius = size / 18f;

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);// w w  w. j  a v a  2s  .  c o  m
    canvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, 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:
    // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1044s
    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, innerPaint);

    // 3. Set the Power Duff mode link:
    // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1056s
    Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));

    // 4. Draw a translucent rounded rectangle link:
    // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU
    outerPaint.setColor(Color.argb(100, 0, 0, 0));
    framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, 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 Bitmap getRoundedCornerBitmap3(Drawable imageDrawable, int radius) {

    Bitmap d = ((BitmapDrawable) imageDrawable).getBitmap();
    BitmapShader shader = new BitmapShader(d, TileMode.CLAMP, TileMode.CLAMP);

    int size = Math.min(d.getWidth(), d.getHeight());
    Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    RectF outerRect = new RectF(0, 0, size, size);
    //      float cornerRadius = radius;

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setShader(shader);//from  w w  w .jav a2  s  .  com
    paint.setAntiAlias(true);
    paint.setColor(Color.RED);
    canvas.drawCircle(outerRect.centerX(), outerRect.centerY(), d.getWidth() / 2, paint);

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

    Canvas canvas1 = new Canvas(output);

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

    Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint1.setShader(shader);
    paint1.setAntiAlias(true);
    paint1.setColor(Color.RED);
    canvas1.drawCircle(outerRect1.centerX(), outerRect1.centerY(), d.getWidth() / 2, paint);

    paint1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    return output;
}

From source file:Main.java

public static void invalidateGlobalRegion(View view) {
    invalidateGlobalRegion(view, new RectF(view.getLeft(), view.getTop(), view.getRight(), view.getBottom()));
}