Search in sources :

Example 91 with Canvas

use of android.graphics.Canvas in project robolectric by robolectric.

the class ShadowCanvasTest method visualize_shouldReturnDescription.

@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));
}
Also used : Matrix(android.graphics.Matrix) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) Test(org.junit.Test)

Example 92 with Canvas

use of android.graphics.Canvas in project robolectric by robolectric.

the class ShadowCanvasTest method drawPath_shouldRecordThePathAndThePaint.

@Test
public void drawPath_shouldRecordThePathAndThePaint() throws Exception {
    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.lineTo(10, 10);
    Paint paint = new Paint();
    paint.setColor(Color.RED);
    paint.setAlpha(7);
    canvas.drawPath(path, paint);
    // changing the values on this Paint shouldn't affect recorded painted path
    paint.setColor(Color.BLUE);
    paint.setAlpha(8);
    ShadowCanvas shadow = shadowOf(canvas);
    assertThat(shadow.getPathPaintHistoryCount()).isEqualTo(1);
    ShadowPath drawnPath = shadowOf(shadow.getDrawnPath(0));
    assertEquals(drawnPath.getPoints().get(0), new ShadowPath.Point(10, 10, LINE_TO));
    Paint drawnPathPaint = shadow.getDrawnPathPaint(0);
    assertThat(drawnPathPaint.getColor()).isEqualTo(Color.RED);
    assertThat(drawnPathPaint.getAlpha()).isEqualTo(7);
}
Also used : Path(android.graphics.Path) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) Test(org.junit.Test)

Example 93 with Canvas

use of android.graphics.Canvas in project robolectric by robolectric.

the class ShadowCanvasTest method drawCircle_shouldRecordCirclePaintHistoryEvents.

@Test
public void drawCircle_shouldRecordCirclePaintHistoryEvents() throws Exception {
    Canvas canvas = new Canvas();
    Paint paint0 = new Paint();
    Paint paint1 = new Paint();
    canvas.drawCircle(1, 2, 3, paint0);
    canvas.drawCircle(4, 5, 6, paint1);
    ShadowCanvas shadowCanvas = shadowOf(canvas);
    assertThat(shadowCanvas.getDrawnCircle(0).centerX).isEqualTo(1);
    assertThat(shadowCanvas.getDrawnCircle(0).centerY).isEqualTo(2);
    assertThat(shadowCanvas.getDrawnCircle(0).radius).isEqualTo(3);
    assertThat(shadowCanvas.getDrawnCircle(0).paint).isSameAs(paint0);
    assertThat(shadowCanvas.getDrawnCircle(1).centerX).isEqualTo(4);
    assertThat(shadowCanvas.getDrawnCircle(1).centerY).isEqualTo(5);
    assertThat(shadowCanvas.getDrawnCircle(1).radius).isEqualTo(6);
    assertThat(shadowCanvas.getDrawnCircle(1).paint).isSameAs(paint1);
}
Also used : Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) Test(org.junit.Test)

Example 94 with Canvas

use of android.graphics.Canvas in project robolectric by robolectric.

the class ShadowCanvasTest method shouldDescribeBitmapDrawing_WithMatrix.

@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());
}
Also used : Matrix(android.graphics.Matrix) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) Test(org.junit.Test)

Example 95 with Canvas

use of android.graphics.Canvas in project robolectric by robolectric.

the class ShadowCanvasTest method drawRect_shouldRecordRectHistoryEvents.

@Test
public void drawRect_shouldRecordRectHistoryEvents() throws Exception {
    Canvas canvas = new Canvas();
    Paint paint0 = new Paint();
    paint0.setColor(Color.WHITE);
    Paint paint1 = new Paint();
    paint1.setColor(Color.BLACK);
    RectF rect0 = new RectF(0f, 2f, 3f, 4f);
    RectF rect1 = new RectF(5f, 6f, 7f, 8f);
    canvas.drawRect(0f, 2f, 3f, 4f, paint0);
    canvas.drawRect(5f, 6f, 7f, 8f, paint1);
    ShadowCanvas shadowCanvas = shadowOf(canvas);
    assertThat(shadowCanvas.getDrawnRect(0).left).isEqualTo(0f);
    assertThat(shadowCanvas.getDrawnRect(0).top).isEqualTo(2f);
    assertThat(shadowCanvas.getDrawnRect(0).right).isEqualTo(3f);
    assertThat(shadowCanvas.getDrawnRect(0).bottom).isEqualTo(4f);
    assertThat(shadowCanvas.getDrawnRect(0).rect).isEqualTo(rect0);
    assertThat(shadowCanvas.getDrawnRect(0).paint.getColor()).isEqualTo(Color.WHITE);
    assertThat(shadowCanvas.getDrawnRect(1).left).isEqualTo(5f);
    assertThat(shadowCanvas.getDrawnRect(1).top).isEqualTo(6f);
    assertThat(shadowCanvas.getDrawnRect(1).right).isEqualTo(7f);
    assertThat(shadowCanvas.getDrawnRect(1).bottom).isEqualTo(8f);
    assertThat(shadowCanvas.getDrawnRect(1).rect).isEqualTo(rect1);
    assertThat(shadowCanvas.getDrawnRect(1).paint.getColor()).isEqualTo(Color.BLACK);
}
Also used : RectF(android.graphics.RectF) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) Test(org.junit.Test)

Aggregations

Canvas (android.graphics.Canvas)1237 Bitmap (android.graphics.Bitmap)890 Paint (android.graphics.Paint)609 Rect (android.graphics.Rect)257 BitmapDrawable (android.graphics.drawable.BitmapDrawable)192 RectF (android.graphics.RectF)136 PorterDuffXfermode (android.graphics.PorterDuffXfermode)103 Matrix (android.graphics.Matrix)80 Point (android.graphics.Point)76 Drawable (android.graphics.drawable.Drawable)68 BitmapShader (android.graphics.BitmapShader)60 Test (org.junit.Test)46 IOException (java.io.IOException)43 FileOutputStream (java.io.FileOutputStream)40 View (android.view.View)39 Path (android.graphics.Path)38 File (java.io.File)37 SuppressLint (android.annotation.SuppressLint)36 ColorMatrix (android.graphics.ColorMatrix)36 ColorMatrixColorFilter (android.graphics.ColorMatrixColorFilter)33