List of usage examples for android.graphics Canvas drawBitmap
public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, @Nullable Paint paint)
From source file:Main.java
public static void drawToCenterOfCanvas(Canvas canvas, Bitmap bp, int max_width, int max_height, Rect cacheRect) {/*from w w w. j av a2s . c om*/ final int b_w = bp.getWidth(); final int b_h = bp.getHeight(); if (b_w <= max_width && b_h <= max_height) { // center aligned canvas.drawBitmap(bp, (max_width - b_w) / 2, (max_height - b_h) / 2, null); } else { // scaled fix given rect size final float s_w = 1.0f * max_width / b_w; final float s_h = 1.0f * max_height / b_h; final float f_s = Math.min(s_w, s_h); final int f_w = (int) (b_w * f_s); final int f_h = (int) (b_h * f_s); cacheRect.set(0, 0, f_w, f_h); cacheRect.offset((max_width - f_w) / 2, (max_height - f_h) / 2); canvas.drawBitmap(bp, null, cacheRect, null); } }
From source file:Main.java
private static Bitmap composeBitmap(Bitmap firstBitmap, Bitmap secondBitmap, int direction) { if (firstBitmap == null) { return null; }//from w ww.j a v a 2 s. com if (secondBitmap == null) { return firstBitmap; } final int fw = firstBitmap.getWidth(); final int fh = firstBitmap.getHeight(); final int sw = secondBitmap.getWidth(); final int sh = secondBitmap.getHeight(); Bitmap bitmap = null; Canvas canvas = null; if (direction == TOP) { bitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh, Config.ARGB_8888); canvas = new Canvas(bitmap); canvas.drawBitmap(secondBitmap, 0, 0, null); canvas.drawBitmap(firstBitmap, 0, sh, null); } else if (direction == BOTTOM) { bitmap = Bitmap.createBitmap(fw > sw ? fw : sw, fh + sh, Config.ARGB_8888); canvas = new Canvas(bitmap); canvas.drawBitmap(firstBitmap, 0, 0, null); canvas.drawBitmap(secondBitmap, 0, fh, null); } else if (direction == LEFT) { bitmap = Bitmap.createBitmap(fw + sw, sh > fh ? sh : fh, Config.ARGB_8888); canvas = new Canvas(bitmap); canvas.drawBitmap(secondBitmap, 0, 0, null); canvas.drawBitmap(firstBitmap, sw, 0, null); } else if (direction == RIGHT) { bitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh, Config.ARGB_8888); canvas = new Canvas(bitmap); canvas.drawBitmap(firstBitmap, 0, 0, null); canvas.drawBitmap(secondBitmap, fw, 0, null); } return bitmap; }
From source file:Main.java
public static Bitmap toGrayscale(Bitmap bmpOriginal) { int width, height; height = bmpOriginal.getHeight();//w w w . j a v a 2 s .c om width = bmpOriginal.getWidth(); Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bmpGrayscale); Paint paint = new Paint(); ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); paint.setColorFilter(f); c.drawBitmap(bmpOriginal, 0, 0, paint); return bmpGrayscale; }
From source file:Main.java
public static Bitmap setGrayscale(Bitmap source) { int width, height; height = source.getHeight();//from w w w . ja va 2s . c om width = source.getWidth(); Bitmap bmpGrayScale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bmpGrayScale); Paint paint = new Paint(); ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); paint.setColorFilter(f); c.drawBitmap(source, 0, 0, paint); return bmpGrayScale; }
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 www . j a va 2s . c om 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 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 ww .j ava2 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 int drawIn(Canvas canvas, Bitmap bitmap, Rect display, int x) { if (bitmap != null) { Rect source = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); display = new Rect(display.left + x, display.top + x, display.right - x, display.bottom - x); canvas.drawBitmap(bitmap, source, display, null); return x + display.width(); }//from w w w. ja v a 2 s.co m return 0; }
From source file:Main.java
/** * clip source bitmap into a circle bitmap * * @param src the source bitmap/*w ww .ja va 2 s. co m*/ * @param recycle whether recycle the source bitmap * @param config bitmap config * @return clipped circle bitmap */ public static Bitmap createCircleBitmap(Bitmap src, boolean recycle, Bitmap.Config config) { if (src == null) { return null; } if (config == null) { config = Bitmap.Config.ARGB_8888; } Bitmap out = Bitmap.createBitmap(src.getWidth(), src.getHeight(), config); final Rect rect = new Rect(0, 0, Math.min(src.getWidth(), src.getHeight()), Math.min(src.getWidth(), src.getHeight())); Paint paint = new Paint(); paint.setAntiAlias(true); Canvas canvas = new Canvas(out); canvas.drawARGB(0, 0, 0, 0); RectF rectF = new RectF(rect); canvas.drawOval(rectF, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(src, rect, rect, paint); if (recycle) { src.recycle(); } return out; }
From source file:Main.java
public static final Bitmap complementedBitmapByAlpha(Bitmap bitmap) { if (null == bitmap) { return null; }//from w ww . j a v a 2s. c o m int width = bitmap.getWidth(); int height = bitmap.getHeight(); if (width == height) { return bitmap; } int len = width > height ? width : height; Bitmap output = Bitmap.createBitmap(len, len, Config.ARGB_8888); Canvas canvas = new Canvas(output); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setAlpha(0); canvas.drawPaint(paint); if (width > height) { int devide = (width - height) / 2; canvas.drawBitmap(bitmap, 0, devide, paint_comm); } else { int devide = (height - width) / 2; canvas.drawBitmap(bitmap, devide, 0, paint_comm); } return output; }
From source file:Main.java
private static Bitmap getResizeBitmap(View view, Bitmap bitmap) { Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasuredWidth() / sScaleFactor), (int) (view.getMeasuredHeight() / sScaleFactor), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(overlay); canvas.translate(-view.getLeft() / sScaleFactor, -view.getTop() / sScaleFactor); canvas.scale(1 / sScaleFactor, 1 / sScaleFactor); Paint paint = new Paint(); paint.setFlags(Paint.FILTER_BITMAP_FLAG); canvas.drawBitmap(bitmap, 0, 0, paint); return overlay; }