| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.widget; | 5 package org.chromium.chrome.browser.widget; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.res.ColorStateList; | 8 import android.content.res.ColorStateList; |
| 9 import android.content.res.TypedArray; | 9 import android.graphics.Canvas; |
| 10 import android.graphics.PorterDuff; | |
| 11 import android.support.v7.widget.AppCompatImageView; | 10 import android.support.v7.widget.AppCompatImageView; |
| 12 import android.util.AttributeSet; | 11 import android.util.AttributeSet; |
| 13 | 12 |
| 14 import org.chromium.chrome.R; | 13 import org.chromium.chrome.browser.widget.ImageViewTinter.ImageViewTinterOwner; |
| 15 | 14 |
| 16 /** | 15 /** |
| 17 * Implementation of ImageView that allows to tint the color of the image view f
or all | 16 * Implementation of ImageView that allows tinting its Drawable for all states. |
| 18 * image view states using chrome:chrometint attribute in XML. | 17 * For usage, see {@link ImageViewTinter}. |
| 19 */ | 18 */ |
| 20 public class TintedImageView extends AppCompatImageView { | 19 public class TintedImageView extends AppCompatImageView implements ImageViewTint
erOwner { |
| 21 private ColorStateList mTint; | 20 private ImageViewTinter mTinter; |
| 22 | 21 |
| 23 public TintedImageView(Context context) { | 22 public TintedImageView(Context context) { |
| 24 super(context); | 23 this(context, null); |
| 25 } | 24 } |
| 26 | 25 |
| 27 public TintedImageView(Context context, AttributeSet attrs) { | 26 public TintedImageView(Context context, AttributeSet attrs) { |
| 28 super(context, attrs); | 27 this(context, attrs, 0); |
| 29 init(context, attrs, 0); | |
| 30 } | 28 } |
| 31 | 29 |
| 32 public TintedImageView(Context context, AttributeSet attrs, int defStyle) { | 30 public TintedImageView(Context context, AttributeSet attrs, int defStyle) { |
| 33 super(context, attrs, defStyle); | 31 super(context, attrs, defStyle); |
| 34 init(context, attrs, defStyle); | 32 mTinter = new ImageViewTinter(this, attrs, defStyle); |
| 35 } | |
| 36 | |
| 37 private void init(Context context, AttributeSet attrs, int defStyle) { | |
| 38 TypedArray a = context.obtainStyledAttributes( | |
| 39 attrs, R.styleable.TintedImage, defStyle, 0); | |
| 40 setTintInternal(a.getColorStateList(R.styleable.TintedImage_chrometint))
; | |
| 41 a.recycle(); | |
| 42 } | 33 } |
| 43 | 34 |
| 44 @Override | 35 @Override |
| 45 protected void drawableStateChanged() { | 36 public void drawableStateChanged() { |
| 46 super.drawableStateChanged(); | 37 super.drawableStateChanged(); |
| 47 updateTintColor(); | 38 mTinter.drawableStateChanged(); |
| 48 } | 39 } |
| 49 | 40 |
| 50 /** | 41 @Override |
| 51 * Sets the tint color for the given ImageView for all view states. | 42 public void setTint(ColorStateList tintList) { |
| 52 * @param tint The set of colors to use to color the ImageView. | 43 mTinter.setTint(tintList); |
| 53 */ | |
| 54 public void setTint(ColorStateList tint) { | |
| 55 if (mTint == tint) return; | |
| 56 setTintInternal(tint); | |
| 57 updateTintColor(); | |
| 58 } | 44 } |
| 59 | 45 |
| 60 private void setTintInternal(ColorStateList tint) { | 46 @Override |
| 61 mTint = tint; | 47 public void onDraw(Canvas canvas) { |
| 62 } | 48 super.onDraw(canvas); |
| 63 | |
| 64 private void updateTintColor() { | |
| 65 if (mTint == null) { | |
| 66 clearColorFilter(); | |
| 67 return; | |
| 68 } | |
| 69 setColorFilter(mTint.getColorForState(getDrawableState(), 0), PorterDuff
.Mode.SRC_IN); | |
| 70 } | 49 } |
| 71 } | 50 } |
| OLD | NEW |