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

The following examples show how to use android.graphics.drawable.Drawable#setState() . 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: ForegroundDrawableRelativeLayout.java    From cathode with Apache License 2.0 6 votes vote down vote up
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();
  }
}
 
Example 2
Source File: AppCompatCompoundDrawableHelper.java    From MagicaSakura with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: AbsSeekBar.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 4
Source File: AppCompatSwitchHelper.java    From timecat with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: Key.java    From simple-keyboard with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: AppCompatImageHelper.java    From MagicaSakura with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 7
Source File: AppCompatCompoundButtonHelper.java    From timecat with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 8
Source File: VerticalViewPager.java    From VerticalViewPager with Apache License 2.0 5 votes vote down vote up
@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    final Drawable d = mMarginDrawable;
    if (d != null && d.isStateful()) {
        d.setState(getDrawableState());
    }
}
 
Example 9
Source File: AppCompatProgressBarHelper.java    From MagicaSakura with Apache License 2.0 5 votes vote down vote up
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());
            }
        }
    }
}
 
Example 10
Source File: SdkCenteredViewPager.java    From Android-SDK-Demo with MIT License 5 votes vote down vote up
@Override
protected void drawableStateChanged()
{
    super.drawableStateChanged();
    final Drawable d = mMarginDrawable;
    if ( d != null && d.isStateful() )
    {
        d.setState( getDrawableState() );
    }
}
 
Example 11
Source File: ForegroundDelegate.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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();
        }
    }
}
 
Example 12
Source File: DirectionalViewpager.java    From BookReader with Apache License 2.0 5 votes vote down vote up
@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    final Drawable d = mMarginDrawable;
    if (d != null && d.isStateful()) {
        d.setState(getDrawableState());
    }
}
 
Example 13
Source File: VerViewPager.java    From likeJDGoodsDetails with Apache License 2.0 5 votes vote down vote up
@Override
protected void drawableStateChanged() {
	super.drawableStateChanged();
	final Drawable d = mMarginDrawable;
	if (d != null && d.isStateful()) {
		d.setState(getDrawableState());
	}
}
 
Example 14
Source File: HorizontalViewPager.java    From DoubleViewPager with Apache License 2.0 5 votes vote down vote up
@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    final Drawable d = mMarginDrawable;
    if (d != null && d.isStateful()) {
        d.setState(getDrawableState());
    }
}
 
Example 15
Source File: KeyPreviewView.java    From openboard with GNU General Public License v3.0 5 votes vote down vote up
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]);
}
 
Example 16
Source File: ViewPager.java    From adt-leanback-support with Apache License 2.0 5 votes vote down vote up
@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    final Drawable d = mMarginDrawable;
    if (d != null && d.isStateful()) {
        d.setState(getDrawableState());
    }
}
 
Example 17
Source File: VerticalViewPager.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
protected void drawableStateChanged()
{
    super.drawableStateChanged();
    Drawable drawable = s;
    if (drawable != null && drawable.isStateful())
    {
        drawable.setState(getDrawableState());
    }
}
 
Example 18
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;
    }
}
 
Example 19
Source File: RangeSeekBar.java    From RangeSeekBar with MIT License 4 votes vote down vote up
protected void setDrawableState(final Drawable drawable, final int[] drawableState) {
    if (null != drawable && drawable.isStateful() && drawable.setState(drawableState)) {
        invalidateDrawable(drawable);
    }
}
 
Example 20
Source File: TabWidget.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@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);
    }
}