Source code
package android.support.v7.widget;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v4.view.TintableBackgroundView;
import android.support.v7.appcompat.R;
import android.support.v7.internal.widget.TintManager;
import android.util.AttributeSet;
import android.widget.ImageButton;
public class AppCompatImageButton extends ImageButton implements TintableBackgroundView {
private AppCompatBackgroundHelper mBackgroundTintHelper;
private AppCompatImageHelper mImageHelper;
public AppCompatImageButton(Context context) {
this(context, null);
}
public AppCompatImageButton(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.imageButtonStyle);
}
public AppCompatImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TintManager tintManager = TintManager.get(context);
this.mBackgroundTintHelper = new AppCompatBackgroundHelper(this, tintManager);
this.mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr);
this.mImageHelper = new AppCompatImageHelper(this, tintManager);
this.mImageHelper.loadFromAttributes(attrs, defStyleAttr);
}
public void setImageResource(@DrawableRes int resId) {
this.mImageHelper.setImageResource(resId);
}
public void setBackgroundResource(@DrawableRes int resId) {
super.setBackgroundResource(resId);
if (this.mBackgroundTintHelper != null) {
this.mBackgroundTintHelper.onSetBackgroundResource(resId);
}
}
public void setBackgroundDrawable(Drawable background) {
super.setBackgroundDrawable(background);
if (this.mBackgroundTintHelper != null) {
this.mBackgroundTintHelper.onSetBackgroundDrawable(background);
}
}
public void setSupportBackgroundTintList(@Nullable ColorStateList tint) {
if (this.mBackgroundTintHelper != null) {
this.mBackgroundTintHelper.setSupportBackgroundTintList(tint);
}
}
@Nullable
public ColorStateList getSupportBackgroundTintList() {
return this.mBackgroundTintHelper != null ? this.mBackgroundTintHelper.getSupportBackgroundTintList() : null;
}
public void setSupportBackgroundTintMode(@Nullable Mode tintMode) {
if (this.mBackgroundTintHelper != null) {
this.mBackgroundTintHelper.setSupportBackgroundTintMode(tintMode);
}
}
@Nullable
public Mode getSupportBackgroundTintMode() {
return this.mBackgroundTintHelper != null ? this.mBackgroundTintHelper.getSupportBackgroundTintMode() : null;
}
protected void drawableStateChanged() {
super.drawableStateChanged();
if (this.mBackgroundTintHelper != null) {
this.mBackgroundTintHelper.applySupportBackgroundTint();
}
}
}