Skip to content

Commit ddf0ee2

Browse files
author
Sérgio Moura
committed
Correct the PR comments and fixing init bug
1 parent 462fd08 commit ddf0ee2

File tree

1 file changed

+18
-47
lines changed

1 file changed

+18
-47
lines changed

roundedimageview/src/main/java/com/makeramen/roundedimageview/RoundedImageView.java

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,16 @@ public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
163163
updateDrawableAttrs();
164164
updateBackgroundDrawableAttrs(true);
165165

166-
if(mMutateBackground){
166+
if (mMutateBackground) {
167167
// when setBackground() is called by View constructor, mMutateBackground is not loaded from the attribute,
168168
// so it's false by default, what doesn't allow to create the RoundedDrawable. At this point, after load
169169
// mMutateBackground and updated BackgroundDrawable to RoundedDrawable, the View's background drawable needs to
170170
// be changed to this new drawable.
171171

172+
if (BuildConfig.VERSION_CODE >= Build.VERSION_CODES.JELLY_BEAN)
172173
super.setBackground(mBackgroundDrawable);
174+
else
175+
super.setBackgroundDrawable(mBackgroundDrawable);
173176
}
174177

175178
a.recycle();
@@ -278,27 +281,22 @@ private Drawable resolveResource() {
278281

279282
@Override
280283
public void setBackground(Drawable background) {
281-
mBackgroundDrawable = background;
282-
updateBackgroundDrawableAttrs(true);
283-
if(BuildConfig.VERSION_CODE >= Build.VERSION_CODES.JELLY_BEAN)
284-
super.setBackground(mBackgroundDrawable);
285-
else
286-
super.setBackgroundDrawable(mBackgroundDrawable);
284+
setBackgroundDrawable(background);
287285
}
288286

289287
@Override
290288
public void setBackgroundResource(@DrawableRes int resId) {
291289
if (mBackgroundResource != resId) {
292290
mBackgroundResource = resId;
293291
mBackgroundDrawable = resolveBackgroundResource();
294-
setBackground(mBackgroundDrawable);
292+
setBackgroundDrawable(mBackgroundDrawable);
295293
}
296294
}
297295

298296
@Override
299297
public void setBackgroundColor(int color) {
300298
mBackgroundDrawable = new ColorDrawable(color);
301-
setBackground(mBackgroundDrawable);
299+
setBackgroundDrawable(mBackgroundDrawable);
302300
}
303301

304302
private Drawable resolveBackgroundResource() {
@@ -320,15 +318,15 @@ private Drawable resolveBackgroundResource() {
320318
}
321319

322320
private void updateDrawableAttrs() {
323-
updateAttrs(mDrawable);
321+
updateAttrs(mDrawable, mScaleType);
324322
}
325323

326324
private void updateBackgroundDrawableAttrs(boolean convert) {
327325
if (mMutateBackground) {
328326
if (convert) {
329327
mBackgroundDrawable = RoundedDrawable.fromDrawable(mBackgroundDrawable);
330328
}
331-
updateBackgroundAttrs(mBackgroundDrawable);
329+
updateAttrs(mBackgroundDrawable, ScaleType.FIT_XY);
332330
}
333331
}
334332

@@ -357,12 +355,12 @@ private void applyColorMod() {
357355
}
358356
}
359357

360-
private void updateAttrs(Drawable drawable) {
358+
private void updateAttrs(Drawable drawable, ScaleType scaleType) {
361359
if (drawable == null) { return; }
362360

363361
if (drawable instanceof RoundedDrawable) {
364362
((RoundedDrawable) drawable)
365-
.setScaleType(mScaleType)
363+
.setScaleType(scaleType)
366364
.setBorderWidth(mBorderWidth)
367365
.setBorderColor(mBorderColor)
368366
.setOval(mIsOval)
@@ -382,47 +380,20 @@ private void updateAttrs(Drawable drawable) {
382380
// loop through layers to and set drawable attrs
383381
LayerDrawable ld = ((LayerDrawable) drawable);
384382
for (int i = 0, layers = ld.getNumberOfLayers(); i < layers; i++) {
385-
updateAttrs(ld.getDrawable(i));
386-
}
387-
}
388-
}
389-
390-
private void updateBackgroundAttrs(Drawable drawable) {
391-
if (drawable == null) { return; }
392-
393-
if (drawable instanceof RoundedDrawable) {
394-
((RoundedDrawable) drawable)
395-
//It's expected for a Background to fit the drawable to
396-
//cover all background, so its ScaleType should be FIT_XY
397-
.setScaleType(ScaleType.FIT_XY)
398-
.setBorderWidth(mBorderWidth)
399-
.setBorderColor(mBorderColor)
400-
.setOval(mIsOval)
401-
.setTileModeX(mTileModeX)
402-
.setTileModeY(mTileModeY);
403-
404-
if (mCornerRadii != null) {
405-
((RoundedDrawable) drawable).setCornerRadius(
406-
mCornerRadii[Corner.TOP_LEFT],
407-
mCornerRadii[Corner.TOP_RIGHT],
408-
mCornerRadii[Corner.BOTTOM_RIGHT],
409-
mCornerRadii[Corner.BOTTOM_LEFT]);
410-
}
411-
412-
applyColorMod();
413-
} else if (drawable instanceof LayerDrawable) {
414-
// loop through layers to and set drawable attrs
415-
LayerDrawable ld = ((LayerDrawable) drawable);
416-
for (int i = 0, layers = ld.getNumberOfLayers(); i < layers; i++) {
417-
updateBackgroundAttrs(ld.getDrawable(i));
383+
updateAttrs(ld.getDrawable(i), scaleType);
418384
}
419385
}
420386
}
421387

422388
@Override
423389
@Deprecated
424390
public void setBackgroundDrawable(Drawable background) {
425-
setBackground(background);
391+
mBackgroundDrawable = background;
392+
updateBackgroundDrawableAttrs(true);
393+
if (BuildConfig.VERSION_CODE >= Build.VERSION_CODES.JELLY_BEAN)
394+
super.setBackground(mBackgroundDrawable);
395+
else
396+
super.setBackgroundDrawable(mBackgroundDrawable);
426397
}
427398

428399
/**

0 commit comments

Comments
 (0)