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
public static Bitmap createWatermark(Bitmap src, Bitmap watermark, int direction, int spacing) { final int w = src.getWidth(); final int h = src.getHeight(); Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(src, 0, 0, null);/*from w w w . j av a2 s .co m*/ if (direction == LEFT_TOP) { canvas.drawBitmap(watermark, spacing, spacing, null); } else if (direction == LEFT_BOTTOM) { canvas.drawBitmap(watermark, spacing, h - watermark.getHeight() - spacing, null); } else if (direction == RIGHT_TOP) { canvas.drawBitmap(watermark, w - watermark.getWidth() - spacing, spacing, null); } else if (direction == RIGHT_BOTTOM) { canvas.drawBitmap(watermark, w - watermark.getWidth() - spacing, h - watermark.getHeight() - spacing, null); } return bitmap; }
From source file:Main.java
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int backgroundColor, int borderColor) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth() + 12, bitmap.getHeight() + 12, Bitmap.Config.ARGB_8888);/*from ww w . ja v a2 s . c o m*/ Canvas canvas = new Canvas(output); //canvas.drawARGB(Color.alpha(backgroundColor), Color.red(backgroundColor), Color.green(backgroundColor), Color.blue(backgroundColor)); Paint borderPaint = new Paint(); borderPaint.setAntiAlias(true); borderPaint.setColor(borderColor); borderPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); borderPaint.setShadowLayer(2.0f, 0.0f, 2.0f, Color.BLACK); int centerWidth = output.getWidth() / 2; int centerHeight = output.getHeight() / 2; canvas.drawCircle(centerWidth, centerHeight, ((centerWidth + centerHeight) / 2) - 4, borderPaint); Paint paint = new Paint(); paint.setAntiAlias(true); Rect rectS = new Rect(0, 0, output.getWidth() - 12, output.getHeight() - 12); Rect rectD = new Rect(0, 0, output.getWidth(), output.getHeight()); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP)); canvas.drawBitmap(bitmap, rectS, rectD, paint); return output; }
From source file:Main.java
public static Bitmap createRoundedBottomBitmap(Context context, int width, int height, int color) { Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(output); //color = 0x80424242; // FIXME final Paint paint = new Paint(); final int roundPxInt = convertDipsToPixels(context, ROUND_DIPS); final Rect rect = new Rect(0, 0, width, height - roundPxInt); final RectF rectF = new RectF(rect); final Rect rectRound = new Rect(roundPxInt, height - roundPxInt, width - roundPxInt, height); final RectF rectFRound = new RectF(rectRound); paint.setAntiAlias(true);/* w ww.j av a 2s . c o m*/ paint.setColor(color); canvas.drawARGB(0, 0, 0, 0); // Corners Rect oval = new Rect(0, height - 2 * roundPxInt, 2 * roundPxInt, height); RectF ovalF = new RectF(oval); canvas.drawArc(ovalF, 90.0f, 90.0f, true, paint); oval = new Rect(width - 2 * roundPxInt, height - 2 * roundPxInt, width, height); ovalF = new RectF(oval); canvas.drawArc(ovalF, 0.0f, 90.0f, true, paint); // Big and small rectangles canvas.drawRect(rectF, paint); canvas.drawRect(rectFRound, paint); return output; }
From source file:Main.java
/** * Method to get a rounded rectange bitmap * @param bitmap/*from w ww .j ava 2s . c o m*/ * @param pixels * @return */ public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) { Bitmap result = null; try { result = Bitmap.createBitmap(150, 150, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(result); int color = 0xff424242; Paint paint = new Paint(); Rect rect = new Rect(0, 0, 200, 200); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawCircle(75, 75, 75, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); } catch (NullPointerException e) { } catch (OutOfMemoryError o) { } return result; }
From source file:Main.java
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) { if (bitmap == null) { return null; }/*from w w w . j ava 2 s . com*/ 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); 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); return output; }
From source file:Main.java
public static android.graphics.Bitmap drawableToBitmap(Drawable drawable) { android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? android.graphics.Bitmap.Config.ARGB_8888 : android.graphics.Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); //canvas.setBitmap(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas);//from w ww.j a va 2s. c o m return bitmap; }
From source file:Main.java
public static Bitmap getRoundAngleImage(Bitmap bitmap, int pixels, boolean recycleOld) { Bitmap output = null;/*from ww w. j a v a 2 s.c om*/ if (bitmap != null && !bitmap.isRecycled()) { 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); paint.setFilterBitmap(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); if (recycleOld) bitmap.recycle(); return output; } return output; }
From source file:Main.java
public static byte[] bmp2bytesStream(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); if (width % 8 != 0) { int adjustWidth = width + (8 - width % 8); final Bitmap.Config config = Bitmap.Config.ARGB_8888; Bitmap whiteBgBitmap = Bitmap.createBitmap(adjustWidth, height, config); Canvas canvas = new Canvas(whiteBgBitmap); canvas.drawColor(Color.WHITE); canvas.drawBitmap(bitmap, 0, 0, null); bitmap = whiteBgBitmap;//from ww w .j a v a2 s .c om width = bitmap.getWidth(); } int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); byte[] image = new byte[width * height]; final byte WHITE = 0, BLACK = 1; for (int i = 0; i < pixels.length; i++) { image[i] = (pixels[i] != 0xFFFFFFFF) ? BLACK : WHITE; } final int COL = width + width % 2; byte[] row = new byte[COL]; byte[] res = new byte[COL / 8 * height]; for (int i = 0, dex = 0, num = 0; i < height; ++i) { System.arraycopy(image, i * width, row, 0, width); for (byte e : row) { res[dex] += e << (7 - num++); num = 8 == num ? 0 : num; dex = 0 == num ? dex + 1 : dex; } } return res; }
From source file:Main.java
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, final float roundPx) { if (bitmap == null) return null; try {//from w ww . j av a 2 s. co m Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight())); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(Color.BLACK); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); final Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); canvas.drawBitmap(bitmap, src, rect, paint); return output; } catch (Exception e) { return bitmap; } }
From source file:Main.java
public static Bitmap getRoundedRectBitmap(Bitmap bitmap) { Bitmap result = null;// www . j a v a 2 s . co m Canvas canvas; Paint paint; try { final int width = bitmap.getWidth(); final int height = bitmap.getHeight(); result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); canvas = new Canvas(result); int color = 0xff424242; float radius = width > height ? width / 2 : height / 2; paint = new Paint(); Rect rect = new Rect(0, 0, width, height); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawCircle(width / 2, height / 2, radius, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); } catch (NullPointerException | OutOfMemoryError e) { e.printStackTrace(); } return result; }