Skip to content

Commit 3d1e718

Browse files
committed
Add support for animated drawables
1 parent 7163ea3 commit 3d1e718

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

circleimageview/src/main/java/de/hdodenhof/circleimageview/CircleImageView.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import androidx.annotation.ColorInt;
4343
import androidx.annotation.ColorRes;
4444
import androidx.annotation.DrawableRes;
45+
import androidx.annotation.NonNull;
4546
import androidx.annotation.RequiresApi;
4647

4748
@SuppressWarnings("UnusedDeclaration")
@@ -72,6 +73,7 @@ public class CircleImageView extends ImageView {
7273
private int mImageAlpha = DEFAULT_IMAGE_ALPHA;
7374

7475
private Bitmap mBitmap;
76+
private Canvas mBitmapCanvas;
7577

7678
private float mDrawableRadius;
7779
private float mBorderRadius;
@@ -80,6 +82,7 @@ public class CircleImageView extends ImageView {
8082

8183
private boolean mInitialized;
8284
private boolean mRebuildShader;
85+
private boolean mDrawableDirty;
8386

8487
private boolean mBorderOverlay;
8588
private boolean mDisableCircularTransformation;
@@ -148,6 +151,7 @@ public void setAdjustViewBounds(boolean adjustViewBounds) {
148151
}
149152
}
150153

154+
@SuppressLint("CanvasSize")
151155
@Override
152156
protected void onDraw(Canvas canvas) {
153157
if (mDisableCircularTransformation) {
@@ -160,6 +164,13 @@ protected void onDraw(Canvas canvas) {
160164
}
161165

162166
if (mBitmap != null) {
167+
if (mDrawableDirty && mBitmapCanvas != null) {
168+
mDrawableDirty = false;
169+
Drawable drawable = getDrawable();
170+
drawable.setBounds(0, 0, mBitmapCanvas.getWidth(), mBitmapCanvas.getHeight());
171+
drawable.draw(mBitmapCanvas);
172+
}
173+
163174
if (mRebuildShader) {
164175
mRebuildShader = false;
165176

@@ -177,6 +188,12 @@ protected void onDraw(Canvas canvas) {
177188
}
178189
}
179190

191+
@Override
192+
public void invalidateDrawable(@NonNull Drawable dr) {
193+
mDrawableDirty = true;
194+
invalidate();
195+
}
196+
180197
@Override
181198
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
182199
super.onSizeChanged(w, h, oldw, oldh);
@@ -276,6 +293,7 @@ public void setDisableCircularTransformation(boolean disableCircularTransformati
276293

277294
if (disableCircularTransformation) {
278295
mBitmap = null;
296+
mBitmapCanvas = null;
279297
mBitmapPaint.setShader(null);
280298
} else {
281299
initializeBitmap();
@@ -387,6 +405,12 @@ private Bitmap getBitmapFromDrawable(Drawable drawable) {
387405
private void initializeBitmap() {
388406
mBitmap = getBitmapFromDrawable(getDrawable());
389407

408+
if (mBitmap != null && mBitmap.isMutable()) {
409+
mBitmapCanvas = new Canvas(mBitmap);
410+
} else {
411+
mBitmapCanvas = null;
412+
}
413+
390414
if (!mInitialized) {
391415
return;
392416
}

0 commit comments

Comments
 (0)