下面列出了android.graphics.drawable.Drawable#setVisible() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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();
}
}
}
/**
* Adds the drawable to the end of the list of contained drawables.
*
* @param dr the drawable to add
* @return the position of the drawable within the container
*/
public final int addChild(Drawable dr) {
final int pos = mNumChildren;
if (pos >= mDrawables.length) {
growArray(pos, pos + 10);
}
dr.mutate();
dr.setVisible(false, true);
dr.setCallback(mOwner);
mDrawables[pos] = dr;
mNumChildren++;
mChildrenChangingConfigurations |= dr.getChangingConfigurations();
invalidateCache();
mConstantPadding = null;
mCheckedPadding = false;
mCheckedConstantSize = false;
mCheckedConstantState = false;
return pos;
}
/**
* Sets the drawable in an image view, makes sure the view is only visible if there
* is a drawable.
*/
private void setViewDrawable(ImageView v, Drawable drawable, int nullVisibility) {
// Set the icon even if the drawable is null, since we need to clear any
// previous icon.
v.setImageDrawable(drawable);
if (drawable == null) {
v.setVisibility(nullVisibility);
} else {
v.setVisibility(View.VISIBLE);
// This is a hack to get any animated drawables (like a 'working' spinner)
// to animate. You have to setVisible true on an AnimationDrawable to get
// it to start animating, but it must first have been false or else the
// call to setVisible will be ineffective. We need to clear up the story
// about animated drawables in the future, see http://b/1878430.
drawable.setVisible(false, false);
drawable.setVisible(true, false);
}
}
/**
* Set the button graphic to a given Drawable
*
* @param d The Drawable to use as the button graphic
*/
public void setButtonDrawable(Drawable d) {
if (drawable != d) {
if (drawable != null) {
drawable.setCallback(null);
unscheduleDrawable(drawable);
}
drawable = d;
if (d != null) {
drawable = DrawableCompat.wrap(d);
d.setCallback(this);
//d.setLayoutDirection(getLayoutDirection());
if (d.isStateful()) {
d.setState(getDrawableState());
}
d.setVisible(getVisibility() == VISIBLE, false);
setMinHeight(d.getIntrinsicHeight());
applyTint();
}
}
}
private void mountDrawable(int index, MountItem mountItem, Rect bounds) {
assertMainThread();
ensureDrawableMountItems();
mDrawableMountItems.put(index, mountItem);
final Drawable drawable = (Drawable) mountItem.getContent();
final LayoutOutput output = getLayoutOutput(mountItem);
drawable.setVisible(getVisibility() == View.VISIBLE, false);
drawable.setCallback(this);
// If mount data is LithoMountData then Litho need to manually set drawable state.
if (mountItem.getMountData() instanceof LithoMountData) {
maybeSetDrawableState(this, drawable, output.getFlags(), output.getNodeInfo());
}
invalidate(bounds);
}
public static void setNavigationIconEnabled(Toolbar toolbar,
boolean enable,
View.OnClickListener listener) {
Drawable navIcon = toolbar.getNavigationIcon();
if (enable) {
if (navIcon != null) {
navIcon.setVisible(true, false);
} else {
toolbar.setNavigationIcon(R.drawable.paginize_contrib_ic_arrow_back_white_24dp);
if (listener != null) {
toolbar.setNavigationOnClickListener(listener);
}
}
} else if (navIcon != null){
navIcon.setVisible(false, false);
}
}
private void updateDrawable(Drawable d, boolean isForegroundDrawable) {
Drawable drawable = isForegroundDrawable ? mForegroundDrawable : mBackgroundDrawable;
if (drawable != null) {
drawable.setCallback(null);
unscheduleDrawable(drawable);
}
if (isForegroundDrawable) {
mForegroundDrawable = d;
} else {
mBackgroundDrawable = d;
}
if (d != null) {
d.setCallback(this);
d.setVisible(getVisibility() == VISIBLE, true);
mMaxDrawableWidth = Math.max(d.getIntrinsicWidth(), mMaxDrawableWidth);
mMaxDrawableHeight = Math.max(d.getIntrinsicHeight(), mMaxDrawableHeight);
applyColorMod();
configureBounds();
}
}
@Override
public void setBackground(Drawable background) {
if (mBackground != null) {
mBackground.setCallback(null);
unscheduleDrawable(mBackground);
}
mBackground = background;
if (background != null) {
background.setLevel(0);
if (background.isStateful()) {
background.setState(getDrawableState());
}
background.setVisible(getVisibility() == VISIBLE, true);
background.setCallback(this);
}
invalidate();
}
/**
* Set the button graphic to a given Drawable
*
* @param d The Drawable to use as the button graphic
*/
public void setButtonDrawable(Drawable d) {
if (drawable != d) {
if (drawable != null) {
drawable.setCallback(null);
unscheduleDrawable(drawable);
}
drawable = d;
if (d != null) {
drawable = DrawableCompat.wrap(d);
d.setCallback(this);
//d.setLayoutDirection(getLayoutDirection());
if (d.isStateful()) {
d.setState(getDrawableState());
}
d.setVisible(getVisibility() == VISIBLE, false);
setMinHeight(d.getIntrinsicHeight());
applyTint();
}
}
}
/**
* Sets the drawable in an image view, makes sure the view is only visible if there
* is a drawable.
*/
private void setViewDrawable(ImageView v, Drawable drawable, int nullVisibility) {
// Set the icon even if the drawable is null, since we need to clear any
// previous icon.
v.setImageDrawable(drawable);
if (drawable == null) {
v.setVisibility(nullVisibility);
} else {
v.setVisibility(View.VISIBLE);
// This is a hack to get any animated drawables (like a 'working' spinner)
// to animate. You have to setVisible true on an AnimationDrawable to get
// it to start animating, but it must first have been false or else the
// call to setVisible will be ineffective. We need to clear up the story
// about animated drawables in the future, see http://b/1878430.
drawable.setVisible(false, false);
drawable.setVisible(true, false);
}
}
public void setIconDrawable(@Nullable Drawable d) {
boolean nullDrawable = iconDrawable == null;
iconDrawable = d;
if (d != null) {
d.setVisible(true, true);
d.setCallback(this);
d.setBounds(0, 0, iconSize, iconSize);
}
if (nullDrawable != (d == null)) {
requestLayout();
} else {
invalidate();
}
}
/** Note: This should not change the size of the drawable. */
private void applyChildDrawable(@Nullable Drawable drawable) {
if (drawable == null) {
return;
}
drawable.setCallback(this);
DrawableCompat.setLayoutDirection(drawable, DrawableCompat.getLayoutDirection(this));
drawable.setLevel(getLevel());
drawable.setVisible(isVisible(), false);
if (drawable == closeIcon) {
if (drawable.isStateful()) {
drawable.setState(getCloseIconState());
}
DrawableCompat.setTintList(drawable, closeIconTint);
return;
}
if (drawable.isStateful()) {
drawable.setState(getState());
}
if (drawable == chipIcon && hasChipIconTint) {
DrawableCompat.setTintList(chipIcon, chipIconTint);
}
}
private void setRemoteIndicatorDrawable(Drawable d) {
if (mRemoteIndicator != null) {
mRemoteIndicator.setCallback(null);
unscheduleDrawable(mRemoteIndicator);
}
mRemoteIndicator = d;
if (d != null) {
d.setCallback(this);
d.setState(getDrawableState());
d.setVisible(getVisibility() == VISIBLE, false);
}
refreshDrawableState();
}
private void setCheckMarkDrawableInternal(@Nullable Drawable d, @DrawableRes int resId) {
if (mCheckMarkDrawable != null) {
mCheckMarkDrawable.setCallback(null);
unscheduleDrawable(mCheckMarkDrawable);
}
mNeedRequestlayout = (d != mCheckMarkDrawable);
if (d != null) {
d.setCallback(this);
d.setVisible(getVisibility() == VISIBLE, false);
d.setState(CHECKED_STATE_SET);
// Record the intrinsic dimensions when in "checked" state.
setMinHeight(d.getIntrinsicHeight());
mCheckMarkWidth = d.getIntrinsicWidth();
d.setState(getDrawableState());
} else {
mCheckMarkWidth = 0;
}
mCheckMarkDrawable = d;
mCheckMarkResource = resId;
applyCheckMarkTint();
// Do padding resolution. This will call internalSetPadding() and do a
// requestLayout() if needed.
resolvePadding();
}
/**
* 添加drawable, 如果id已经存在, drawable将会被替换
*
* @param id the drawable id.
* @param drawable the drawable.
* @return <code>true</code> - 如果添加成功, <code>false</code> - 其他
*/
public boolean addDrawable(int id, @NonNull Drawable drawable) {
DrawableInfo old = findAvatarDrawableById(id);
if (old != null) {
Drawable d = old.mDrawable;
old.mDrawable = drawable;
if (!hasSameDrawable(d)) {
cleanDrawable(d);
}
updateDrawableBounds(old);
} else {
if (getNumberOfDrawables() >= MAX_DRAWABLE_COUNT) {
return false;
}
mDrawables.add(crateAvatarDrawable(id, drawable));
layoutDrawables();
}
drawable.setCallback(this);
drawable.setVisible(getWindowVisibility() == VISIBLE && isShown(), true);
if (drawable.isStateful()) {
drawable.setState(getDrawableState());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
drawable.setLayoutDirection(getLayoutDirection());
}
invalidate();
return true;
}
private void swapCurrentDrawable(Drawable newDrawable) {
final Drawable oldDrawable = mCurrentDrawable;
mCurrentDrawable = newDrawable;
if (oldDrawable != mCurrentDrawable) {
if (oldDrawable != null) {
oldDrawable.setVisible(false, false);
}
if (mCurrentDrawable != null) {
mCurrentDrawable.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
}
}
}
/**
* Copies various properties from one drawable to the other.
*
* @param to drawable to copy properties to
* @param from drawable to copy properties from
*/
public static void copyProperties(@Nullable Drawable to, @Nullable Drawable from) {
if (from == null || to == null || to == from) {
return;
}
to.setBounds(from.getBounds());
to.setChangingConfigurations(from.getChangingConfigurations());
to.setLevel(from.getLevel());
to.setVisible(from.isVisible(), /* restart */ false);
to.setState(from.getState());
}
private void setRemoteIndicatorDrawable(Drawable d) {
if (mRemoteIndicator != null) {
mRemoteIndicator.setCallback(null);
unscheduleDrawable(mRemoteIndicator);
}
mRemoteIndicator = d;
if (d != null) {
d.setCallback(this);
d.setState(getDrawableState());
d.setVisible(getVisibility() == VISIBLE, false);
}
refreshDrawableState();
}
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;
}
}
/**
* Initializes a drawable for display in this container.
*
* @param d The drawable to initialize.
*/
private void initializeDrawableForDisplay(Drawable d) {
if (mBlockInvalidateCallback == null) {
mBlockInvalidateCallback = new BlockInvalidateCallback();
}
// Temporary fix for suspending callbacks during initialization. We
// don't want any of these setters causing an invalidate() since that
// may call back into DrawableContainer.
d.setCallback(mBlockInvalidateCallback.wrap(d.getCallback()));
try {
if (mDrawableContainerState.mEnterFadeDuration <= 0 && mHasAlpha) {
d.setAlpha(mAlpha);
}
if (mDrawableContainerState.mHasColorFilter) {
// Color filter always overrides tint.
d.setColorFilter(mDrawableContainerState.mColorFilter);
} else {
if (mDrawableContainerState.mHasTintList) {
DrawableCompat.setTintList(d, mDrawableContainerState.mTintList);
}
if (mDrawableContainerState.mHasTintMode) {
DrawableCompat.setTintMode(d, mDrawableContainerState.mTintMode);
}
}
d.setVisible(isVisible(), true);
d.setDither(mDrawableContainerState.mDither);
d.setState(getState());
d.setLevel(getLevel());
d.setBounds(getBounds());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
d.setLayoutDirection(getLayoutDirection());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
d.setAutoMirrored(mDrawableContainerState.mAutoMirrored);
}
final Rect hotspotBounds = mHotspotBounds;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && hotspotBounds != null) {
d.setHotspotBounds(hotspotBounds.left, hotspotBounds.top,
hotspotBounds.right, hotspotBounds.bottom);
}
} finally {
d.setCallback(mBlockInvalidateCallback.unwrap());
}
}