1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chrome.browser.widget;
7 import android.content.res.ColorStateList;
8 import android.content.res.Resources;
9 import android.graphics.Bitmap;
10 import android.graphics.BitmapFactory;
11 import android.graphics.PorterDuff;
12 import android.graphics.drawable.BitmapDrawable;
14 import org.chromium.chrome.R;
17 * Implementation of BitmapDrawable that allows to tint the color of the drawable for all
18 * bitmap drawable states using chrome:tint attribute in XML.
20 public class TintedDrawable extends BitmapDrawable {
22 * The set of colors that just be used for tinting this bitmap drawable.
24 protected ColorStateList mTint;
26 public TintedDrawable(Resources res, Bitmap bitmap) {
28 mTint = res.getColorStateList(R.color.dark_mode_tint);
32 protected boolean onStateChange(int[] state) {
33 boolean ret = updateTintColor();
34 super.onStateChange(state);
39 public boolean isStateful() {
44 * Sets the tint color for the given Drawable for all button states.
45 * @param tint The set of colors to use to color the ImageButton.
47 public void setTint(ColorStateList tint) {
48 if (mTint == tint) return;
53 public static TintedDrawable constructTintedDrawable(Resources res, int drawableId) {
54 Bitmap icon = BitmapFactory.decodeResource(res, drawableId);
55 return new TintedDrawable(res, icon);
58 private boolean updateTintColor() {
59 if (mTint == null) return false;
60 setColorFilter(mTint.getColorForState(getState(), 0), PorterDuff.Mode.SRC_IN);