Skip to content

Commit 0b8d98e

Browse files
committed
Added check width and height of the view before generating the bitmap #6
1 parent 48dbf6a commit 0b8d98e

File tree

1 file changed

+14
-9
lines changed
  • library/src/main/java/io/github/douglasjunior/androidSimpleTooltip

1 file changed

+14
-9
lines changed

library/src/main/java/io/github/douglasjunior/androidSimpleTooltip/OverlayView.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class OverlayView extends View {
5050
private View mAnchorView;
5151
private Bitmap bitmap;
5252
private float offset = 0;
53+
private boolean invalidated = true;
5354

5455
OverlayView(Context context, View anchorView) {
5556
super(context);
@@ -59,20 +60,24 @@ public class OverlayView extends View {
5960

6061
@Override
6162
protected void dispatchDraw(Canvas canvas) {
62-
super.dispatchDraw(canvas);
63-
64-
if (bitmap == null) {
63+
if (invalidated)
6564
createWindowFrame();
66-
}
6765

6866
canvas.drawBitmap(bitmap, 0, 0, null);
6967
}
7068

7169
private void createWindowFrame() {
72-
bitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
70+
final int width = getMeasuredWidth(), height = getMeasuredHeight();
71+
if (width <= 0 || height <= 0)
72+
return;
73+
74+
if (bitmap != null && !bitmap.isRecycled())
75+
bitmap.recycle();
76+
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
77+
7378
Canvas osCanvas = new Canvas(bitmap);
7479

75-
RectF outerRectangle = new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight());
80+
RectF outerRectangle = new RectF(0, 0, width, height);
7681

7782
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
7883
paint.setColor(Color.BLACK);
@@ -91,6 +96,8 @@ private void createWindowFrame() {
9196
RectF oval = new RectF(left - offset, top - offset, left + mAnchorView.getMeasuredWidth() + offset, top + mAnchorView.getMeasuredHeight() + offset);
9297

9398
osCanvas.drawOval(oval, paint);
99+
100+
invalidated = false;
94101
}
95102

96103
@Override
@@ -101,9 +108,7 @@ public boolean isInEditMode() {
101108
@Override
102109
protected void onLayout(boolean changed, int l, int t, int r, int b) {
103110
super.onLayout(changed, l, t, r, b);
104-
if (bitmap != null && !bitmap.isRecycled())
105-
bitmap.recycle();
106-
bitmap = null;
111+
invalidated = true;
107112
}
108113

109114
public View getAnchorView() {

0 commit comments

Comments
 (0)