Skip to content

Commit cf5df22

Browse files
drchenpaulfthomas
authored andcommitted
[TextField] Fix crashes when text field size is too large
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
1 parent adbcf8c commit cf5df22

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

lib/java/com/google/android/material/textfield/CutoutDrawable.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,25 @@ protected void drawStrokeShape(@NonNull Canvas canvas) {
9696
super.drawStrokeShape(canvas);
9797
return;
9898
}
99-
Bitmap bitmap =
100-
Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
101-
Canvas tempCanvas = new Canvas(bitmap);
102-
103-
// Draw the stroke on the temporary canvas
104-
super.drawStrokeShape(tempCanvas);
105-
106-
// Draw mask for the cutout.
107-
tempCanvas.drawRect(cutoutBounds, cutoutPaint);
108-
109-
// Draw the temporary canvas back to the original canvas
110-
canvas.drawBitmap(bitmap, 0, 0, null);
99+
try {
100+
Bitmap bitmap =
101+
Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
102+
Canvas tempCanvas = new Canvas(bitmap);
103+
104+
// Draw the stroke on the temporary canvas
105+
super.drawStrokeShape(tempCanvas);
106+
107+
// Draw mask for the cutout.
108+
tempCanvas.drawRect(cutoutBounds, cutoutPaint);
109+
110+
// Draw the temporary canvas back to the original canvas
111+
canvas.drawBitmap(bitmap, 0, 0, null);
112+
} catch (RuntimeException e) {
113+
// Bitmap is too big to draw, directly paint cutout on the canvas.
114+
// TODO(b/205774869): find a better way to only draw part of the stroke to avoid the issue.
115+
super.drawStrokeShape(canvas);
116+
canvas.drawRect(cutoutBounds, cutoutPaint);
117+
}
111118
}
112119

113120
private void preDraw(@NonNull Canvas canvas) {

0 commit comments

Comments
 (0)