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 getCircularBitmap(Bitmap srcBitmap) { // Calculate the circular bitmap width with border int squareBitmapWidth = Math.min(srcBitmap.getWidth(), srcBitmap.getHeight()); //int squareBitmapWidth = 300; // Initialize a new instance of Bitmap Bitmap dstBitmap = Bitmap.createBitmap(squareBitmapWidth, // Width squareBitmapWidth, // Height Bitmap.Config.ARGB_8888 // Config );//from w w w .j av a2 s.co m Canvas canvas = new Canvas(dstBitmap); // Initialize a new Paint instance Paint paint = new Paint(); paint.setAntiAlias(true); Rect rect = new Rect(0, 0, squareBitmapWidth, squareBitmapWidth); RectF rectF = new RectF(rect); canvas.drawOval(rectF, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); // Calculate the left and top of copied bitmap float left = (squareBitmapWidth - srcBitmap.getWidth()) / 2; float top = (squareBitmapWidth - srcBitmap.getHeight()) / 2; canvas.drawBitmap(srcBitmap, left, top, paint); srcBitmap.recycle(); return dstBitmap; }
From source file:Main.java
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int maxHeight, int pixels) { maxHeight = (int) (getApplicationContext().getResources().getDisplayMetrics().density * maxHeight); float scale = bitmap.getHeight() * 1.0f / maxHeight; int newWidth = (int) (bitmap.getWidth() / scale); Bitmap output = Bitmap.createBitmap(newWidth, maxHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xffffffff; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final Rect dstRect = new Rect(0, 0, newWidth, maxHeight); final RectF rectF = new RectF(dstRect); paint.setAntiAlias(true);/*from w ww . jav a2s .c o m*/ canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, (float) pixels, (float) pixels, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, dstRect, paint); return output; }
From source file:Main.java
public static Bitmap scrambleImage(Bitmap image, int piecesPerLine) { int px = (int) Math.ceil(image.getWidth() / (double) piecesPerLine); int ph = (int) Math.ceil(image.getHeight() / (double) piecesPerLine); int count = piecesPerLine * piecesPerLine; int[] arr = shuffle(arrayRange(count)); Config config = image.getConfig() == null ? Bitmap.Config.ARGB_8888 : image.getConfig(); Bitmap destBmp = Bitmap.createBitmap(image.getWidth(), image.getHeight(), config); Canvas canvas = new Canvas(destBmp); for (int i = 0; i < piecesPerLine; i++) { for (int j = 0; j < piecesPerLine; j++) { int n = arr[i * piecesPerLine + j]; int row = n / piecesPerLine; int col = n % piecesPerLine; Rect fromRect = new Rect(row * px, col * ph, row * px + px, col * ph + ph); Rect toRect = new Rect(j * px, i * ph, j * px + px, i * ph + ph); canvas.drawBitmap(image, fromRect, toRect, null); }/*from w w w. j a va 2s. com*/ } return destBmp; }
From source file:Main.java
private static void drawTouchIconToCanvas(Bitmap touchIcon, Canvas canvas, Rect iconBounds) { Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight()); // Paint used for scaling the bitmap and drawing the rounded rect. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setFilterBitmap(true);/*w w w . j a v a 2 s .c o m*/ canvas.drawBitmap(touchIcon, src, iconBounds, paint); // Construct a path from a round rect. This will allow drawing with // an inverse fill so we can punch a hole using the round rect. Path path = new Path(); path.setFillType(Path.FillType.INVERSE_WINDING); RectF rect = new RectF(iconBounds); rect.inset(1, 1); path.addRoundRect(rect, 8f, 8f, Path.Direction.CW); // Reuse the paint and clear the outside of the rectangle. paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); canvas.drawPath(path, paint); }
From source file:Main.java
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int maxHeight, int pixels) { if (bitmap == null) return bitmap; maxHeight = (int) (getApplicationContext().getResources().getDisplayMetrics().density * maxHeight); float scale = bitmap.getHeight() * 1.0f / maxHeight; int newWidth = (int) (bitmap.getWidth() / scale); Bitmap output = Bitmap.createBitmap(newWidth, maxHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xffffffff; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final Rect dstRect = new Rect(0, 0, newWidth, maxHeight); final RectF rectF = new RectF(dstRect); paint.setAntiAlias(true);/*from w ww .j av a 2 s . co m*/ canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, (float) pixels, (float) pixels, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, dstRect, paint); return output; }
From source file:Main.java
public static Bitmap getRoundImage(Bitmap oriImg, boolean recycleOld) { Bitmap targetBitmap = null;/*from w w w . j a va 2 s . c o m*/ if (oriImg != null && !oriImg.isRecycled()) { int size = Math.min(oriImg.getWidth(), oriImg.getHeight()); int targetWidth = size; int targetHeight = size; targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(targetBitmap); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setFilterBitmap(true); canvas.drawARGB(0, 0, 0, 0); canvas.drawCircle(targetWidth / 2f, targetHeight / 2f, targetWidth / 2f, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(oriImg, new Rect(0, 0, size, size), new Rect(0, 0, targetWidth, targetHeight), paint); if (recycleOld) { oriImg.recycle(); } } return targetBitmap; }
From source file:Main.java
public static Bitmap yuv2Bitmap(byte[] data, int width, int height) { final YuvImage image = new YuvImage(data, ImageFormat.NV21, width, height, null); ByteArrayOutputStream os = new ByteArrayOutputStream(data.length); if (!image.compressToJpeg(new Rect(0, 0, width, height), 100, os)) { return null; }/* ww w. java 2 s.c o m*/ byte[] tmp = os.toByteArray(); Bitmap bitmap = BitmapFactory.decodeByteArray(tmp, 0, tmp.length); return bitmap; }
From source file:Main.java
public static Bitmap getRoundCornerBitmap(Bitmap bitmap, int radius) { // Canvas canvas = new Canvas(bitmap); // Rect r =new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); // RectF rect = new RectF(r); // Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER)); // canvas.drawRoundRect(rect, 3f, 3f, paint); // // canvas.drawBitmap(bitmap, 0, 0, paint); // return 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 = radius; paint.setAntiAlias(true);//from www.j ava 2s .com // canvas.drawARGB(0, 0, 0, 0); // paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, new Paint(Paint.ANTI_ALIAS_FLAG)); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
From source file:Main.java
@SuppressLint("NewApi") public static Bitmap NV21ToRGBABitmap(byte[] nv21, int width, int height, Context context) { TimingLogger timings = new TimingLogger(TIMING_LOG_TAG, "NV21ToRGBABitmap"); Rect rect = new Rect(0, 0, width, height); try {//from w w w . j a v a2 s . c o m Class.forName("android.renderscript.Element$DataKind").getField("PIXEL_YUV"); Class.forName("android.renderscript.ScriptIntrinsicYuvToRGB"); byte[] imageData = nv21; if (mRS == null) { mRS = RenderScript.create(context); mYuvToRgb = ScriptIntrinsicYuvToRGB.create(mRS, Element.U8_4(mRS)); Type.Builder tb = new Type.Builder(mRS, Element.createPixel(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV)); tb.setX(width); tb.setY(height); tb.setMipmaps(false); tb.setYuvFormat(ImageFormat.NV21); ain = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT); timings.addSplit("Prepare for ain"); Type.Builder tb2 = new Type.Builder(mRS, Element.RGBA_8888(mRS)); tb2.setX(width); tb2.setY(height); tb2.setMipmaps(false); aOut = Allocation.createTyped(mRS, tb2.create(), Allocation.USAGE_SCRIPT & Allocation.USAGE_SHARED); timings.addSplit("Prepare for aOut"); bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); timings.addSplit("Create Bitmap"); } ain.copyFrom(imageData); timings.addSplit("ain copyFrom"); mYuvToRgb.setInput(ain); timings.addSplit("setInput ain"); mYuvToRgb.forEach(aOut); timings.addSplit("NV21 to ARGB forEach"); aOut.copyTo(bitmap); timings.addSplit("Allocation to Bitmap"); } catch (Exception e) { YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, width, height, null); timings.addSplit("NV21 bytes to YuvImage"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); yuvImage.compressToJpeg(rect, 90, baos); byte[] cur = baos.toByteArray(); timings.addSplit("YuvImage crop and compress to Jpeg Bytes"); bitmap = BitmapFactory.decodeByteArray(cur, 0, cur.length); timings.addSplit("Jpeg Bytes to Bitmap"); } timings.dumpToLog(); return bitmap; }
From source file:Main.java
public static void fillRect(Canvas canvas, int left, int top, int right, int bottom, boolean drawBorder, int fillColor, int borderColor, float borderSize) { if (borderSize == 0) borderSize = 0.5f;// ww w. ja v a2 s . c om paint.setAntiAlias(true); paint.setStyle(Paint.Style.FILL); canvas.save(); canvas.clipRect(new Rect(left, top, right, bottom)); if (drawBorder) { paint.setColor(borderColor); canvas.drawRect(left, top, right, bottom, paint); paint.setColor(fillColor); canvas.drawRect(left + borderSize, top + borderSize, right - (borderSize * 2), bottom - (borderSize * 2), paint); } else { paint.setColor(fillColor); canvas.drawRect(left, top, right, bottom, paint); } canvas.restore(); }