Java Code Examples for android.graphics.drawable.StateListDrawable#getCurrent()

The following examples show how to use android.graphics.drawable.StateListDrawable#getCurrent() . 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: Slider.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
private void setSliderColors() {
  if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
    seekbar.setProgressTintList(ColorStateList.valueOf(leftColor));
    if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP_MR1 ||
        !(seekbar.getProgressDrawable() instanceof StateListDrawable)) {
      seekbar.setProgressBackgroundTintList(ColorStateList.valueOf(rightColor));
      seekbar.setProgressBackgroundTintMode(Mode.MULTIPLY);
    } else {
      // Looking at the AOSP code, the previous calls should effectively accomplish what the
      // following code does... except it doesn't on Android 5.0. Instead, the result is that the
      // right side color is 50% opacity of leftColor. The following code works on Android 5.0,
      // but assumes a Drawable hierarchy that may not be true if the device manufacturer deviates
      // from the AOSP design. If that is the case, then the right hand side will not change.
      StateListDrawable drawable = (StateListDrawable) seekbar.getProgressDrawable();
      if (drawable.getCurrent() instanceof LayerDrawable) {
        LayerDrawable layerDrawable = (LayerDrawable) drawable.getCurrent();
        Drawable background = layerDrawable.findDrawableByLayerId(R.id.background);
        background.setTintList(ColorStateList.valueOf(rightColor));
        background.setTintMode(Mode.MULTIPLY);
      }
    }
  } else {
    LayerDrawable fullBar = (LayerDrawable) seekbar.getProgressDrawable();
    fullBar.setColorFilter(rightColor,PorterDuff.Mode.SRC);
    fullBar.findDrawableByLayerId(R.id.progress).setColorFilter(leftColor, PorterDuff.Mode.SRC);
  }
}
 
Example 2
Source File: GBSlideBar.java    From GBSlideBar with Apache License 2.0 4 votes vote down vote up
@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mFirstDraw) drawBackground();
        if (mBackgroundDrawable != null) mBackgroundDrawable.draw(canvas);
        if (isInEditMode()) return;

        Drawable itemDefault, itemSlide;
        StateListDrawable stateListDrawable;

        if (!mSlide) {
            int distance, minIndex = 0, minDistance = Integer.MAX_VALUE;
            for (int i = 0; i < getCount(); i++) {
                distance = Math.abs(mModIsHorizontal ? mAnchor[i][0] - mCurrentX : mAnchor[i][1] - mCurrentY);
                if (minDistance > distance) {
                    minIndex = i;
                    minDistance = distance;
                }
            }

            setCurrentItem(minIndex);
            stateListDrawable = mAdapter.getItem(minIndex);


        } else {
            mSlide = false;
            mCurrentX = mAnchor[mCurrentItem][0];
            mCurrentY = mAnchor[mCurrentItem][1];
            if (mFirstDraw) {
                mSlideX = mLastX = mCurrentX;
            }
            stateListDrawable = mAdapter.getItem(mCurrentItem);

            mIsFirstSelect = true;


        }
        stateListDrawable.setState(mState);
        itemDefault = stateListDrawable.getCurrent();


        for (int i = 0; i < getCount(); i++) {
            if (i == mCurrentItem) {
//                continue; //
                mPaint.setColor(mAdapter.getTextColor(mCurrentItem));
                canvas.drawText(mAdapter.getText(i), mAnchor[i][0], mAnchor[i][1] + mAnchorHeight * 3 / 2 + mTextMargin, mPaint);
            }else {
                mPaint.setColor(mTextColor);
                canvas.drawText(mAdapter.getText(i), mAnchor[i][0], mAnchor[i][1] + mAnchorHeight * 3 / 2 + mTextMargin, mPaint);
            }
            stateListDrawable = mAdapter.getItem(i);
            stateListDrawable.setState(STATE_NORMAL);
            itemSlide = stateListDrawable.getCurrent();
            itemSlide.setBounds(
                    mAnchor[i][0] - mPlaceHolderWidth,
                    mAnchor[i][1] - mPlaceHolderHeight,
                    mAnchor[i][0] + mPlaceHolderWidth,
                    mAnchor[i][1] + mPlaceHolderHeight
            );
            itemSlide.draw(canvas);

        }


        itemDefault.setBounds(
                mSlideX - mAnchorWidth,
                mPivotY + mAbsoluteY / 2 - mAnchorHeight,
                mSlideX + mAnchorWidth,
                mPivotY + mAbsoluteY / 2 + mAnchorHeight
        );

        itemDefault.draw(canvas);

        setFirstDraw(false);

    }
 
Example 3
Source File: PhasedSeekBar.java    From android-phased-seek-bar with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mFirstDraw) configure();
    if (mBackgroundDrawable != null) mBackgroundDrawable.draw(canvas);
    if (isInEditMode()) return;

    Drawable itemOff;
    Drawable itemOn;
    StateListDrawable stateListDrawable;
    int count = getCount();

    if (!mUpdateFromPosition) {
        int distance;
        int minIndex = 0;
        int minDistance = Integer.MAX_VALUE;
        for (int i = 0; i < count; i++) {
            distance = Math.abs(mModeIsHorizontal ? mAnchors[i][0] - mCurrentX : mAnchors[i][1] - mCurrentY);
            if (minDistance > distance) {
                minIndex = i;
                minDistance = distance;
            }
        }

        setCurrentItem(minIndex);
        stateListDrawable = mAdapter.getItem(minIndex);
    } else {
        mUpdateFromPosition = false;
        mCurrentX = mAnchors[mCurrentItem][0];
        mCurrentY = mAnchors[mCurrentItem][1];
        stateListDrawable = mAdapter.getItem(mCurrentItem);
    }
    stateListDrawable.setState(mState);
    itemOn = stateListDrawable.getCurrent();

    for (int i = 0; i < count; i++) {
        if (!mDrawOnOff && i == mCurrentItem) continue;
        stateListDrawable = mAdapter.getItem(i);
        stateListDrawable.setState(STATE_NORMAL);
        itemOff = stateListDrawable.getCurrent();
        itemOff.setBounds(
                mAnchors[i][0] - mItemHalfWidth,
                mAnchors[i][1] - mItemHalfHeight,
                mAnchors[i][0] + mItemHalfWidth,
                mAnchors[i][1] + mItemHalfHeight);
        itemOff.draw(canvas);
    }

    itemOn.setBounds(
            mCurrentX - mItemHalfWidth,
            mCurrentY - mItemHalfHeight,
            mCurrentX + mItemHalfWidth,
            mCurrentY + mItemHalfHeight);
    itemOn.draw(canvas);

    setFirstDraw(false);
}