Skip to content

Commit

Permalink
Improve drawable tinting
Browse files Browse the repository at this point in the history
DrawableCompat has issue with tinting the layer drawables on
Android lollipop (21) devices. So, using setColorFilter(color, mode)
method for those devices.
  • Loading branch information
pranavpandey committed Feb 9, 2018
1 parent 520c64a commit c40fe97
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,11 @@ public static void setBackground(@NonNull View view, @Nullable Drawable drawable
@Nullable Drawable drawable, boolean wrap, @NonNull ColorFilter colorFilter) {
if (drawable != null) {
if (wrap) {
drawable = DrawableCompat.wrap(drawable.mutate());
drawable = drawable.mutate();
}

drawable.setColorFilter(colorFilter);

if (!DynamicVersionUtils.isMarshmallow()) {
drawable.invalidateSelf();
}
drawable.invalidateSelf();
}

return drawable;
Expand All @@ -97,20 +94,26 @@ public static void setBackground(@NonNull View view, @Nullable Drawable drawable
@Nullable Drawable drawable, boolean wrap,
@ColorInt int color, @Nullable PorterDuff.Mode mode) {
if (drawable != null) {
if (wrap) {
drawable = DrawableCompat.wrap(drawable.mutate());
if (mode == null) {
mode = PorterDuff.Mode.SRC_IN;
}

if (mode != null) {
DrawableCompat.setTintMode(drawable, mode);
// Handle issue with layer drawables.
if (DynamicVersionUtils.isLollipop(true)) {
if (wrap) {
drawable = drawable.mutate();
drawable.setColorFilter(color, mode);
}
} else {
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);
}
DrawableCompat.setTint(drawable, color);
if (wrap) {
drawable = DrawableCompat.wrap(drawable.mutate());
}

if (!DynamicVersionUtils.isMarshmallow()) {
drawable.invalidateSelf();
DrawableCompat.setTintMode(drawable, mode);
DrawableCompat.setTint(drawable, color);
}

drawable.invalidateSelf();
}

return drawable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,8 @@ public class DynamicWindowUtils {

if (windowManager != null) {
Display display = windowManager.getDefaultDisplay();

if (DynamicVersionUtils.isHoneycombMR2()) {
display.getSize(size);
} else {
size.x = display.getWidth();
size.y = display.getHeight();
}
size.x = display.getWidth();
size.y = display.getHeight();
}

return size;
Expand Down

0 comments on commit c40fe97

Please sign in to comment.