private static void applyMatrix(@NonNull Bitmap inBitmap, @NonNull Bitmap targetBitmap, Matrix matrix) { BITMAP_DRAWABLE_LOCK.lock(); try { Canvas canvas = new Canvas(targetBitmap); canvas.drawBitmap(inBitmap, matrix, DEFAULT_PAINT); clear(canvas); } finally { BITMAP_DRAWABLE_LOCK.unlock(); } }
private Bitmap createBitmap(@NonNull ReflowData data, @NonNull Layout layout) { Bitmap bitmap = Bitmap.createBitmap( data.bounds.width(), data.bounds.height(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.translate(data.textPosition.x, data.textPosition.y); layout.draw(canvas); return bitmap; }
private static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int roundPixels, Rect srcRect, Rect destRect, int width, int height) { Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(output); final Paint paint = new Paint(); final RectF destRectF = new RectF(destRect); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(0xFF000000); canvas.drawRoundRect(destRectF, roundPixels, roundPixels, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, srcRect, destRectF, paint); return output; } }
/** * For testing purposes only. DO NOT USE IN PRODUCTION. */ public static Bitmap renderPath(Path path) { RectF bounds = new RectF(); path.computeBounds(bounds, false); Bitmap bitmap = Bitmap.createBitmap((int) bounds.right, (int) bounds.bottom, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new LPaint(); paint.setAntiAlias(true); paint.setColor(Color.BLUE); canvas.drawPath(path, paint); return bitmap; } }
private static Bitmap getAlphaSafeBitmap( @NonNull BitmapPool pool, @NonNull Bitmap maybeAlphaSafe) { Bitmap.Config safeConfig = getAlphaSafeConfig(maybeAlphaSafe); if (safeConfig.equals(maybeAlphaSafe.getConfig())) { return maybeAlphaSafe; } Bitmap argbBitmap = pool.get(maybeAlphaSafe.getWidth(), maybeAlphaSafe.getHeight(), safeConfig); new Canvas(argbBitmap).drawBitmap(maybeAlphaSafe, 0 /*left*/, 0 /*top*/, null /*paint*/); // We now own this Bitmap. It's our responsibility to replace it in the pool outside this method // when we're finished with it. return argbBitmap; }
private Bitmap createRect(int color, int width, int height, Bitmap.Config config) { final Bitmap result = Bitmap.createBitmap(width, height, config); Canvas canvas = new Canvas(result); canvas.drawColor(color); return result; } }
/** * Returns a textual representation of the appearance of the object. * * @param view the view to visualize * @return Textual representation of the appearance of the object. */ public static String visualize(View view) { Canvas canvas = new Canvas(); view.draw(canvas); ShadowCanvas shadowCanvas = Shadow.extract(canvas); return shadowCanvas.getDescription(); }
private Bitmap createSolidRedBitmap(int width, int height) { Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setColor(Color.RED); Rect rect = new Rect(0, 0, width, height); canvas.drawRect(rect, paint); return result; } }
private Bitmap createBitmapWithRedCircle(int width, int height) { int minEdge = Math.min(width, height); float radius = minEdge / 2f; Bitmap result = Bitmap.createBitmap(minEdge, minEdge, Bitmap.Config.ARGB_8888); result.setHasAlpha(true); Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.RED); canvas.drawCircle(radius, radius, radius, paint); return result; }
@Test public void visualize_shouldReturnDescription() throws Exception { Canvas canvas = new Canvas(targetBitmap); canvas.drawBitmap(imageBitmap, new Matrix(), new Paint()); canvas.drawBitmap(imageBitmap, new Matrix(), new Paint()); assertEquals("Bitmap for file:/an/image.jpg transformed by Matrix[pre=[], set={}, post=[]]\n" + "Bitmap for file:/an/image.jpg transformed by Matrix[pre=[], set={}, post=[]]", ShadowCanvas.visualize(canvas)); }
@Test public void getArcHistoryCount_shouldReturnTotalNumberOfDrawArcEvents() throws Exception { Canvas canvas = new Canvas(); canvas.drawArc(new RectF(), 0f, 0f, true, new Paint()); canvas.drawArc(new RectF(), 0f, 0f, true, new Paint()); ShadowCanvas shadowCanvas = shadowOf(canvas); assertThat(shadowCanvas.getArcPaintHistoryCount()).isEqualTo(2); }
@Test public void shouldDescribeBitmapDrawing_withDestinationRect() throws Exception { Canvas canvas = new Canvas(targetBitmap); canvas.drawBitmap(imageBitmap, new Rect(1,2,3,4), new Rect(5,6,7,8), new Paint()); assertEquals("Bitmap for file:/an/image.jpg at (5,6) with height=2 and width=2 taken from Rect(1, 2 - 3, 4)", shadowOf(canvas).getDescription()); }
@Test public void shouldDescribeBitmapDrawing_withDestinationRectF() throws Exception { Canvas canvas = new Canvas(targetBitmap); canvas.drawBitmap(imageBitmap, new Rect(1,2,3,4), new RectF(5.0f,6.0f,7.5f,8.5f), new Paint()); assertEquals("Bitmap for file:/an/image.jpg at (5.0,6.0) with height=2.5 and width=2.5 taken from Rect(1, 2 - 3, 4)", shadowOf(canvas).getDescription()); }
@SuppressWarnings("ResultOfMethodCallIgnored") @Test public void testShouldDrawFirstFrameBeforeAnyFrameRead() { Canvas canvas = new Canvas(); drawable.draw(canvas); BitmapTrackingShadowCanvas shadowCanvas = Shadow.extract(canvas); assertThat(shadowCanvas.getDrawnBitmaps()).containsExactly(firstFrame); }
@Test public void getOvalHistoryCount_shouldReturnTotalNumberOfDrawOvalEvents() throws Exception { Canvas canvas = new Canvas(); canvas.drawOval(new RectF(), new Paint()); canvas.drawOval(new RectF(), new Paint()); ShadowCanvas shadowCanvas = shadowOf(canvas); assertThat(shadowCanvas.getOvalPaintHistoryCount()).isEqualTo(2); }
@Test public void shouldDescribeBitmapDrawing_WithMatrix() throws Exception { Canvas canvas = new Canvas(targetBitmap); canvas.drawBitmap(imageBitmap, new Matrix(), new Paint()); canvas.drawBitmap(imageBitmap, new Matrix(), new Paint()); assertEquals("Bitmap for file:/an/image.jpg transformed by Matrix[pre=[], set={}, post=[]]\n" + "Bitmap for file:/an/image.jpg transformed by Matrix[pre=[], set={}, post=[]]", shadowOf(canvas).getDescription()); assertEquals("Bitmap for file:/an/image.jpg transformed by Matrix[pre=[], set={}, post=[]]\n" + "Bitmap for file:/an/image.jpg transformed by Matrix[pre=[], set={}, post=[]]", shadowOf(targetBitmap).getDescription()); }
@Test public void resetCanvasHistory_shouldClearTheHistoryAndDescription() throws Exception { Canvas canvas = new Canvas(); canvas.drawPath(new Path(), new Paint()); canvas.drawText("hi", 1, 2, new Paint()); ShadowCanvas shadow = shadowOf(canvas); shadow.resetCanvasHistory(); assertThat(shadow.getPathPaintHistoryCount()).isEqualTo(0); assertThat(shadow.getTextHistoryCount()).isEqualTo(0); assertEquals("", shadow.getDescription()); }
@Test public void shouldDescribeBitmapDrawing() throws Exception { Canvas canvas = new Canvas(targetBitmap); canvas.drawBitmap(imageBitmap, 1, 2, new Paint()); canvas.drawBitmap(imageBitmap, 100, 200, new Paint()); assertEquals("Bitmap for file:/an/image.jpg at (1,2)\n" + "Bitmap for file:/an/image.jpg at (100,200)", shadowOf(canvas).getDescription()); assertEquals("Bitmap for file:/an/image.jpg at (1,2)\n" + "Bitmap for file:/an/image.jpg at (100,200)", shadowOf(targetBitmap).getDescription()); }
@Test public void draw_shouldCopyDescriptionToCanvas() throws Exception { BitmapDrawable drawable = (BitmapDrawable) resources.getDrawable(R.drawable.an_image); Canvas canvas = new Canvas(); drawable.draw(canvas); assertThat(shadowOf(canvas).getDescription()).isEqualTo("Bitmap for resource:org.robolectric:drawable/an_image"); }
@Test public void shouldGetAndSetHeightAndWidth() throws Exception { Canvas canvas = new Canvas(); shadowOf(canvas).setWidth(99); shadowOf(canvas).setHeight(42); assertEquals(99, canvas.getWidth()); assertEquals(42, canvas.getHeight()); }