下面列出了android.graphics.drawable.Drawable#setState() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Drawable applySupportCompoundDrawableTint(int position) {
Drawable originDrawable = mView.getCompoundDrawables()[position];
Drawable compoundDrawable = originDrawable;
TintInfo tintInfo = mCompoundDrawableTintInfos[position];
if (compoundDrawable != null && tintInfo != null && tintInfo.mHasTintList) {
compoundDrawable = DrawableCompat.wrap(compoundDrawable);
compoundDrawable.mutate();
if (tintInfo.mHasTintList) {
DrawableCompat.setTintList(compoundDrawable, tintInfo.mTintList);
}
if (tintInfo.mHasTintMode) {
DrawableCompat.setTintMode(compoundDrawable, tintInfo.mTintMode);
}
if (compoundDrawable.isStateful()) {
compoundDrawable.setState(originDrawable.getState());
}
return compoundDrawable;
}
return originDrawable;
}
public void setForeground(Drawable drawable) {
if (foreground != drawable) {
if (foreground != null) {
foreground.setCallback(null);
unscheduleDrawable(foreground);
}
foreground = 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;
}
/**
* Returns the background drawable for the key, based on the current state and type of the key.
* @return the background drawable of the key.
* @see android.graphics.drawable.StateListDrawable#setState(int[])
*/
public final Drawable selectBackgroundDrawable(final Drawable keyBackground,
final Drawable functionalKeyBackground,
final Drawable spacebarBackground) {
final Drawable background;
if (mBackgroundType == BACKGROUND_TYPE_FUNCTIONAL) {
background = functionalKeyBackground;
} else if (mBackgroundType == BACKGROUND_TYPE_SPACEBAR) {
background = spacebarBackground;
} else {
background = keyBackground;
}
final int[] state = KeyBackgroundState.STATES[mBackgroundType].getState(mPressed);
background.setState(state);
return background;
}
public boolean applySupportButtonDrawableTint() {
Drawable buttonDrawable = CompoundButtonCompat.getButtonDrawable((CompoundButton) 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 applySupportImageTint() {
Drawable image = mView.getDrawable();
if (image != null && mImageTintInfo != null && mImageTintInfo.mHasTintList) {
Drawable tintDrawable = image.mutate();
tintDrawable = DrawableCompat.wrap(tintDrawable);
if (mImageTintInfo.mHasTintList) {
DrawableCompat.setTintList(tintDrawable, mImageTintInfo.mTintList);
}
if (mImageTintInfo.mHasTintMode) {
DrawableCompat.setTintMode(tintDrawable, mImageTintInfo.mTintMode);
}
if (tintDrawable.isStateful()) {
tintDrawable.setState(mView.getDrawableState());
}
setImageDrawable(tintDrawable);
if (image == tintDrawable) {
tintDrawable.invalidateSelf();
}
return true;
}
return false;
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
final Drawable progressDrawable = getProgressDrawable();
if (progressDrawable != null && mDisabledAlpha < 1.0f) {
progressDrawable.setAlpha(isEnabled() ? NO_ALPHA : (int) (NO_ALPHA * mDisabledAlpha));
}
final Drawable thumb = mThumb;
if (thumb != null && thumb.isStateful()
&& thumb.setState(getDrawableState())) {
invalidateDrawable(thumb);
}
final Drawable tickMark = mTickMark;
if (tickMark != null && tickMark.isStateful()
&& tickMark.setState(getDrawableState())) {
invalidateDrawable(tickMark);
}
}
protected void drawableStateChanged()
{
super.drawableStateChanged();
Drawable drawable = s;
if (drawable != null && drawable.isStateful())
{
drawable.setState(getDrawableState());
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
final Drawable d = mMarginDrawable;
if (d != null && d.isStateful()) {
d.setState(getDrawableState());
}
}
public void setPreviewBackground(final boolean hasMoreKeys, final int position) {
final Drawable background = getBackground();
if (background == null) {
return;
}
final int hasMoreKeysState = hasMoreKeys ? STATE_HAS_MOREKEYS : STATE_NORMAL;
background.setState(KEY_PREVIEW_BACKGROUND_STATE_TABLE[position][hasMoreKeysState]);
}
@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());
}
}
/**
* 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() );
}
}
private void applySupportProgressTint() {
if (mProgressTintInfo != null
&& (mProgressTintInfo.mHasTintList || mProgressTintInfo.mHasTintMode)) {
final Drawable target = getTintTarget(android.R.id.progress, true);
if (target != null) {
TintManager.tintViewDrawable(mView, target, mProgressTintInfo);
// The drawable (or one of its children) may not have been
// stateful before applying the tint, so let's try again.
if (target.isStateful()) {
target.setState(mView.getDrawableState());
}
}
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
final Drawable d = mMarginDrawable;
if (d != null && d.isStateful()) {
d.setState(getDrawableState());
}
}
private void updateDrawable(Drawable d) {
if (d != mRecycleableBitmapDrawable && mRecycleableBitmapDrawable != null) {
mRecycleableBitmapDrawable.setBitmap(null);
}
boolean sameDrawable = false;
if (mDrawable != null) {
sameDrawable = mDrawable == d;
mDrawable.setCallback(null);
unscheduleDrawable(mDrawable);
if (!sCompatDrawableVisibilityDispatch && !sameDrawable && isAttachedToWindow()) {
mDrawable.setVisible(false, false);
}
}
mDrawable = d;
if (d != null) {
d.setCallback(this);
d.setLayoutDirection(getLayoutDirection());
if (d.isStateful()) {
d.setState(getDrawableState());
}
if (!sameDrawable || sCompatDrawableVisibilityDispatch) {
final boolean visible = sCompatDrawableVisibilityDispatch
? getVisibility() == VISIBLE
: isAttachedToWindow() && getWindowVisibility() == VISIBLE && isShown();
d.setVisible(visible, true);
}
d.setLevel(mLevel);
mDrawableWidth = d.getIntrinsicWidth();
mDrawableHeight = d.getIntrinsicHeight();
applyImageTint();
applyColorMod();
configureBounds();
} else {
mDrawableWidth = mDrawableHeight = -1;
}
}
protected void setDrawableState(final Drawable drawable, final int[] drawableState) {
if (null != drawable && drawable.isStateful() && drawable.setState(drawableState)) {
invalidateDrawable(drawable);
}
}
@Override
public void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
// Do nothing if there are no tabs.
if (getTabCount() == 0) return;
// If the user specified a custom view for the tab indicators, then
// do not draw the bottom strips.
if (!mDrawBottomStrips) {
// Skip drawing the bottom strips.
return;
}
final View selectedChild = getChildTabViewAt(mSelectedTab);
final Drawable leftStrip = mLeftStrip;
final Drawable rightStrip = mRightStrip;
if (leftStrip != null) {
leftStrip.setState(selectedChild.getDrawableState());
}
if (rightStrip != null) {
rightStrip.setState(selectedChild.getDrawableState());
}
if (mStripMoved) {
final Rect bounds = mBounds;
bounds.left = selectedChild.getLeft();
bounds.right = selectedChild.getRight();
final int myHeight = getHeight();
if (leftStrip != null) {
leftStrip.setBounds(Math.min(0, bounds.left - leftStrip.getIntrinsicWidth()),
myHeight - leftStrip.getIntrinsicHeight(), bounds.left, myHeight);
}
if (rightStrip != null) {
rightStrip.setBounds(bounds.right, myHeight - rightStrip.getIntrinsicHeight(),
Math.max(getWidth(), bounds.right + rightStrip.getIntrinsicWidth()),
myHeight);
}
mStripMoved = false;
}
if (leftStrip != null) {
leftStrip.draw(canvas);
}
if (rightStrip != null) {
rightStrip.draw(canvas);
}
}