1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.v7.widget; 18 19import android.content.Context; 20import android.content.res.ColorStateList; 21import android.graphics.PorterDuff; 22import android.graphics.drawable.Drawable; 23import android.support.annotation.DrawableRes; 24import android.support.annotation.Nullable; 25import android.support.v4.view.TintableBackgroundView; 26import android.support.v7.appcompat.R; 27import android.util.AttributeSet; 28import android.widget.ImageView; 29 30/** 31 * A {@link ImageView} which supports compatible features on older version of the platform, 32 * including: 33 * <ul> 34 * <li>Allows dynamic tint of it background via the background tint methods in 35 * {@link android.support.v4.view.ViewCompat}.</li> 36 * <li>Allows setting of the background tint using {@link R.attr#backgroundTint} and 37 * {@link R.attr#backgroundTintMode}.</li> 38 * </ul> 39 * 40 * <p>This will automatically be used when you use {@link android.widget.ImageView} in your 41 * layouts. You should only need to manually use this class when writing custom views.</p> 42 */ 43public class AppCompatImageView extends ImageView implements TintableBackgroundView { 44 45 private AppCompatBackgroundHelper mBackgroundTintHelper; 46 private AppCompatImageHelper mImageHelper; 47 48 public AppCompatImageView(Context context) { 49 this(context, null); 50 } 51 52 public AppCompatImageView(Context context, AttributeSet attrs) { 53 this(context, attrs, 0); 54 } 55 56 public AppCompatImageView(Context context, AttributeSet attrs, int defStyleAttr) { 57 super(TintContextWrapper.wrap(context), attrs, defStyleAttr); 58 59 final AppCompatDrawableManager drawableManager = AppCompatDrawableManager.get(); 60 61 mBackgroundTintHelper = new AppCompatBackgroundHelper(this, drawableManager); 62 mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr); 63 64 mImageHelper = new AppCompatImageHelper(this, drawableManager); 65 mImageHelper.loadFromAttributes(attrs, defStyleAttr); 66 } 67 68 @Override 69 public void setImageResource(@DrawableRes int resId) { 70 // Intercept this call and instead retrieve the Drawable via the image helper 71 mImageHelper.setImageResource(resId); 72 } 73 74 @Override 75 public void setBackgroundResource(@DrawableRes int resId) { 76 super.setBackgroundResource(resId); 77 if (mBackgroundTintHelper != null) { 78 mBackgroundTintHelper.onSetBackgroundResource(resId); 79 } 80 } 81 82 @Override 83 public void setBackgroundDrawable(Drawable background) { 84 super.setBackgroundDrawable(background); 85 if (mBackgroundTintHelper != null) { 86 mBackgroundTintHelper.onSetBackgroundDrawable(background); 87 } 88 } 89 90 /** 91 * This should be accessed via 92 * {@link android.support.v4.view.ViewCompat#setBackgroundTintList(android.view.View, ColorStateList)} 93 * 94 * @hide 95 */ 96 @Override 97 public void setSupportBackgroundTintList(@Nullable ColorStateList tint) { 98 if (mBackgroundTintHelper != null) { 99 mBackgroundTintHelper.setSupportBackgroundTintList(tint); 100 } 101 } 102 103 /** 104 * This should be accessed via 105 * {@link android.support.v4.view.ViewCompat#getBackgroundTintList(android.view.View)} 106 * 107 * @hide 108 */ 109 @Override 110 @Nullable 111 public ColorStateList getSupportBackgroundTintList() { 112 return mBackgroundTintHelper != null 113 ? mBackgroundTintHelper.getSupportBackgroundTintList() : null; 114 } 115 116 /** 117 * This should be accessed via 118 * {@link android.support.v4.view.ViewCompat#setBackgroundTintMode(android.view.View, PorterDuff.Mode)} 119 * 120 * @hide 121 */ 122 @Override 123 public void setSupportBackgroundTintMode(@Nullable PorterDuff.Mode tintMode) { 124 if (mBackgroundTintHelper != null) { 125 mBackgroundTintHelper.setSupportBackgroundTintMode(tintMode); 126 } 127 } 128 129 /** 130 * This should be accessed via 131 * {@link android.support.v4.view.ViewCompat#getBackgroundTintMode(android.view.View)} 132 * 133 * @hide 134 */ 135 @Override 136 @Nullable 137 public PorterDuff.Mode getSupportBackgroundTintMode() { 138 return mBackgroundTintHelper != null 139 ? mBackgroundTintHelper.getSupportBackgroundTintMode() : null; 140 } 141 142 @Override 143 protected void drawableStateChanged() { 144 super.drawableStateChanged(); 145 if (mBackgroundTintHelper != null) { 146 mBackgroundTintHelper.applySupportBackgroundTint(); 147 } 148 } 149 150 public boolean hasOverlappingRendering() { 151 return mImageHelper.hasOverlappingRendering() && super.hasOverlappingRendering(); 152 } 153} 154