下面列出了android.graphics.drawable.Drawable#isStateful() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
final int[] state = getDrawableState();
boolean changed = false;
final Drawable thumbDrawable = mThumbDrawable;
if (thumbDrawable != null && thumbDrawable.isStateful()) {
changed |= thumbDrawable.setState(state);
}
final Drawable trackDrawable = mTrackDrawable;
if (trackDrawable != null && trackDrawable.isStateful()) {
changed |= trackDrawable.setState(state);
}
if (changed) {
invalidate();
}
}
protected void applyTint() {
Drawable drawable = getDrawable();
if (drawable == null)
return;
if (tint != null && tintMode != null) {
Carbon.setTintListMode(drawable, tint, tintMode);
} else {
Carbon.clearTint(drawable);
}
if (drawable.isStateful())
drawable.setState(getDrawableState());
if (tint != null && tint instanceof AnimatedColorStateList)
((AnimatedColorStateList) tint).setState(getDrawableState());
}
/**
* Sets a drawable as the compound button image.
*
* @param drawable the drawable to set
* @attr ref android.R.styleable#CompoundButton_button
*/
public void setButtonDrawable(@Nullable Drawable drawable) {
if (mButtonDrawable != drawable) {
if (mButtonDrawable != null) {
mButtonDrawable.setCallback(null);
unscheduleDrawable(mButtonDrawable);
}
mButtonDrawable = drawable;
if (drawable != null) {
drawable.setCallback(this);
drawable.setLayoutDirection(getLayoutDirection());
if (drawable.isStateful()) {
drawable.setState(getDrawableState());
}
drawable.setVisible(getVisibility() == VISIBLE, false);
setMinHeight(drawable.getIntrinsicHeight());
applyButtonTint();
}
}
}
public void setForeground(Drawable drawable) {
if (foregroundSelector != drawable) {
if (foregroundSelector != null) {
foregroundSelector.setCallback(null);
unscheduleDrawable(foregroundSelector);
}
foregroundSelector = drawable;
if (drawable != null) {
setWillNotDraw(false);
drawable.setCallback(this);
if (drawable.isStateful()) {
drawable.setState(getDrawableState());
}
} else {
setWillNotDraw(true);
}
requestLayout();
invalidate();
}
}
private boolean applySupportDrawableTint() {
Drawable drawable = mDrawableCallback.getDrawable();
if (drawable != null && mTintInfo != null && mTintInfo.mHasTintList) {
Drawable tintDrawable = drawable.mutate();
tintDrawable = DrawableCompat.wrap(tintDrawable);
if (mTintInfo.mHasTintList) {
DrawableCompat.setTintList(tintDrawable, mTintInfo.mTintList);
}
if (mTintInfo.mHasTintMode) {
DrawableCompat.setTintMode(tintDrawable, mTintInfo.mTintMode);
}
if (tintDrawable.isStateful()) {
tintDrawable.setState(mSwitchCompat.getDrawableState());
}
setDrawable(tintDrawable);
if (drawable == tintDrawable) {
tintDrawable.invalidateSelf();
}
return true;
}
return false;
}
public boolean applySupportButtonDrawableTint() {
Drawable buttonDrawable = CompoundButtonCompat.getButtonDrawable(mView);
if (buttonDrawable != null && mCompoundButtonTintInfo != null && mCompoundButtonTintInfo.mHasTintList) {
buttonDrawable = DrawableCompat.wrap(buttonDrawable);
buttonDrawable = buttonDrawable.mutate();
if (mCompoundButtonTintInfo.mHasTintList) {
DrawableCompat.setTintList(buttonDrawable, mCompoundButtonTintInfo.mTintList);
}
if (mCompoundButtonTintInfo.mHasTintMode) {
DrawableCompat.setTintMode(buttonDrawable, mCompoundButtonTintInfo.mTintMode);
}
// The drawable (or one of its children) may not have been
// stateful before applying the tint, so let's try again.
if (buttonDrawable.isStateful()) {
buttonDrawable.setState(mView.getDrawableState());
}
setButtonDrawable(buttonDrawable);
return true;
}
return false;
}
private boolean applySupportBackgroundTint() {
Drawable backgroundDrawable = mView.getBackground();
if (backgroundDrawable != null && mBackgroundTintInfo != null && mBackgroundTintInfo.mHasTintList) {
backgroundDrawable = DrawableCompat.wrap(backgroundDrawable);
backgroundDrawable = backgroundDrawable.mutate();
if (mBackgroundTintInfo.mHasTintList) {
DrawableCompat.setTintList(backgroundDrawable, mBackgroundTintInfo.mTintList);
}
if (mBackgroundTintInfo.mHasTintMode) {
DrawableCompat.setTintMode(backgroundDrawable, mBackgroundTintInfo.mTintMode);
}
if (backgroundDrawable.isStateful()) {
backgroundDrawable.setState(mView.getDrawableState());
}
setBackgroundDrawable(backgroundDrawable);
return true;
}
return false;
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
final Drawable d = mMarginDrawable;
if (d != null && d.isStateful()) {
d.setState(getDrawableState());
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
final Drawable d = mMarginDrawable;
if (d != null && d.isStateful()) {
d.setState(getDrawableState());
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
final Drawable d = mMarginDrawable;
if (d != null && d.isStateful()) {
d.setState(getDrawableState());
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
final Drawable buttonDrawable = mButtonDrawable;
if (buttonDrawable != null && buttonDrawable.isStateful()
&& buttonDrawable.setState(getDrawableState())) {
invalidateDrawable(buttonDrawable);
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
final Drawable d = mMarginDrawable;
if (d != null && d.isStateful()) {
d.setState(getDrawableState());
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
final Drawable d = mMarginDrawable;
if (d != null && d.isStateful()) {
d.setState(getDrawableState());
}
}
public void setProgressDrawable(Drawable d) {
if (mProgressDrawable != d) {
if (mProgressDrawable != null) {
mProgressDrawable.setCallback(null);
unscheduleDrawable(mProgressDrawable);
}
mProgressDrawable = d;
if (d != null) {
d.setCallback(this);
DrawableCompat.setLayoutDirection(d, getLayoutDirection());
if (d.isStateful()) {
d.setState(getDrawableState());
}
// Make sure the ProgressBar is always tall enough
int drawableHeight = d.getMinimumHeight();
if (mMaxHeight < drawableHeight) {
mMaxHeight = drawableHeight;
requestLayout();
}
applyProgressTints();
}
swapCurrentDrawable(d);
postInvalidate();
updateDrawableBounds(getWidth(), getHeight());
updateDrawableState();
doRefreshProgress(android.R.id.progress, mStartProgress, mEndProgress, false, false, false);
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
final Drawable d = mMarginDrawable;
if (d != null && d.isStateful()) {
d.setState(getDrawableState());
}
}
/**
* Supply a Drawable that is to be rendered on top of all of the child
* views in the frame layout. Any padding in the Drawable will be taken
* into account by ensuring that the children are inset to be placed
* inside of the padding area.
*
* @param drawable The Drawable to be drawn on top of the children.
*/
public void setForeground(Drawable drawable) {
if (mWheelForeground != drawable) {
if (mWheelForeground != null) {
mWheelForeground.setCallback(null);
unscheduleDrawable(mWheelForeground);
}
mWheelForeground = drawable;
if (drawable != null) {
setWillNotDraw(false);
drawable.setCallback(this);
if (drawable.isStateful()) {
drawable.setState(getDrawableState());
}
if (mForegroundGravity == Gravity.FILL) {
Rect padding = new Rect();
drawable.getPadding(padding);
}
} else {
setWillNotDraw(true);
}
requestLayout();
invalidate();
}
}
/**
* Supply a Drawable that is to be rendered on top of all of the child
* views in the frame layout. Any padding in the Drawable will be taken
* into account by ensuring that the children are inset to be placed
* inside of the padding area.
*
* @param drawable The Drawable to be drawn on top of the children.
*/
public void setForeground(View view, Drawable drawable) {
if (view != null) {
if (mForeground != drawable) {
if (mForeground != null) {
mForeground.setCallback(null);
view.unscheduleDrawable(mForeground);
}
mForeground = drawable;
if (drawable != null) {
view.setWillNotDraw(false);
drawable.setCallback(view);
if (drawable.isStateful()) {
drawable.setState(view.getDrawableState());
}
if (mForegroundGravity == Gravity.FILL) {
Rect padding = new Rect();
drawable.getPadding(padding);
}
//update bounds
updateBounds(view, drawable);//added by song
} else {
view.setWillNotDraw(true);
}
view.requestLayout();
view.invalidate();
}
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
final Drawable d = mMarginDrawable;
if (d != null && d.isStateful()) {
d.setState(getDrawableState());
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
final Drawable d = mMarginDrawable;
if (d != null && d.isStateful()) {
d.setState(getDrawableState());
}
}
protected void setDrawableState(final Drawable drawable, final int[] drawableState) {
if (null != drawable && drawable.isStateful() && drawable.setState(drawableState)) {
invalidateDrawable(drawable);
}
}