Java Code Examples for android.graphics.drawable.Drawable#setVisible()

The following examples show how to use android.graphics.drawable.Drawable#setVisible() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SplitImageView.java    From SplitImageView with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 2
Source File: ChipDrawable.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** 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);
  }
}
 
Example 3
Source File: HourlyTrendItemView.java    From GeometricWeather with GNU Lesser General Public License v3.0 6 votes vote down vote up
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();
    }
}
 
Example 4
Source File: SuggestionsAdapter.java    From Libraries-for-Android-Developers with MIT License 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 5
Source File: RadioButton.java    From Carbon with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
        }
    }
}
 
Example 6
Source File: CardViewWithBackgroundSupport.java    From RippleDrawable with MIT License 6 votes vote down vote up
@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();
}
 
Example 7
Source File: ToolbarHelper.java    From Paginize with MIT License 6 votes vote down vote up
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);
  }
}
 
Example 8
Source File: CheckBox.java    From Carbon with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
        }
    }
}
 
Example 9
Source File: SuggestionsAdapter.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 10
Source File: DrawableContainerCompat.java    From MaterialProgressBar with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: CompoundButton.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
        }
    }
}
 
Example 12
Source File: MediaRouteButton.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 13
Source File: CompositionAvatarView.java    From CompositionAvatar with MIT License 5 votes vote down vote up
/**
 * 添加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;
}
 
Example 14
Source File: RangeProgressBar.java    From RangeSeekBar with MIT License 5 votes vote down vote up
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);
        }
    }
}
 
Example 15
Source File: CheckedTextView.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 16
Source File: DrawableUtils.java    From fresco with MIT License 5 votes vote down vote up
/**
 * 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());
}
 
Example 17
Source File: MediaRouteButton.java    From cwac-mediarouter with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 18
Source File: DrawableContainerCompat.java    From MaterialProgressBar with Apache License 2.0 4 votes vote down vote up
/**
 * 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());
    }
}
 
Example 19
Source File: ImageView.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
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;
    }
}