|
23 | 23 | import android.graphics.Canvas; |
24 | 24 | import android.graphics.Color; |
25 | 25 | import android.graphics.Paint; |
| 26 | +import android.graphics.PorterDuff; |
26 | 27 | import android.graphics.Rect; |
27 | 28 | import android.support.annotation.FloatRange; |
28 | 29 | import android.util.AttributeSet; |
@@ -112,6 +113,16 @@ public ShadowLayout(final Context context, final AttributeSet attrs, final int d |
112 | 113 | } |
113 | 114 | } |
114 | 115 |
|
| 116 | + @Override |
| 117 | + protected void onDetachedFromWindow() { |
| 118 | + super.onDetachedFromWindow(); |
| 119 | + // Clear shadow bitmap |
| 120 | + if (mBitmap != null) { |
| 121 | + mBitmap.recycle(); |
| 122 | + mBitmap = null; |
| 123 | + } |
| 124 | + } |
| 125 | + |
115 | 126 | public boolean isShadowed() { |
116 | 127 | return mIsShadowed; |
117 | 128 | } |
@@ -224,17 +235,27 @@ protected void dispatchDraw(final Canvas canvas) { |
224 | 235 | // paint does`t draw shadow, it draw another copy of bitmap |
225 | 236 | super.dispatchDraw(mCanvas); |
226 | 237 |
|
227 | | - // Draw alpha chanel bitmap of our local canvas |
228 | | - mCanvas.drawBitmap(mBitmap.extractAlpha(), mShadowDx, mShadowDy, mPaint); |
| 238 | + // Get the alpha bounds of bitmap |
| 239 | + final Bitmap extractedAlpha = mBitmap.extractAlpha(); |
| 240 | + // Clear past content content to draw shadow |
| 241 | + mCanvas.drawColor(0, PorterDuff.Mode.CLEAR); |
| 242 | + |
| 243 | + // Draw extracted alpha bounds of our local canvas |
| 244 | + mCanvas.drawBitmap(extractedAlpha, mShadowDx, mShadowDy, mPaint); |
| 245 | + |
| 246 | + // Recycle and clear extracted alpha |
| 247 | + extractedAlpha.recycle(); |
229 | 248 | } else { |
230 | | - // Clear shadow bitmap |
231 | | - mBitmap.recycle(); |
232 | | - mBitmap = null; |
| 249 | + // Create placeholder bitmap when size is zero and wait until new size coming up |
| 250 | + mBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.RGB_565); |
233 | 251 | } |
234 | 252 | } |
235 | 253 |
|
236 | 254 | // Draw shadow bitmap |
237 | | - if (mCanvas != null && mBitmap != null) canvas.drawBitmap(mBitmap, 0.0f, 0.0f, null); |
| 255 | + if (mCanvas != null) { |
| 256 | + if (mBitmap != null && !mBitmap.isRecycled()) |
| 257 | + canvas.drawBitmap(mBitmap, 0.0f, 0.0f, null); |
| 258 | + } |
238 | 259 | } |
239 | 260 |
|
240 | 261 | // Draw child`s |
|
0 commit comments