List of usage examples for android.graphics Rect Rect
public Rect(int left, int top, int right, int bottom)
From source file:Main.java
public static Bitmap toRoundBitmap(Bitmap bitmap, int roundPx) { if (bitmap == null) { return null; }//from w ww . j a va 2s . c o m int width = bitmap.getWidth(); int height = bitmap.getHeight(); float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { 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); return output; }
From source file:Main.java
/** * For excessively large images with an awkward ratio we * will want to crop them/* w w w . j av a 2 s.c o m*/ * @return */ public static Rect getCropRectIfNecessary(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int width = options.outWidth; final int height = options.outHeight; Rect rect = new Rect(0, 0, width, height); // Determine downsampled size int targetWidth = reqWidth * options.inSampleSize; int targetHeight = reqHeight * options.inSampleSize; if (targetHeight < height) { rect.top = (height - targetHeight) / 2; rect.bottom = rect.top + targetHeight; } if (targetWidth < width) { rect.left = (width - targetWidth) / 2; rect.right = rect.left + targetWidth; } return rect; }
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}. */// w ww . ja v a 2 s . 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) { //Image need to be resized. // int scaledWidth = image.getWidth(); // int scaledHeight = image.getHeight(); // final float factorWidth = scaledWidth / width; // final float factorHeight = scaledHeight / height; //final float factor = (scaledWidth / width) - (scaledHeight / height); // final long factor = (scaledWidth * height) - (scaledHeight * width); // if ((crop && factor > 0) || (factor < 0)) { // scaledHeight = (scaledHeight * width) / scaledWidth; // scaledWidth = width; // } else { // scaledWidth = (scaledWidth * height) / scaledHeight; // scaledHeight = 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
static public Bitmap NV21ToRGBABitmap(byte[] nv21, int width, int height) { YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, width, height, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, baos); byte[] cur = baos.toByteArray(); return BitmapFactory.decodeByteArray(cur, 0, cur.length); }
From source file:Main.java
public static Bitmap toRoundBitmap(Bitmap bitmap, int roundPx) { if (bitmap == null) { return null; }//from w w w .jav a 2s . co m int width = bitmap.getWidth(); int height = bitmap.getHeight(); float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { 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:com.arisprung.tailgate.utilities.FacebookImageLoader.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()); paint.setAntiAlias(true);//from w w w. j av a 2s . co m canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); // canvas.drawRoundRect(rectF, roundPx, roundPx, paint); canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); int dens = mContext.getResources().getDisplayMetrics().densityDpi; Bitmap _bmp = Bitmap.createScaledBitmap(output, dens / 2, dens / 2, false); // return _bmp; Bitmap bit = TailGateUtility.drawWhiteFrame(_bmp); return bit; }
From source file:ar.uba.fi.splitapp.MockServer.java
private static Bitmap getCircleBitmap(Bitmap bitmap) { final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(output); final int color = Color.RED; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); paint.setAntiAlias(true);/* ww w . j a v a2 s .com*/ canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawOval(rectF, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); bitmap.recycle(); return output; }
From source file:Main.java
/** * Helper that does the work of the above functions. Gets the rectangular * position of a Bitmap if it were placed inside a View with scale type set * to {@link ImageView#ScaleType #CENTER_INSIDE}. * // ww w . ja va 2 s .c o m * @param bitmapWidth the Bitmap's width * @param bitmapHeight the Bitmap's height * @param viewWidth the parent View's width * @param viewHeight the parent View's height * @return the rectangular position of the Bitmap */ private static Rect getBitmapRectCenterInsideHelper(int bitmapWidth, int bitmapHeight, int viewWidth, int viewHeight) { double resultWidth; double resultHeight; int resultX; int resultY; double viewToBitmapWidthRatio = Double.POSITIVE_INFINITY; double viewToBitmapHeightRatio = Double.POSITIVE_INFINITY; // Checks if either width or height needs to be fixed if (viewWidth < bitmapWidth) { viewToBitmapWidthRatio = (double) viewWidth / (double) bitmapWidth; } if (viewHeight < bitmapHeight) { viewToBitmapHeightRatio = (double) viewHeight / (double) bitmapHeight; } // If either needs to be fixed, choose smallest ratio and calculate from // there if (viewToBitmapWidthRatio != Double.POSITIVE_INFINITY || viewToBitmapHeightRatio != Double.POSITIVE_INFINITY) { if (viewToBitmapWidthRatio <= viewToBitmapHeightRatio) { resultWidth = viewWidth; resultHeight = (bitmapHeight * resultWidth / bitmapWidth); } else { resultHeight = viewHeight; resultWidth = (bitmapWidth * resultHeight / bitmapHeight); } } // Otherwise, the picture is within frame layout bounds. Desired width // is simply picture size else { resultHeight = bitmapHeight; resultWidth = bitmapWidth; } // Calculate the position of the bitmap inside the ImageView. if (resultWidth == viewWidth) { resultX = 0; resultY = (int) Math.round((viewHeight - resultHeight) / 2); } else if (resultHeight == viewHeight) { resultX = (int) Math.round((viewWidth - resultWidth) / 2); resultY = 0; } else { resultX = (int) Math.round((viewWidth - resultWidth) / 2); resultY = (int) Math.round((viewHeight - resultHeight) / 2); } final Rect result = new Rect(resultX, resultY, resultX + (int) Math.ceil(resultWidth), resultY + (int) Math.ceil(resultHeight)); return result; }
From source file:app.geochat.ui.widgets.BezelImageView.java
@Override protected boolean setFrame(int l, int t, int r, int b) { final boolean changed = super.setFrame(l, t, r, b); mBounds = new Rect(0, 0, r - l, b - t); mBoundsF = new RectF(mBounds); if (mBorderDrawable != null) { mBorderDrawable.setBounds(mBounds); }//from w ww . j ava 2 s . c om if (mMaskDrawable != null) { mMaskDrawable.setBounds(mBounds); } if (changed) { mCacheValid = false; } return changed; }
From source file:com.ruesga.rview.misc.BitmapUtils.java
@SuppressWarnings("ResultOfMethodCallIgnored") public static File optimizeImage(Context context, InputStream source, Bitmap.CompressFormat format, int quality) throws IOException { // Copy original source to a temp file File f = null;/*from w w w . jav a 2 s. c o m*/ OutputStream os = null; try { f = CacheHelper.createNewTemporaryFile(context, "." + format.name()); if (f != null) { os = new BufferedOutputStream(new FileOutputStream(f), 4096); IOUtils.copy(source, os); IOUtils.closeQuietly(os); // Compress to webp format long start = System.currentTimeMillis(); int[] size = decodeBitmapSize(f); if (size[0] != -1 && size[1] != -1) { Rect r = new Rect(0, 0, size[0], size[1]); adjustRectToMinimumSize(r, calculateMaxAvailableSize(context)); Bitmap src = createUnscaledBitmap(f, r.width(), r.height()); try { long originalSize = f.length(); os = new BufferedOutputStream(new FileOutputStream(f), 4096); src.compress(format, quality, os); IOUtils.closeQuietly(os); long end = System.currentTimeMillis(); Log.d(TAG, "Compressed " + f + " to " + format.name() + " in " + (end - start) + "ms" + "; dimensions " + src.getWidth() + "x" + src.getHeight() + "; size: " + f.length() + "; original: " + originalSize); return f; } finally { src.recycle(); } } else { f.delete(); } } } catch (IOException ex) { IOUtils.closeQuietly(os); if (f != null) { f.delete(); } throw ex; } finally { IOUtils.closeQuietly(os); IOUtils.closeQuietly(source); } return null; }