OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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. |
| 4 |
| 5 package org.chromium.chrome.browser.widget; |
| 6 |
| 7 import android.content.res.ColorStateList; |
| 8 import android.content.res.TypedArray; |
| 9 import android.graphics.Canvas; |
| 10 import android.graphics.PorterDuff; |
| 11 import android.support.annotation.Nullable; |
| 12 import android.util.AttributeSet; |
| 13 import android.widget.ImageView; |
| 14 |
| 15 import org.chromium.chrome.R; |
| 16 |
| 17 /** |
| 18 * Utility for tinting an ImageView and its Drawable. |
| 19 * |
| 20 * Example usage in XML: |
| 21 * <ImageViewTinterInstanceOwner |
| 22 * xmlns:android="http://schemas.android.com/apk/res/android" |
| 23 * xmlns:chrome="http://schemas.android.com/apk/res-auto" |
| 24 * chrome:chrometint="@color/light_active_color" /> |
| 25 * |
| 26 * The default style used by the Application will likely cause your Drawable to
be automatically |
| 27 * tinted. To prevent this, set the value of chrome:chrometint to "@null". |
| 28 */ |
| 29 public class ImageViewTinter { |
| 30 /** Classes that own an ImageViewTinter must implement these functions. */ |
| 31 public static interface ImageViewTinterOwner { |
| 32 /** See {@link ImageViewTinter#drawableStateChanged}. */ |
| 33 void drawableStateChanged(); |
| 34 |
| 35 /** See {@link ImageViewTinter#setTint}. */ |
| 36 void setTint(ColorStateList tintList); |
| 37 |
| 38 /** See {@link ImageView#onDraw}. */ |
| 39 void onDraw(Canvas canvas); |
| 40 } |
| 41 |
| 42 private ImageView mImageView; |
| 43 private ColorStateList mTintList; |
| 44 |
| 45 /** |
| 46 * Constructor. Should be called with the AttributeSet and style of the Ima
geView so that XML |
| 47 * attributes for it can be parsed. |
| 48 * @param view ImageView being tinted. |
| 49 * @param attrs AttributeSet that is pulled in from an XML layout. May b
e null. |
| 50 * @param defStyle Style that is pulled in from an XML layout. |
| 51 */ |
| 52 public ImageViewTinter(ImageViewTinterOwner view, @Nullable AttributeSet att
rs, int defStyle) { |
| 53 assert view instanceof ImageView; |
| 54 mImageView = (ImageView) view; |
| 55 |
| 56 // Parse out the attributes from the XML. |
| 57 if (attrs != null) { |
| 58 TypedArray a = mImageView.getContext().obtainStyledAttributes( |
| 59 attrs, R.styleable.TintedImage, defStyle, 0); |
| 60 setTint(a.getColorStateList(R.styleable.TintedImage_chrometint)); |
| 61 a.recycle(); |
| 62 } |
| 63 } |
| 64 |
| 65 /** |
| 66 * Sets the tint color for the given ImageView for all states. |
| 67 * @param tintList The set of colors to use. |
| 68 */ |
| 69 public void setTint(ColorStateList tintList) { |
| 70 if (mTintList == tintList) return; |
| 71 mTintList = tintList; |
| 72 updateTintColor(); |
| 73 } |
| 74 |
| 75 /** Call when the state of the Drawable has changed. */ |
| 76 public void drawableStateChanged() { |
| 77 updateTintColor(); |
| 78 } |
| 79 |
| 80 private void updateTintColor() { |
| 81 if (mImageView.getDrawable() == null) { |
| 82 return; |
| 83 } else if (mTintList == null) { |
| 84 mImageView.clearColorFilter(); |
| 85 return; |
| 86 } |
| 87 |
| 88 int tintColor = mTintList.getColorForState(mImageView.getDrawableState()
, 0); |
| 89 mImageView.setColorFilter(tintColor, PorterDuff.Mode.SRC_IN); |
| 90 } |
| 91 } |
OLD | NEW |