public final class Icon implements Parcelable {
......
private ColorStateList mTintList;
static final PorterDuff.Mode DEFAULT_TINT_MODE = Drawable.DEFAULT_TINT_MODE; // SRC_IN
private PorterDuff.Mode mTintMode = DEFAULT_TINT_MODE;
public Drawable loadDrawable(Context context) {
final Drawable result = loadDrawableInner(context);
if (result != null && (mTintList != null || mTintMode != DEFAULT_TINT_MODE)) {
result.mutate();
result.setTintList(mTintList);
result.setTintMode(mTintMode);
}
return result;
}
......
/**
* Store a color to use whenever this Icon is drawn.
*
* @param tintList as in {@link Drawable#setTintList(ColorStateList)}, null to remove tint
* @return this same object, for use in chained construction
*/
public Icon setTintList(ColorStateList tintList) {
mTintList = tintList;
return this;
}
/**
* Store a blending mode to use whenever this Icon is drawn.
*
* @param mode a blending mode, as in {@link Drawable#setTintMode(PorterDuff.Mode)}, may be null
* @return this same object, for use in chained construction
*/
public Icon setTintMode(PorterDuff.Mode mode) {
mTintMode = mode;
return this;
}
/** @hide */
public boolean hasTint() {
return (mTintList != null) || (mTintMode != DEFAULT_TINT_MODE);
}
}
QSTile是一个接口,接口中定义了类State,BooleanState,SignalState等,BooleanState继承State,SignalState继承BooleanState,各自添加部分属性。State中包括两个重要属性Icon和Tile,同时Tile中还包括Icon属性,这里State中的Icon和Tile中Icon不是一个类,State中的Icon是接口QSTile中的一个抽象类,Tile中的Icon和Tile都是实现了parcelable的类。QSTileImpl类中的内部静态类ResourceIcon继承了抽象类Icon,定义了实际获取图片资源的方法getdrawable()等方法
FreemeMobileDataTile.java
private void handleUpdateState(SignalState state,Object arg){
......
state.value = mConnected;
state.label = mContext.getString(R.string.freeme_data_connection);
state.state = mConnected ? Tile.STATE_ACTIVE :
(cb.airplaneModeEnabled || cb.noSim) ? Tile.STATE_UNAVAILABLE : Tile.STATE_INACTIVE;
state.icon = ResourceIcon.get(R.drawable.freeme_ic_qs_dataconnection);
mTileLabel = state.label;
}
根据state状态进行着色的位置是QSIconViewImpl类,它是抽象类QSIconView的实现类,QSIconView的内容如下
public abstract class QSIconView extends ViewGroup {
public static final int VERSION = 1;
public QSIconView(Context context) {
super(context);
}
public abstract void setIcon(State state);
public abstract void disableAnimation();
public abstract View getIconView();
}
QSIconViewImpl中更新图标状态的方法是updateIcon
protected void updateIcon(ImageView iv, State state) {
final QSTile.Icon icon = state.iconSupplier != null ? state.iconSupplier.get() : state.icon;
if (true) {
boolean shouldAnimate = iv.isShown() && mAnimationEnabled
&& iv.getDrawable() != null;
Drawable d = icon != null
? shouldAnimate ? icon.getDrawable(mContext)
: icon.getInvisibleDrawable(mContext) : null;
int padding = icon != null ? icon.getPadding() : 0;
if (d != null) {
d.setAutoMirrored(false);
d.setLayoutDirection(getLayoutDirection());
}
if (d != null) {
tintColor(mContext, state, d);//给drawable着色的方法
}
if (iv instanceof SlashImageView) {
((SlashImageView) iv).setAnimationEnabled(shouldAnimate);
((SlashImageView) iv).setState(state.slash, d);
} else {
iv.setImageDrawable(d);//给ImageView对象设置Drawable对象
}
iv.setTag(R.id.qs_icon_tag, icon);
iv.setTag(R.id.qs_slash_tag, state.slash);
iv.setPadding(0, padding, 0, padding);
if (d instanceof Animatable2) {
Animatable2 a = (Animatable2) d;
a.start();
if (state.isTransient) {
a.registerAnimationCallback(new AnimationCallback() {
@Override
public void onAnimationEnd(Drawable drawable) {
a.start();
}
});
}
}
}
}
private static final int TINT_COLOR_ON = R.color.freeme_tile_icon_on;
private static final int TINT_COLOR_OFF = R.color.freeme_tile_icon_off;
private static final int TINT_COLOR_UNAVAILABLE = R.color.freeme_tile_icon_unavailable;
private void tintColor(Context context, State state, Drawable d) {
if (state.state == Tile.STATE_UNAVAILABLE) {
d.setTint(context.getResources().getColor(TINT_COLOR_UNAVAILABLE));//根据State的状态给Drawable对象着色
} else {
d.setTint(context.getResources().getColor(
(state != null && state instanceof QSTile.BooleanState && ((QSTile.BooleanState) state).value)
|| (state != null && state.state == Tile.STATE_ACTIVE)
? TINT_COLOR_ON : TINT_COLOR_OFF
));
}
}
public static void setTint(ImageView iv, int color) {//该方法是给ImageView对象着色的方法
iv.setImageTintList(ColorStateList.valueOf(color));
}
View的生成
QSIconViewImpl.java
public QSIconViewImpl(Context context) {
super(context);
final Resources res = context.getResources();
mIconSizePx = res.getDimensionPixelSize(R.dimen.qs_tile_icon_size);
mTilePaddingBelowIconPx = res.getDimensionPixelSize(R.dimen.qs_tile_padding_below_icon);
mIcon = createIcon();
addView(mIcon);
}
protected View createIcon() {
final ImageView icon = new SlashImageView(mContext);//SlashImageView是ImageView的子类
icon.setId(android.R.id.icon);
icon.setScaleType(ScaleType.FIT_CENTER);
return icon;
}