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
/** * Method to overlay a color on a gray scale Bitmap, passed as a resource id {@code id}. * If you want to call this method from a Fragment/Activity call it in this way: * overlayColorOnGrayScale(getResources(), R.drawable.your_image, Color.RED) * * @param res A reference to Resources. * @param id The id of a drawable image. * @param color Color to overlay.//from ww w . j a v a 2 s.co m * @return A colored gray scale Bitmap. * @throws IOException */ public static Bitmap overlayColorOnGrayScale(Resources res, int id, int color) throws IOException { Bitmap mutableBitmap = getMutableBitmap(res, id); Canvas canvas = new Canvas(mutableBitmap); canvas.drawBitmap(mutableBitmap, 0, 0, getGrayScalePaint()); Paint paint = new Paint(); paint.setAntiAlias(true); ColorFilter filter = new LightingColorFilter(color, 1); paint.setColorFilter(filter); canvas.drawBitmap(mutableBitmap, 0, 0, paint); return mutableBitmap; }
From source file:Main.java
/** * Create a book cover with the specified bitmap. This method applies several * lighting effects to the original bitmap and returns a new decored bitmap. * * @param bitmap The bitmap to decor with lighting effects * @param width The target width of the decored bitmap * @param height The target height of the decored bitmap * * @return A new Bitmap based on the original bitmap *///from w ww.java 2 s . c o m public static Bitmap createBookCover(Bitmap bitmap, int width, int height) { final int bitmapWidth = bitmap.getWidth(); final int bitmapHeight = bitmap.getHeight(); final float scale = Math.min((float) width / (float) bitmapWidth, (float) height / (float) bitmapHeight); final int scaledWidth = (int) (bitmapWidth * scale); final int scaledHeight = (int) (bitmapHeight * scale); final Bitmap decored = createScaledBitmap(bitmap, scaledWidth, scaledHeight, SHADOW_RADIUS, true, SHADOW_PAINT); final Canvas canvas = new Canvas(decored); canvas.translate(SHADOW_RADIUS / 2.0f, SHADOW_RADIUS / 2.0f); canvas.drawRect(EDGE_START, 0.0f, EDGE_END, scaledHeight, EDGE_PAINT); canvas.drawRect(FOLD_START, 0.0f, FOLD_END, scaledHeight, FOLD_PAINT); //noinspection PointlessArithmeticExpression canvas.translate(scaledWidth - (EDGE_END - EDGE_START), 0.0f); canvas.drawRect(EDGE_START, 0.0f, EDGE_END, scaledHeight, END_EDGE_PAINT); return decored; }
From source file:Main.java
public static Bitmap formatBitmap(Bitmap bitmap, int size) { float width = bitmap.getWidth(); float height = bitmap.getHeight(); float rota = size / height; int widthnew = (int) ((size * width) / height); // int wh = Math.max(bitmap.getWidth(), bitmap.getHeight()); Bitmap mBitmap = Bitmap.createBitmap(widthnew, size, Bitmap.Config.ARGB_8888); Matrix matrix = new Matrix(); matrix.postScale(rota, rota);// ww w . j a v a2s . c o m Canvas canvas = new Canvas(mBitmap); canvas.drawBitmap(bitmap, matrix, null); // bitmap = Bitmap.createScaledBitmap(mBitmap, width, height, true); return mBitmap; }
From source file:Main.java
/** * This is only used when the launcher shortcut is created. * //www . j ava 2 s .c om * @param bitmap The artist, album, genre, or playlist image that's going to * be cropped. * @param size The new size. * @return A {@link Bitmap} that has been resized and cropped for a launcher * shortcut. */ public static final Bitmap resizeAndCropCenter(final Bitmap bitmap, final int size) { Bitmap blurbitmap = null; final int w = bitmap.getWidth(); final int h = bitmap.getHeight(); if (w == size && h == size) { return bitmap; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos); byte[] bitmapdata = bos.toByteArray(); ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata); try { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 8; options.inJustDecodeBounds = true; blurbitmap = BitmapFactory.decodeStream(bs, null, options); options.inJustDecodeBounds = false; } catch (Exception e) { e.printStackTrace(); } finally { if (bs != null) { try { bs.close(); } catch (IOException e) { e.printStackTrace(); } } } final float mScale = (float) size / Math.min(w, h); final Bitmap mTarget = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); final int mWidth = Math.round(mScale * blurbitmap.getWidth()); final int mHeight = Math.round(mScale * blurbitmap.getHeight()); final Canvas mCanvas = new Canvas(mTarget); mCanvas.translate((size - mWidth) / 2f, (size - mHeight) / 2f); mCanvas.scale(mScale, mScale); final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG); mCanvas.drawBitmap(bitmap, 0, 0, paint); return mTarget; }
From source file:com.kabootar.GlassMemeGenerator.ImageOverlay.java
public static Bitmap overlay(Bitmap image, String topCaption, String bottomCaption, Context baseContext) throws IOException, InterruptedException { Canvas graphics = new Canvas(image); graphics.drawBitmap(image, 0, 0, null); drawStringCentered(graphics, topCaption, image, true, baseContext); graphics.drawBitmap(image, 0, 0, null); drawStringCentered(graphics, bottomCaption, image, false, baseContext); return image; }
From source file:Main.java
public static Bitmap toRoundBitmap(Bitmap bitmap, int roundPx) { if (bitmap == null) { return null; }/* w w w.java 2 s . c o m*/ int width = bitmap.getWidth(); int height = bitmap.getHeight(); float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { 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
/** * Draw input bitmap in shadow color and offset. Assumes offset is positive. * * @param inBitmap// w w w . ja va 2s . co m * @param offsetX * @param offsetY * @param blurRadius Not used * @param shadowColor * @return */ public static Bitmap shadowBitmap(Bitmap inBitmap, int offsetX, int offsetY, float blurRadius, int shadowColor) { int imgWidth = inBitmap.getWidth(); int imgHeight = inBitmap.getHeight(); Bitmap bottomBm = Bitmap.createBitmap(imgWidth, imgHeight, Bitmap.Config.ARGB_8888); Canvas bottomCanvas = new Canvas(bottomBm); // bottomCanvas.drawColor(shadowColor); Paint bottomPaint = new Paint(Paint.ANTI_ALIAS_FLAG); bottomPaint.setColor(shadowColor); bottomCanvas.drawRect(offsetX, offsetY, imgWidth, imgHeight, bottomPaint); // MaskFilter filter = new BlurMaskFilter(Math.max(0.5f, blurRadius), BlurMaskFilter.Blur.NORMAL); bottomPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP)); // bottomPaint.setShadowLayer(blurRadius, offsetX, offsetX, shadowColor); // bottomPaint.setMaskFilter(filter); bottomCanvas.drawBitmap(inBitmap, offsetX, offsetY, bottomPaint); return bottomBm; }
From source file:de.treichels.hott.ui.android.html.AndroidCurveImageGenerator.java
private Bitmap getBitmap(final Curve curve, final float scale, final boolean description) { final boolean pitchCurve = curve.getPoint()[0].getPosition() == 0; final float scale1 = scale * 0.75f; // smaller images on the android // platform//from ww w.j a v a2 s. c om final Bitmap image = Bitmap.createBitmap((int) (10 + 200 * scale1), (int) (10 + 250 * scale1), Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(image); final Paint backgroundPaint = new Paint(); backgroundPaint.setColor(Color.WHITE); backgroundPaint.setStyle(Style.FILL); final Paint forgroundPaint = new Paint(); forgroundPaint.setColor(Color.BLACK); forgroundPaint.setStyle(Style.STROKE); forgroundPaint.setStrokeWidth(1.0f); forgroundPaint.setStrokeCap(Cap.BUTT); forgroundPaint.setStrokeJoin(Join.ROUND); forgroundPaint.setStrokeMiter(0.0f); final Paint curvePaint = new Paint(forgroundPaint); curvePaint.setFlags(Paint.ANTI_ALIAS_FLAG); curvePaint.setStrokeWidth(2.0f); final Paint pointPaint = new Paint(curvePaint); pointPaint.setStrokeWidth(5.0f); pointPaint.setStyle(Style.FILL_AND_STROKE); final Paint helpLinePaint = new Paint(forgroundPaint); helpLinePaint.setColor(Color.GRAY); helpLinePaint.setPathEffect(new DashPathEffect(new float[] { 5.0f, 5.0f }, 2.5f)); final Paint textPaint = new Paint(forgroundPaint); textPaint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD)); textPaint.setTextSize(12.0f); textPaint.setTextAlign(Align.CENTER); textPaint.setStyle(Style.FILL); canvas.drawRect(0, 0, 10 + 200 * scale1, 10 + 250 * scale1, backgroundPaint); canvas.drawRect(5, 5, 5 + 200 * scale1, 5 + 250 * scale1, forgroundPaint); canvas.drawLine(5, 5 + 25 * scale1, 5 + 200 * scale1, 5 + 25 * scale1, helpLinePaint); canvas.drawLine(5, 5 + 225 * scale1, 5 + 200 * scale1, 5 + 225 * scale1, helpLinePaint); if (!pitchCurve) { canvas.drawLine(5, 5 + 125 * scale1, 5 + 200 * scale1, 5 + 125 * scale1, helpLinePaint); canvas.drawLine(5 + 100 * scale1, 5, 5 + 100 * scale1, 5 + 250 * scale1, helpLinePaint); } if (curve.getPoint() != null) { int numPoints = 0; for (final CurvePoint p : curve.getPoint()) { if (p.isEnabled()) { numPoints++; } } final double[] xVals = new double[numPoints]; final double[] yVals = new double[numPoints]; int i = 0; for (final CurvePoint p : curve.getPoint()) { if (p.isEnabled()) { if (i == 0) { xVals[i] = pitchCurve ? 0 : -100; } else if (i == numPoints - 1) { xVals[i] = 100; } else { xVals[i] = p.getPosition(); } yVals[i] = p.getValue(); if (description) { float x0; float y0; if (pitchCurve) { x0 = (float) (5 + xVals[i] * 2 * scale1); y0 = (float) (5 + (225 - yVals[i] * 2) * scale1); } else { x0 = (float) (5 + (100 + xVals[i]) * scale1); y0 = (float) (5 + (125 - yVals[i]) * scale1); } canvas.drawPoint(x0, y0, pointPaint); if (y0 < 5 + 125 * scale1) { canvas.drawRect(x0 - 4, y0 + 5, x0 + 3, y0 + 18, backgroundPaint); canvas.drawText(Integer.toString(p.getNumber() + 1), x0 - 1, y0 + 16, textPaint); } else { canvas.drawRect(x0 - 4, y0 - 5, x0 + 3, y0 - 18, backgroundPaint); canvas.drawText(Integer.toString(p.getNumber() + 1), x0 - 1, y0 - 7, textPaint); } } i++; } } if (numPoints > 2 && curve.isSmoothing()) { final SplineInterpolator s = new SplineInterpolator(); final PolynomialSplineFunction function = s.interpolate(xVals, yVals); float x0 = 5; float y0; if (pitchCurve) { y0 = (float) (5 + (225 - yVals[0] * 2) * scale1); } else { y0 = (float) (5 + (125 - yVals[0]) * scale1); } while (x0 < 4 + 200 * scale1) { final float x1 = x0 + 1; float y1; if (pitchCurve) { y1 = (float) (5 + (225 - function.value((x1 - 5) / scale1 / 2) * 2) * scale1); } else { y1 = (float) (5 + (125 - function.value((x1 - 5) / scale1 - 100)) * scale1); } canvas.drawLine(x0, y0, x1, y1, curvePaint); x0 = x1; y0 = y1; } } else { for (i = 0; i < numPoints - 1; i++) { float x0, y0, x1, y1; if (pitchCurve) { x0 = (float) (5 + xVals[i] * 2 * scale1); y0 = (float) (5 + (225 - yVals[i] * 2) * scale1); x1 = (float) (5 + xVals[i + 1] * 2 * scale1); y1 = (float) (5 + (225 - yVals[i + 1] * 2) * scale1); } else { x0 = (float) (5 + (100 + xVals[i]) * scale1); y0 = (float) (5 + (125 - yVals[i]) * scale1); x1 = (float) (5 + (100 + xVals[i + 1]) * scale1); y1 = (float) (5 + (125 - yVals[i + 1]) * scale1); } canvas.drawLine(x0, y0, x1, y1, curvePaint); } } } return image; }
From source file:com.sebible.cordova.videosnapshot.VideoSnapshot.java
private void drawTimestamp(Bitmap bm, String prefix, long timeMs, int textSize) { float w = bm.getWidth(), h = bm.getHeight(); float size = (float) (textSize * bm.getWidth()) / 1280; float margin = (float) (w < h ? w : h) * 0.05f; Canvas c = new Canvas(bm); Paint p = new Paint(); p.setColor(Color.WHITE);/*w ww . j a v a 2s .c o m*/ p.setStrokeWidth((int) (size / 10)); p.setTextSize((int) size); // Text Size p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern long second = (timeMs / 1000) % 60; long minute = (timeMs / (1000 * 60)) % 60; long hour = (timeMs / (1000 * 60 * 60)) % 24; String text = String.format("%s %02d:%02d:%02d", prefix, hour, minute, second); Rect r = new Rect(); p.getTextBounds(text, 0, text.length(), r); //c.drawBitmap(originalBitmap, 0, 0, paint); c.drawText(text, bm.getWidth() - r.width() - margin, bm.getHeight() - r.height() - margin, p); }
From source file:Main.java
public static Bitmap toRoundBitmap(Bitmap bitmap, int roundPx) { if (bitmap == null) { return null; }//from www. j ava 2 s. c o m int width = bitmap.getWidth(); int height = bitmap.getHeight(); float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { 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; }