Skip to content

Commit

Permalink
[TextField] Fix crashes when text field size is too large
Browse files Browse the repository at this point in the history
When we try to draw cutout on text field borders, we draw the stroke on a bitmap first. However if the text field is too large, the bitmap size will be too large and cannot be drawn back to the real canvas, which causes crashes.

Adds a workaround/fallback solution to avoid the crash.

PiperOrigin-RevId: 410523013
  • Loading branch information
drchen authored and paulfthomas committed Nov 29, 2021
1 parent adbcf8c commit cf5df22
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions lib/java/com/google/android/material/textfield/CutoutDrawable.java
Expand Up @@ -96,18 +96,25 @@ protected void drawStrokeShape(@NonNull Canvas canvas) {
super.drawStrokeShape(canvas);
return;
}
Bitmap bitmap =
Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bitmap);

// Draw the stroke on the temporary canvas
super.drawStrokeShape(tempCanvas);

// Draw mask for the cutout.
tempCanvas.drawRect(cutoutBounds, cutoutPaint);

// Draw the temporary canvas back to the original canvas
canvas.drawBitmap(bitmap, 0, 0, null);
try {
Bitmap bitmap =
Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bitmap);

// Draw the stroke on the temporary canvas
super.drawStrokeShape(tempCanvas);

// Draw mask for the cutout.
tempCanvas.drawRect(cutoutBounds, cutoutPaint);

// Draw the temporary canvas back to the original canvas
canvas.drawBitmap(bitmap, 0, 0, null);
} catch (RuntimeException e) {
// Bitmap is too big to draw, directly paint cutout on the canvas.
// TODO(b/205774869): find a better way to only draw part of the stroke to avoid the issue.
super.drawStrokeShape(canvas);
canvas.drawRect(cutoutBounds, cutoutPaint);
}
}

private void preDraw(@NonNull Canvas canvas) {
Expand Down

0 comments on commit cf5df22

Please sign in to comment.