Example usage for android.graphics Rect Rect

List of usage examples for android.graphics Rect Rect

Introduction

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

Prototype

public Rect(int left, int top, int right, int bottom) 

Source Link

Document

Create a new rectangle with the specified coordinates.

Usage

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    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 = 50;

    paint.setAntiAlias(true);/* ww w.  j  a  v  a  2s .  c o  m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

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

    return output;
}

From source file:Main.java

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
    Bitmap sbmp;/*from   w ww  .j ava2s. com*/

    if (bmp.getWidth() != radius || bmp.getHeight() != radius)
        sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    else
        sbmp = bmp;

    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    //final int color = 0xffa19774;
    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);

    canvas.drawARGB(0, 0, 0, 0);

    paint.setColor(Color.parseColor("#BAB399"));

    canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f,
            paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

    canvas.drawBitmap(sbmp, rect, rect, paint);

    return output;
}

From source file:Main.java

public static String savetoJPEG(byte[] data, int width, int height, String file) {
    Rect frame = new Rect(0, 0, width, height);
    YuvImage img = new YuvImage(data, ImageFormat.NV21, width, height, null);
    OutputStream os = null;//w w  w  .  j  av a  2s .  c om
    File jpgfile = new File(file);
    try {
        os = new FileOutputStream(jpgfile);
        img.compressToJpeg(frame, 100, os);
        os.flush();
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return jpgfile.getPath();
}

From source file:Main.java

/**
 * Use touch-icon or higher-resolution favicon and round the corners.
 * @param context    Context used to get resources.
 * @param touchIcon  Touch icon bitmap.// w w  w .  j a v  a 2 s  .c o m
 * @param canvas     Canvas that holds the touch icon.
 */
private static void drawTouchIconToCanvas(Context context, Bitmap touchIcon, Canvas canvas) {
    Rect iconBounds = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
    Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setFilterBitmap(true);
    canvas.drawBitmap(touchIcon, src, iconBounds, paint);
    // Convert dp to px.
    int borderRadii = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, TOUCHICON_BORDER_RADII,
            context.getResources().getDisplayMetrics());
    Path path = new Path();
    path.setFillType(Path.FillType.INVERSE_WINDING);
    RectF rect = new RectF(iconBounds);
    rect.inset(INSET_DIMENSION_FOR_TOUCHICON, INSET_DIMENSION_FOR_TOUCHICON);
    path.addRoundRect(rect, borderRadii, borderRadii, Path.Direction.CW);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    canvas.drawPath(path, paint);
}

From source file:Main.java

@SuppressLint("NewApi")
private static List<Camera.Area> buildMiddleArea(int areaPer1000) {
    return Collections
            .singletonList(new Camera.Area(new Rect(-areaPer1000, -areaPer1000, areaPer1000, areaPer1000), 1));
}

From source file:Main.java

public static Rect getFramingRect(int imgWidth, int imgHeight) {

    int width;//from   w w  w  .ja  va  2s. c  o  m
    int height;
    Rect framingRect;
    if (imgWidth > imgHeight) {
        if (imgWidth * 9 / 16 > imgHeight) {
            width = imgHeight * 16 / 9;
            height = imgHeight;
        } else {
            height = imgWidth * 9 / 16;
            width = imgWidth;
        }
    } else {
        if (imgWidth * 16 / 9 > imgHeight) {
            width = imgHeight * 9 / 16;
            height = imgHeight;

        } else {
            width = imgWidth;
            height = imgWidth * 16 / 9;
        }
    }
    int leftOffset = (imgWidth - width) / 2;
    int topOffset = (imgHeight - height) / 2;
    System.out.println("leftOffset=" + leftOffset + ":topOffset=" + topOffset);
    framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);

    return framingRect;
}

From source file:Main.java

/**
 * Get a circular (rounded) bitmap shape with the diameter is the smaller between target width and target height.
 * @param bitmap/*  w  ww  .  j  a  va2s .  c  o  m*/
 * @param width target width
 * @param height target height
 * @return Rounded (circular) bitmap width diameter is the smaller between target width and target height.
 */
public static Bitmap getRoundedBitmap(Bitmap bitmap, int width, int height) {
    int diameter = width < height ? width : height;
    Bitmap targetBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(width / 2f, height / 2f, diameter / 2f, Path.Direction.CCW);
    canvas.clipPath(path);

    Bitmap sourceBitmap = bitmap;
    Rect destinationRect = new Rect(0, 0, width, height);
    Rect sourceRect = new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight());
    canvas.drawBitmap(sourceBitmap, sourceRect, destinationRect, null);
    return targetBitmap;
}

From source file:Main.java

public static boolean canScrollList(final ListView lv, int direction) {
    final int childCount = lv.getChildCount();
    if (childCount == 0) {
        return false;
    }/*from  w  ww.j a va2s .  co  m*/

    final int firstPosition = lv.getFirstVisiblePosition();
    final Rect listPadding = new Rect(lv.getListPaddingLeft(), lv.getListPaddingTop(), lv.getListPaddingRight(),
            lv.getListPaddingBottom());
    if (direction > 0) {
        final int lastBottom = lv.getChildAt(childCount - 1).getBottom();
        final int lastPosition = firstPosition + childCount;
        return lastPosition < lv.getCount() || lastBottom > lv.getHeight() - listPadding.bottom;
    } else {
        final int firstTop = lv.getChildAt(0).getTop();
        return firstPosition > 0 || firstTop < listPadding.top;
    }
}

From source file:Main.java

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

    int bmWidth = bitmap.getWidth();
    int bmHeight = bitmap.getHeight();
    int side = bmWidth < bmHeight ? bmWidth : bmHeight;
    int x = (bmWidth - side) / 2;
    int y = (bmHeight - side) / 2;
    Bitmap newBm = Bitmap.createBitmap(side, side, Bitmap.Config.ARGB_8888);
    if (newBm != null) {
        Canvas canvas = new Canvas(newBm);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);

        Rect rect = new Rect(0, 0, newBm.getWidth(), newBm.getHeight());
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawCircle(newBm.getWidth() / 2, newBm.getHeight() / 2, newBm.getHeight() / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return newBm;
    }
    return null;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Context context, Bitmap input, int pixels, int w, int h,
        boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR) {

    bitmapResult = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmapResult);
    final float densityMultiplier = context.getResources().getDisplayMetrics().density;

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0 + 5, 0 + 5, w - 5, h - 5);
    final RectF rectF = new RectF(rect);

    // make sure that our rounded corner is scaled appropriately

    final float roundPx = pixels * densityMultiplier;

    paint.setAntiAlias(true);//from w  w w  .ja va 2s  .  c o  m
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    // canvas.drawColor(Color.BLACK);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    // draw rectangles over the corners we want to be square
    if (squareTL) {
        canvas.drawRect(0, 0, w / 2, h / 2, paint);
    }
    if (squareTR) {
        canvas.drawRect(w / 2, 0, w, h / 2, paint);
    }
    if (squareBL) {
        canvas.drawRect(0, h / 2, w / 2, h, paint);
    }
    if (squareBR) {
        canvas.drawRect(w / 2, h / 2, w, h, paint);
    }

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    // paint.setXfermode(new AvoidXfermode(Color.WHITE, 255,
    // AvoidXfermode.Mode.TARGET));
    canvas.drawBitmap(input, 0, 0, paint);

    return bitmapResult;
}