List of usage examples for android.graphics Rect Rect
public Rect(int left, int top, int right, int bottom)
From source file:Main.java
/** * Gets the rounded corner bitmap./*from w w w .j a v a2 s. com*/ * * @param bitmap * the bitmap * @return the rounded corner bitmap */ public static Bitmap getRoundedCornerBitmap(Context context, Bitmap bitmap, Boolean create_circle) { DisplayMetrics mMetrics = context.getResources().getDisplayMetrics(); float mScaleFactor = mMetrics.density; Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = Color.BLACK; final Paint paint = new Paint(); int width = bitmap.getWidth(); int height = (bitmap.getHeight() > width) ? width : bitmap.getHeight(); final Rect rect = new Rect(0, 0, width, height); final RectF rectF = new RectF(rect); final float roundPx = (create_circle) ? (bitmap.getWidth() > 360) ? bitmap.getWidth() : 360 : 2; paint.setAntiAlias(true); paint.setColor(color); paint.setStyle(Paint.Style.FILL); canvas.drawARGB(0, 0, 0, 0); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); // draw border paint.setColor(Color.parseColor("#cccccc")); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(0.5F * mScaleFactor); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); return output; }
From source file:Main.java
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 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
/** * Create a Bitmap that will be used as a mask to create the preview window in the background. * The Bitmap will be the given width and height. The Bitmap will be transparent except for a * gradient on the left and bottom sides. * * @param width Width of the mask.//from w ww.j a v a 2s .co m * @param height Height of the mask. * @param gradientSize Size of the gradient. * @return Bitmap mask that will be used to create a preview window. */ private static Bitmap createPreviewWindowMask(int width, int height, int gradientSize) { // Do nothing if the size is invalid if (width <= 0 || height <= 0) { return null; } // Initialize the mask Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(mask); canvas.drawColor(Color.TRANSPARENT); // If the gradient size is zero, don't draw the gradients if (gradientSize <= 0) { return mask; } // Calculate gradient rects Rect leftGradientRect = new Rect(0, 0, gradientSize, height - gradientSize); Rect bottomGradientRect = new Rect(leftGradientRect.right, height - gradientSize, width, height); Rect cornerGradientRect = new Rect(leftGradientRect.left, leftGradientRect.bottom, bottomGradientRect.left, bottomGradientRect.bottom); // Create left gradient Paint leftGradientPaint = new Paint(); leftGradientPaint.setDither(true); leftGradientPaint.setShader(new LinearGradient(leftGradientRect.left, 0, leftGradientRect.right, 0, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP)); // Create right gradient Paint bottomGradientPaint = new Paint(); bottomGradientPaint.setDither(true); bottomGradientPaint.setShader( new LinearGradient(leftGradientRect.right, bottomGradientRect.bottom, leftGradientRect.right, bottomGradientRect.top, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP)); // Create corner gradient Paint cornerGradientPaint = new Paint(); cornerGradientPaint.setDither(true); cornerGradientPaint.setShader(new RadialGradient(cornerGradientRect.right, cornerGradientRect.top, gradientSize, Color.TRANSPARENT, Color.BLACK, Shader.TileMode.CLAMP)); // Draw the gradients canvas.drawRect(leftGradientRect, leftGradientPaint); canvas.drawRect(bottomGradientRect, bottomGradientPaint); canvas.drawRect(cornerGradientRect, cornerGradientPaint); return mask; }
From source file:Main.java
public static Bitmap roundCornerFromFile(String filePath, int pixels, int size) { try {// w w w. j av a2 s . c o m Bitmap bitmap = loadFile(filePath, size); if (bitmap == null) return null; Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); 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; paint.setAntiAlias(true); 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); bitmap.recycle(); bitmap = null; return output; } catch (OutOfMemoryError e) { return null; } catch (Exception e) { return null; } }
From source file:Main.java
public static Bitmap toRoundBitmap(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { roundPx = width / 2;//from w ww . ja v a 2 s.co m top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { roundPx = height / 2; float clip = (width - height) / 2; left = clip; right = width - clip; top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height; } Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom); final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom); final RectF rectF = new RectF(dst); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, src, dst, paint); bitmap.recycle(); return output; }
From source file:Main.java
public static Bitmap getRoundBitmap(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { roundPx = width / 2;/*from ww w . ja v a 2 s . co m*/ top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { roundPx = height / 2; float clip = (width - height) / 2; left = clip; right = width - clip; top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height; } Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom); final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom); final RectF rectF = new RectF(dst); paint.setAntiAlias(true); 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, src, dst, paint); return output; }
From source file:Main.java
/** * Load the image at {@code imagePath} as a {@link Bitmap}, scaling it to * the specified size and preserving the aspect ratio. * @param imagePath Path of the image to load. * @param width Required width of the resulting {@link Bitmap}. * @param height Required height of the resulting {@link Bitmap}. * @param fill {@code true} to fill the empty space with transparent color. * @param crop {@code true} to crop the image, {@code false} to resize without cutting the image. * @return {@link Bitmap} representing the image at {@code imagePath}. *///from w w w . j a v a 2s. c o m public static Bitmap loadResizedBitmap(String imagePath, int width, int height, boolean fill, boolean crop) { Bitmap retVal; BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inSampleSize = getScale(imagePath, width, height); opts.inJustDecodeBounds = false; Bitmap image = BitmapFactory.decodeFile(imagePath, opts); if (image == null) { return null; } if (image.getWidth() != width || image.getHeight() != height) { int scaledWidth = (image.getWidth() * height) / image.getHeight(); int scaledHeight; // = (image.getHeight() * width) / image.getWidth(); if ((crop && scaledWidth > width) || (!crop && scaledWidth < width)) { scaledHeight = height; } else { scaledWidth = width; scaledHeight = (image.getHeight() * width) / image.getWidth(); } //image = Bitmap.createScaledBitmap(image, scaledWidth, scaledHeight, true); Rect src = new Rect(0, 0, image.getWidth(), image.getHeight()); Rect dst = new Rect(0, 0, scaledWidth, scaledHeight); if (fill) { retVal = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); dst.offset((width - scaledWidth) / 2, (height - scaledHeight) / 2); } else { retVal = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888); } retVal.eraseColor(Color.TRANSPARENT); synchronized (canvas) { if (antiAliasPaint == null) { antiAliasPaint = new Paint(); antiAliasPaint.setAntiAlias(true); antiAliasPaint.setFilterBitmap(true); antiAliasPaint.setDither(true); } canvas.setBitmap(retVal); canvas.drawBitmap(image, src, dst, antiAliasPaint); } image.recycle(); } else { //No need to scale. retVal = image; } return retVal; }
From source file:Main.java
public static Bitmap toRoundBitmap(Bitmap bitmap) { if (bitmap == null) { return null; }/*from w ww. j a va 2s. c om*/ int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { roundPx = width / 2; top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { roundPx = height / 2; float clip = (width - height) / 2; left = clip; right = width - clip; top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height; } Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom); final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom); final RectF rectF = new RectF(dst); paint.setAntiAlias(true); 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, src, dst, paint); return output; }
From source file:Main.java
/** * Transform source Bitmap to targeted width and height. *//*ww w .java2 s . co m*/ private static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, int options) { boolean scaleUp = (options & OPTIONS_SCALE_UP) != 0; boolean recycle = (options & OPTIONS_RECYCLE_INPUT) != 0; int deltaX = source.getWidth() - targetWidth; int deltaY = source.getHeight() - targetHeight; if (!scaleUp && (deltaX < 0 || deltaY < 0)) { /* * In this case the bitmap is smaller, at least in one dimension, * than the target. Transform it by placing as much of the image * as possible into the target and leaving the top/bottom or * left/right (or both) black. */ Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b2); int deltaXHalf = Math.max(0, deltaX / 2); int deltaYHalf = Math.max(0, deltaY / 2); Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()), deltaYHalf + Math.min(targetHeight, source.getHeight())); int dstX = (targetWidth - src.width()) / 2; int dstY = (targetHeight - src.height()) / 2; Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY); c.drawBitmap(source, src, dst, null); if (recycle) { source.recycle(); } return b2; } float bitmapWidthF = source.getWidth(); float bitmapHeightF = source.getHeight(); float bitmapAspect = bitmapWidthF / bitmapHeightF; float viewAspect = (float) targetWidth / targetHeight; if (bitmapAspect > viewAspect) { float scale = targetHeight / bitmapHeightF; if (scale < .9F || scale > 1F) { scaler.setScale(scale, scale); } else { scaler = null; } } else { float scale = targetWidth / bitmapWidthF; if (scale < .9F || scale > 1F) { scaler.setScale(scale, scale); } else { scaler = null; } } Bitmap b1; if (scaler != null) { // this is used for minithumb and crop, so we want to filter here. b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true); } else { b1 = source; } if (recycle && b1 != source) { source.recycle(); } int dx1 = Math.max(0, b1.getWidth() - targetWidth); // int dy1 = Math.max(0, b1.getHeight() - targetHeight); Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, 0, //dy1 / 2, targetWidth, targetHeight); if (b2 != b1) { if (recycle || b1 != source) { b1.recycle(); } } return b2; }
From source file:Main.java
/** * Transform source Bitmap to targeted width and height. *///from w ww . j a v a 2 s .c o m private static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, int options) { boolean scaleUp = (options & OPTIONS_SCALE_UP) != 0; boolean recycle = (options & OPTIONS_RECYCLE_INPUT) != 0; int deltaX = source.getWidth() - targetWidth; int deltaY = source.getHeight() - targetHeight; if (!scaleUp && (deltaX < 0 || deltaY < 0)) { /* * In this case the bitmap is smaller, at least in one dimension, * than the target. Transform it by placing as much of the image as * possible into the target and leaving the top/bottom or left/right * (or both) black. */ Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Config.ARGB_8888); Canvas c = new Canvas(b2); int deltaXHalf = Math.max(0, deltaX / 2); int deltaYHalf = Math.max(0, deltaY / 2); Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()), deltaYHalf + Math.min(targetHeight, source.getHeight())); int dstX = (targetWidth - src.width()) / 2; int dstY = (targetHeight - src.height()) / 2; Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY); c.drawBitmap(source, src, dst, null); if (recycle) { source.recycle(); } return b2; } float bitmapWidthF = source.getWidth(); float bitmapHeightF = source.getHeight(); float bitmapAspect = bitmapWidthF / bitmapHeightF; float viewAspect = (float) targetWidth / targetHeight; if (bitmapAspect > viewAspect) { float scale = targetHeight / bitmapHeightF; if (scale < .9F || scale > 1F) { scaler.setScale(scale, scale); } else { scaler = null; } } else { float scale = targetWidth / bitmapWidthF; if (scale < .9F || scale > 1F) { scaler.setScale(scale, scale); } else { scaler = null; } } Bitmap b1; if (scaler != null) { // this is used for minithumb and crop, so we want to filter here. b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true); } else { b1 = source; } if (recycle && b1 != source) { source.recycle(); } int dx1 = Math.max(0, b1.getWidth() - targetWidth); int dy1 = Math.max(0, b1.getHeight() - targetHeight); Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight); if (b2 != b1) { if (recycle || b1 != source) { b1.recycle(); } } return b2; }