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
static public void setImageColor(ImageView view, Bitmap sourceBitmap, int rgbcolor)// ,Bitmap sourceBitmap) { if (sourceBitmap != null) { float R = Color.red(rgbcolor); float G = Color.green(rgbcolor); float B = Color.blue(rgbcolor); Log.v("R:G:B", R + ":" + G + ":" + B); // float[] colorTransform = { R / 255f, 0, 0, 0, 0, // R color 0, G / 255f, 0, 0, 0 // G color , 0, 0, B / 255f, 0, 0 // B color , 0, 0, 0, 1f, 0f };// w ww . j ava 2 s .c om ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0f); // Remove Colour colorMatrix.set(colorTransform); ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix); Paint paint = new Paint(); paint.setColorFilter(colorFilter); Bitmap mutableBitmap = sourceBitmap.copy(Bitmap.Config.ARGB_8888, true); view.setImageBitmap(mutableBitmap); Canvas canvas = new Canvas(mutableBitmap); canvas.drawBitmap(mutableBitmap, 0, 0, paint); } }
From source file:Main.java
public static Bitmap toRoundBitmap(Bitmap bitmap) { 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) { roundPx = width / 2;/* w ww . 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, 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
public static Bitmap resizeAndCropCenter(Bitmap bitmap, int size, boolean recycle) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); if (w == size && h == size) return bitmap; // scale the image so that the shorter side equals to the target; // the longer side will be center-cropped. float scale = (float) size / Math.min(w, h); Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap)); int width = Math.round(scale * bitmap.getWidth()); int height = Math.round(scale * bitmap.getHeight()); Canvas canvas = new Canvas(target); canvas.translate((size - width) / 2f, (size - height) / 2f); canvas.scale(scale, scale);/*from w w w .j ava2 s. c o m*/ Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG); canvas.drawBitmap(bitmap, 0, 0, paint); if (recycle) bitmap.recycle(); return target; }
From source file:Main.java
private static Bitmap createBitmapForFotoMix(Bitmap first, Bitmap second, int direction) { if (first == null) { return null; }// ww w .j a v a2 s .c o m if (second == null) { return first; } int fw = first.getWidth(); int fh = first.getHeight(); int sw = second.getWidth(); int sh = second.getHeight(); Bitmap newBitmap = null; if (direction == LEFT) { newBitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh, Config.ARGB_8888); Canvas canvas = new Canvas(newBitmap); canvas.drawBitmap(first, sw, 0, null); canvas.drawBitmap(second, 0, 0, null); } else if (direction == RIGHT) { newBitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh, Config.ARGB_8888); Canvas canvas = new Canvas(newBitmap); canvas.drawBitmap(first, 0, 0, null); canvas.drawBitmap(second, fw, 0, null); } else if (direction == TOP) { newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh, Config.ARGB_8888); Canvas canvas = new Canvas(newBitmap); canvas.drawBitmap(first, 0, sh, null); canvas.drawBitmap(second, 0, 0, null); } else if (direction == BOTTOM) { newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh, Config.ARGB_8888); Canvas canvas = new Canvas(newBitmap); canvas.drawBitmap(first, 0, 0, null); canvas.drawBitmap(second, 0, fh, null); } return newBitmap; }
From source file:Main.java
public static Bitmap resizeDownAndCropCenter(Bitmap bitmap, int size, boolean recycle) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); int minSide = Math.min(w, h); if (w == h && minSide <= size) return bitmap; size = Math.min(size, minSide); float scale = Math.max((float) size / bitmap.getWidth(), (float) size / bitmap.getHeight()); Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap)); int width = Math.round(scale * bitmap.getWidth()); int height = Math.round(scale * bitmap.getHeight()); Canvas canvas = new Canvas(target); canvas.translate((size - width) / 2f, (size - height) / 2f); canvas.scale(scale, scale);//from w ww . ja va2 s. com Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG); canvas.drawBitmap(bitmap, 0, 0, paint); if (recycle) bitmap.recycle(); return target; }
From source file:Main.java
public static Bitmap text2Bitmap(String text, int color, float size) { if (TextUtils.isEmpty(text)) { return null; }/*w w w . ja va 2s .co m*/ Paint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG); paint.setTextSize(size); paint.setColor(color); paint.setTextAlign(Paint.Align.LEFT); float baseline = -paint.ascent(); int width = (int) (paint.measureText(text) + 0.5f); int height = (int) (baseline + paint.descent() + 0.5f); Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(image); canvas.drawText(text, 0, baseline, paint); return image; }
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;// w w w . j av 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); if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); } return output; }
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 ww w.ja va 2s . com*/ 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
public static Bitmap createRoundCornerBitmap(Bitmap bitmap, float roundPx, boolean lt, boolean rt, boolean lb, boolean rb) { Bitmap roundCornerBitmap = createRoundCornerBitmap(bitmap, roundPx); Canvas canvas = new Canvas(roundCornerBitmap); canvas.drawARGB(0, 0, 0, 0);//from w w w . j a v a2 s.c om int bw = bitmap.getWidth(); int bh = bitmap.getHeight(); int centerW = bw / 2; int centerH = bh / 2; Paint paint = new Paint(); Paint defaultPaint = new Paint(); int color = 0xff424242; paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); paint.setAntiAlias(true); paint.setColor(color); Bitmap cutBitmap = null; Canvas cutCanvas = null; if (!lt) { cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888); cutCanvas = new Canvas(cutBitmap); cutCanvas.drawRect(0, 0, centerW, centerH, defaultPaint); cutCanvas.drawBitmap(bitmap, 0, 0, paint); canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint); cutBitmap.recycle(); } if (!rt) { cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888); cutCanvas = new Canvas(cutBitmap); cutCanvas.drawRect(centerW, 0, bw, centerH, defaultPaint); cutCanvas.drawBitmap(bitmap, 0, 0, paint); canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint); cutBitmap.recycle(); } if (!lb) { cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888); cutCanvas = new Canvas(cutBitmap); cutCanvas.drawRect(0, centerH, centerW, bh, defaultPaint); cutCanvas.drawBitmap(bitmap, 0, 0, paint); canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint); cutBitmap.recycle(); } if (!rb) { cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888); cutCanvas = new Canvas(cutBitmap); cutCanvas.drawRect(centerW, centerH, bw, bh, defaultPaint); cutCanvas.drawBitmap(bitmap, 0, 0, paint); canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint); cutBitmap.recycle(); } return roundCornerBitmap; }
From source file:Main.java
/** * Save jpeg image with background color * @param strFileName Save file path/*from www .j a v a2s.co m*/ * @param bitmap Input bitmap * @param nQuality Jpeg quality for saving * @param nBackgroundColor background color * @return whether success or not */ public static boolean saveBitmapJPEGWithBackgroundColor(String strFileName, Bitmap bitmap, int nQuality, int nBackgroundColor) { boolean bSuccess1 = false; boolean bSuccess2 = false; boolean bSuccess3; File saveFile = new File(strFileName); if (saveFile.exists()) { if (!saveFile.delete()) return false; } int nA = (nBackgroundColor >> 24) & 0xff; // If Background color alpha is 0, Background color substitutes as white if (nA == 0) nBackgroundColor = 0xFFFFFFFF; Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(newBitmap); canvas.drawColor(nBackgroundColor); canvas.drawBitmap(bitmap, rect, rect, new Paint()); // Quality limitation min/max if (nQuality < 10) nQuality = 10; else if (nQuality > 100) nQuality = 100; OutputStream out = null; try { bSuccess1 = saveFile.createNewFile(); } catch (IOException e1) { e1.printStackTrace(); } try { out = new FileOutputStream(saveFile); bSuccess2 = newBitmap.compress(CompressFormat.JPEG, nQuality, out); } catch (Exception e) { e.printStackTrace(); } try { if (out != null) { out.flush(); out.close(); bSuccess3 = true; } else bSuccess3 = false; } catch (IOException e) { e.printStackTrace(); bSuccess3 = false; } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } return (bSuccess1 && bSuccess2 && bSuccess3); }