List of usage examples for android.graphics Canvas Canvas
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) public Canvas(long nativeCanvas)
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 w w .j ava2 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; }
From source file:Main.java
@TargetApi(Build.VERSION_CODES.HONEYCOMB) public static int getColor(final ColorDrawable colorDrawable) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { final Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); colorDrawable.draw(canvas);/*from www . ja v a 2s . c om*/ return bitmap.getPixel(0, 0); } else { return colorDrawable.getColor(); } }
From source file:Main.java
/** * Adds tint effect to target bitmap./*www . j a v a 2 s. co m*/ * * @param src * PNG bitmap to apply tint affect * @param argb * tint color * @return bitmap with tint affect */ public static Bitmap addTintEffect(Bitmap src, int argb) { Paint paint = new Paint(); paint.setColorFilter(new PorterDuffColorFilter(argb, PorterDuff.Mode.SRC_OVER)); Canvas bottomCanvas = new Canvas(src); bottomCanvas.drawBitmap(src, 0, 0, paint); return src; }
From source file:Main.java
public static Bitmap createReflectedBitmap(Bitmap srcBitmap, float reflectHeight) { if (null == srcBitmap) { return null; }/*from ww w . j a v a2 s . c om*/ 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
public static Bitmap roundCornerFromFile(String filePath, int pixels, int size) { try {// ww w . jav 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
private static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); }/*w w w . j av a 2 s. com*/ final Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; }
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 w w .ja v a 2s. 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 formatBitmap(Bitmap bitmap, int width, int height) { int wh = Math.max(bitmap.getWidth(), bitmap.getHeight()); Bitmap mBitmap = Bitmap.createBitmap(wh, wh, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(mBitmap); canvas.drawBitmap(bitmap, -(bitmap.getWidth() - wh) / 2, -(bitmap.getHeight() - wh) / 2, null); bitmap = Bitmap.createScaledBitmap(mBitmap, width, height, true); return bitmap; }
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 . j a v a 2 s.c o 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
/** * Convert the specified view to a drawable, if possible * * @param view the view to convert//from w w w. ja v a2s . c o m * @return the bitmap or {@code null} if the {@code view} is null */ @Nullable public static Bitmap toBitmap(@Nullable final View view) { if (view == null) { return null; } final int width = view.getWidth(); final int height = view.getHeight(); final Bitmap bitmapToExport = Bitmap.createBitmap(width > 0 ? width : DEFAULT_BITMAP_WIDTH, height > 0 ? height : DEFAULT_BITMAP_HEIGHT, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmapToExport); view.draw(canvas); return bitmapToExport; }