List of usage examples for android.graphics PorterDuffXfermode PorterDuffXfermode
public PorterDuffXfermode(PorterDuff.Mode mode)
From source file:Main.java
/** * cut a circle from a bitmap// w w w . j a v a 2 s . c o m * * @param picturePath complete path name for the file. * @param destCube the cube dimension of dest bitmap * @return the bitmap */ public static Bitmap cutCircleFromBitmap(String picturePath, int destCube) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inDither = false; //Disable Dithering mode opts.inPurgeable = true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared opts.inInputShareable = true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future opts.inTempStorage = new byte[32 * 1024]; Bitmap bitmapImg = BitmapFactory.decodeFile(picturePath, opts); int cube = destCube; if (bitmapImg == null) return null; int smallest = Math.min(bitmapImg.getWidth(), bitmapImg.getHeight()); Bitmap output = Bitmap.createBitmap(cube, cube, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); int left = (int) ((bitmapImg.getWidth() - smallest) * 0.5); int top = (int) ((bitmapImg.getHeight() - smallest) * 0.5); final Rect rectSrc = new Rect(left, top, left + smallest, top + smallest); final Rect rectDest = new Rect(0, 0, cube, cube); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawCircle(cube / 2, cube / 2, cube / 2, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmapImg, rectSrc, rectDest, paint); bitmapImg.recycle(); return output; }
From source file:Main.java
/** * This method takes a square bitmap and clips it into a circle * * @param bitmap : the image to clip//ww w . ja v a 2 s . co m * @return the clipped bitmap */ public static Bitmap getCroppedBitmap(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.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); 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(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false); //return _bmp; return output; }
From source file:Main.java
/** * Frames the input bitmap in a circle./* w ww .j ava2 s. c o m*/ */ public static Bitmap frameBitmapInCircle(Bitmap input) { if (input == null) { return null; } // Crop the image if not squared. int inputWidth = input.getWidth(); int inputHeight = input.getHeight(); int targetX, targetY, targetSize; if (inputWidth >= inputHeight) { targetX = inputWidth / 2 - inputHeight / 2; targetY = 0; targetSize = inputHeight; } else { targetX = 0; targetY = inputHeight / 2 - inputWidth / 2; targetSize = inputWidth; } // Create an output bitmap and a canvas to draw on it. Bitmap output = Bitmap.createBitmap(targetSize, targetSize, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); // Create a black paint to draw the mask. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.BLACK); // Draw a circle. canvas.drawCircle(targetSize / 2, targetSize / 2, targetSize / 2, paint); // Replace the black parts of the mask with the input image. paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(input, targetX /* left */, targetY /* top */, paint); return output; }
From source file:Main.java
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) { final int reflectionGap = 4; int w = bitmap.getWidth(); int h = bitmap.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(1, -1);// ww w . ja v a2 s .c o m Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w, h / 2, matrix, false); Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2), Config.ARGB_8888); Canvas canvas = new Canvas(bitmapWithReflection); canvas.drawBitmap(bitmap, 0, 0, null); Paint deafalutPaint = new Paint(); canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint); canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null); Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); paint.setShader(shader); // Set the Transfer mode to be porter duff and destination in paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // Draw a rectangle using the paint with our linear gradient canvas.drawRect(0, h, w, bitmapWithReflection.getHeight() + reflectionGap, paint); return bitmapWithReflection; }
From source file:Main.java
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) { if (bitmap == null) return null; Bitmap output;// ww w. java 2 s .c o m try { output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.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 = pixels; 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, rect, rect, paint); } catch (Exception ex) { //adding as a precaution because finding a few crashes in bitmap modification return bitmap; } return output; }
From source file:Main.java
public static Bitmap createReflectionBitmap(Bitmap bitmap) { final int reflectionGap = 4; int width = bitmap.getWidth(); int height = bitmap.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(1, -1);//from ww w .j a v a2s. co m Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false); Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888); Canvas canvas = new Canvas(bitmapWithReflection); canvas.drawBitmap(bitmap, 0, 0, null); Paint deafalutPaint = new Paint(); canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint); canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, Shader.TileMode.CLAMP); paint.setShader(shader); // Set the Transfer mode to be porter duff and destination in paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); // Draw a rectangle using the paint with our linear gradient canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); return bitmapWithReflection; }
From source file:Main.java
public static Bitmap createReflectionBitmap(Bitmap bitmap) { final int reflectionGap = 4; int width = bitmap.getWidth(); int height = bitmap.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(1, -1);//from w w w . j a va 2 s . c o m Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false); Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888); Canvas canvas = new Canvas(bitmapWithReflection); canvas.drawBitmap(bitmap, 0, 0, null); Paint deafalutPaint = new Paint(); canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint); canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); paint.setShader(shader); // Set the Transfer mode to be porter duff and destination in paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // Draw a rectangle using the paint with our linear gradient canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); return bitmapWithReflection; }
From source file:Main.java
public static Bitmap toRoundBitmap(Bitmap bitmap, int round) { if (bitmap == null) return null; 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) { if (round != 0) roundPx = round;/*from ww w .j a v a 2 s. c o m*/ else 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 { if (round != 0) roundPx = round; 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
public static Bitmap createReflectedBitmap(Bitmap srcBitmap, float reflectHeight) { if (null == srcBitmap) { return null; }//w w w . j a va 2 s .co m int srcWidth = srcBitmap.getWidth(); int srcHeight = srcBitmap.getHeight(); int reflectionWidth = srcBitmap.getWidth(); int reflectionHeight = reflectHeight == 0 ? srcHeight / 3 : (int) (reflectHeight * srcHeight); if (0 == srcWidth || srcHeight == 0) { return null; } // The matrix Matrix matrix = new Matrix(); matrix.preScale(1, -1); try { // The reflection bitmap, width is same with original's Bitmap reflectionBitmap = Bitmap.createBitmap(srcBitmap, 0, srcHeight - reflectionHeight, reflectionWidth, reflectionHeight, matrix, false); if (null == reflectionBitmap) { return null; } Canvas canvas = new Canvas(reflectionBitmap); Paint paint = new Paint(); paint.setAntiAlias(true); LinearGradient shader = new LinearGradient(0, 0, 0, reflectionBitmap.getHeight(), 0x70FFFFFF, 0x00FFFFFF, TileMode.MIRROR); paint.setShader(shader); paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN)); // Draw the linear shader. canvas.drawRect(0, 0, reflectionBitmap.getWidth(), reflectionBitmap.getHeight(), paint); return reflectionBitmap; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Given an input bitmap, scales it to the given width/height and makes it round. * * @param input {@link Bitmap} to scale and crop * @param targetWidth desired output width * @param targetHeight desired output height * @return output bitmap scaled to the target width/height and cropped to an oval. The * cropping algorithm will try to fit as much of the input into the output as possible, * while preserving the target width/height ratio. *///from w ww .j av a 2 s. c o m public static Bitmap getRoundedBitmap(Bitmap input, int targetWidth, int targetHeight) { if (input == null) { return null; } final Bitmap.Config inputConfig = input.getConfig(); final Bitmap result = Bitmap.createBitmap(targetWidth, targetHeight, inputConfig != null ? inputConfig : Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(result); final Paint paint = new Paint(); canvas.drawARGB(0, 0, 0, 0); paint.setAntiAlias(true); canvas.drawOval(0, 0, targetWidth, targetHeight, paint); // Specifies that only pixels present in the destination (i.e. the drawn oval) should // be overwritten with pixels from the input bitmap. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); final int inputWidth = input.getWidth(); final int inputHeight = input.getHeight(); // Choose the largest scale factor that will fit inside the dimensions of the // input bitmap. final float scaleBy = Math.min((float) inputWidth / targetWidth, (float) inputHeight / targetHeight); final int xCropAmountHalved = (int) (scaleBy * targetWidth / 2); final int yCropAmountHalved = (int) (scaleBy * targetHeight / 2); final Rect src = new Rect(inputWidth / 2 - xCropAmountHalved, inputHeight / 2 - yCropAmountHalved, inputWidth / 2 + xCropAmountHalved, inputHeight / 2 + yCropAmountHalved); final RectF dst = new RectF(0, 0, targetWidth, targetHeight); canvas.drawBitmap(input, src, dst, paint); return result; }