Example usage for android.graphics Canvas restoreToCount

List of usage examples for android.graphics Canvas restoreToCount

Introduction

In this page you can find the example usage for android.graphics Canvas restoreToCount.

Prototype

public void restoreToCount(int saveCount) 

Source Link

Document

Efficient way to pop any calls to save() that happened after the save count reached saveCount.

Usage

From source file:com.hippo.widget.Slider.java

@Override
protected void onDraw(@NonNull Canvas canvas) {
    int width = getWidth();
    int height = getHeight();
    if (width < LayoutUtils.dp2pix(mContext, 24)) {
        canvas.drawRect(0, 0, width, getHeight(), mPaint);
    } else {/*  ww  w  . j a  va 2  s.com*/
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();
        float thickness = mThickness;
        float radius = mRadius;
        float halfThickness = thickness / 2;

        int saved = canvas.save();

        canvas.translate(0, paddingTop + ((height - paddingTop - paddingBottom) / 2));

        float currentX = paddingLeft + radius + (width - radius - radius - paddingLeft - paddingRight)
                * (mReverse ? (1.0f - mDrawPercent) : mDrawPercent);

        mLeftRectF.set(paddingLeft + radius, -halfThickness, currentX, halfThickness);
        mRightRectF.set(currentX, -halfThickness, width - paddingRight - radius, halfThickness);

        // Draw bar
        if (mReverse) {
            canvas.drawRect(mRightRectF, mPaint);
            canvas.drawRect(mLeftRectF, mBgPaint);
        } else {
            canvas.drawRect(mLeftRectF, mPaint);
            canvas.drawRect(mRightRectF, mBgPaint);
        }

        // Draw controller
        float scale = 1.0f - mDrawBubbleScale;
        if (scale != 0.0f) {
            canvas.scale(scale, scale, currentX, 0);
            canvas.drawCircle(currentX, 0, radius, mPaint);
        }

        canvas.restoreToCount(saved);
    }
}

From source file:com.akshay.protocol10.asplayer.widget.SlidingUpPanelLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    boolean result;
    final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);

    if (isSlidingEnabled() && mSlideableView != child) {
        // Clip against the slider; no sense drawing what will immediately
        // be covered,
        // Unless the panel is set to overlay content
        if (!mOverlayContent) {
            canvas.getClipBounds(mTmpRect);
            if (mIsSlidingUp) {
                mTmpRect.bottom = Math.min(mTmpRect.bottom, mSlideableView.getTop());
            } else {
                mTmpRect.top = Math.max(mTmpRect.top, mSlideableView.getBottom());
            }//  ww w.  j  a  v a  2s  . c  o  m
            canvas.clipRect(mTmpRect);
        }
    }

    result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(save);

    if (mCoveredFadeColor != 0 && mSlideOffset > 0) {
        final int baseAlpha = (mCoveredFadeColor & 0xff000000) >>> 24;
        final int imag = (int) (baseAlpha * mSlideOffset);
        final int color = imag << 24 | (mCoveredFadeColor & 0xffffff);
        mCoveredFadePaint.setColor(color);
        canvas.drawRect(mTmpRect, mCoveredFadePaint);
    }

    return result;
}

From source file:android.support.design.widget.CustomCollapsingTextHelper.java

public void draw(Canvas canvas) {
    final int saveCount = canvas.save();

    if (mTextToDraw != null && mDrawTitle) {
        float x = mCurrentDrawX;
        float y = mCurrentDrawY;
        float subY = mCurrentSubY;
        final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null;

        final float ascent;
        final float descent;
        if (drawTexture) {
            ascent = mTextureAscent * mScale;
            descent = mTextureDescent * mScale;
        } else {/*from   ww w  . j  a va  2s.  c o  m*/
            ascent = mTitlePaint.ascent() * mScale;
            descent = mTitlePaint.descent() * mScale;
        }

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a magenta rect in the text bounds
            canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent,
                    DEBUG_DRAW_PAINT);
        }

        if (drawTexture) {
            y += ascent;
        }

        //region modification
        final int saveCountSub = canvas.save();
        if (mSub != null) {
            if (mSubScale != 1f) {
                canvas.scale(mSubScale, mSubScale, x, subY);
            }
            canvas.drawText(mSub, 0, mSub.length(), x, subY, mSubPaint);
            canvas.restoreToCount(saveCountSub);
        }
        //endregion

        if (mScale != 1f) {
            canvas.scale(mScale, mScale, x, y);
        }

        if (drawTexture) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint);
        } else {
            canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTitlePaint);
        }
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.android.dialer.widget.OverlappingPaneLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    boolean result;
    final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);

    if (mCanSlide && !lp.slideable && mSlideableView != null) {
        // Clip against the slider; no sense drawing what will immediately be covered.
        canvas.getClipBounds(mTmpRect);/*from  w  w  w. ja  va 2  s .c  o  m*/

        mTmpRect.bottom = Math.min(mTmpRect.bottom, mSlideableView.getTop());
        canvas.clipRect(mTmpRect);
    }

    if (Build.VERSION.SDK_INT >= 11) { // HC
        result = super.drawChild(canvas, child, drawingTime);
    } else {
        if (child.isDrawingCacheEnabled()) {
            child.setDrawingCacheEnabled(false);
        }
        result = super.drawChild(canvas, child, drawingTime);
    }

    canvas.restoreToCount(save);

    return result;
}

From source file:com.tr4android.support.extension.widget.CollapsingTextHelper.java

public void draw(Canvas canvas) {
    final int saveCount = canvas.save();

    if (mTextToDraw != null && mDrawTitle) {
        float x = mCurrentDrawX;
        float y = mCurrentDrawY;

        final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null;

        final float ascent;
        final float descent;

        // Update the TextPaint to the current text size
        mTextPaint.setTextSize(mCurrentTextSize);

        if (drawTexture) {
            ascent = mTextureAscent * mScale;
            descent = mTextureDescent * mScale;
        } else {//  w  w w.j  a  v a2  s.  co m
            ascent = mTextPaint.ascent() * mScale;
            descent = mTextPaint.descent() * mScale;
        }

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta rect in the text bounds
            canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent,
                    DEBUG_DRAW_PAINT);
        }

        if (drawTexture) {
            y += ascent;
        }

        if (mScale != 1f) {
            canvas.scale(mScale, mScale, x, y);
        }

        if (drawTexture) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint);
        } else {
            canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTextPaint);
        }
    }

    canvas.restoreToCount(saveCount);
}

From source file:android.support.v7.widget.SwitchCompat.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    final Rect padding = mTempRect;
    final Drawable trackDrawable = mTrackDrawable;
    if (trackDrawable != null) {
        trackDrawable.getPadding(padding);
    } else {/*w ww  .j  a va 2  s  .c om*/
        padding.setEmpty();
    }

    final int switchTop = mSwitchTop;
    final int switchBottom = mSwitchBottom;
    final int switchInnerTop = switchTop + padding.top;
    final int switchInnerBottom = switchBottom - padding.bottom;

    final Drawable thumbDrawable = mThumbDrawable;
    if (trackDrawable != null) {
        if (mSplitTrack && thumbDrawable != null) {
            final Rect insets = DrawableUtils.getOpticalBounds(thumbDrawable);
            thumbDrawable.copyBounds(padding);
            padding.left += insets.left;
            padding.right -= insets.right;

            final int saveCount = canvas.save();
            canvas.clipRect(padding, Region.Op.DIFFERENCE);
            trackDrawable.draw(canvas);
            canvas.restoreToCount(saveCount);
        } else {
            trackDrawable.draw(canvas);
        }
    }

    final int saveCount = canvas.save();

    if (thumbDrawable != null) {
        thumbDrawable.draw(canvas);
    }

    final Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
    if (switchText != null) {
        final int drawableState[] = getDrawableState();
        if (mTextColors != null) {
            mTextPaint.setColor(mTextColors.getColorForState(drawableState, 0));
        }
        mTextPaint.drawableState = drawableState;

        final int cX;
        if (thumbDrawable != null) {
            final Rect bounds = thumbDrawable.getBounds();
            cX = bounds.left + bounds.right;
        } else {
            cX = getWidth();
        }

        final int left = cX / 2 - switchText.getWidth() / 2;
        final int top = (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2;
        canvas.translate(left, top);
        switchText.draw(canvas);
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.blestep.sportsbracelet.view.TimelineChartView.java

private void drawTickLabels(Canvas c, SparseArray<Object[]> data) {
    final int size = data.size() - 1;
    final float graphCenterX = mGraphArea.left + (mGraphArea.width() / 2);
    final float halfItemBarWidth = mBarItemWidth / 2;
    for (int i = mItemsOnScreen[1]; i >= mItemsOnScreen[0] && i <= data.size(); i--) {
        final float barCenterX = graphCenterX + mCurrentOffset - (mBarWidth * (size - i));
        float barLeft = barCenterX - halfItemBarWidth;
        float barRight = barCenterX + halfItemBarWidth;
        // Update the dynamic layout
        String label = (String) data.valueAt(i)[0];
        // /*from ww w  .  j a va  2s.  c o  m*/
        // Calculate the x position and draw the layout
        final float x = graphCenterX + mCurrentOffset - (mBarWidth * (size - i))
                - (mLabelFgPaint.measureText(label) / 2);
        final int restoreCount = c.save();
        c.translate(x, mFooterArea.top + (mFooterArea.height() / 2 - mTickLabelMinHeight / 2));
        final Paint paint;
        // ?
        paint = barLeft < graphCenterX && barRight > graphCenterX
                && (mLastPosition == mCurrentPosition || (mState != STATE_SCROLLING)) ? mHighlightLabelFgPaint
                        : mLabelFgPaint;
        c.drawText(label, 0, 0, paint);
        c.restoreToCount(restoreCount);
    }
}

From source file:com.bitflake.counter.HorizontalPicker.java

private void drawEdgeEffect(Canvas canvas, EdgeEffect edgeEffect, int degrees) {

    if (canvas == null || edgeEffect == null || (degrees != 90 && degrees != 270)) {
        return;/* w  ww  .j  a  v a  2 s .  c  o  m*/
    }

    if (!edgeEffect.isFinished()) {
        final int restoreCount = canvas.getSaveCount();
        final int width = getWidth();
        final int height = getHeight();

        canvas.rotate(degrees);

        if (degrees == 270) {
            canvas.translate(-height, Math.max(0, getScrollX()));
        } else { // 90
            canvas.translate(0, -(Math.max(getScrollRange(), getScaleX()) + width));
        }

        edgeEffect.setSize(height, width);
        if (edgeEffect.draw(canvas)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                postInvalidateOnAnimation();
            } else {
                postInvalidate();
            }
        }

        canvas.restoreToCount(restoreCount);
    }

}

From source file:com.muzakki.ahmad.widget.CollapsingTextHelper.java

public void draw(Canvas canvas) {
    final int saveCount = canvas.save();

    if (mTextToDraw != null && mDrawTitle) {
        float x = mCurrentDrawX;
        float y = mCurrentDrawY;
        float subX = mCurrentSubX;
        float subY = mCurrentSubY;
        final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null;

        final float ascent;
        final float descent;
        if (drawTexture) {
            ascent = mTextureAscent * mScale;
            descent = mTextureDescent * mScale;
        } else {//from   w w w.ja v  a 2 s  .c om
            ascent = mTextPaint.ascent() * mScale;
            descent = mTextPaint.descent() * mScale;
        }

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a magenta rect in the text bounds
            canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent,
                    DEBUG_DRAW_PAINT);
        }

        if (drawTexture) {
            y += ascent;
        }

        // begin modification
        // draw subtitle
        final int saveCountSub = canvas.save();
        if (mSub != null) {
            if (mSubScale != 1f) {
                canvas.scale(mSubScale, mSubScale, subX, subY);
            }
            canvas.drawText(mSub, 0, mSub.length(), subX, subY, mSubPaint);
            canvas.restoreToCount(saveCountSub);
        }
        // end modification

        if (mScale != 1f) {
            canvas.scale(mScale, mScale, x, y);
        }

        if (drawTexture) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint);
        } else {
            canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTextPaint);
        }
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.quran.labs.androidquran.widgets.SlidingUpPanelLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    boolean result;
    final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);

    boolean drawScrim = false;

    if (mCanSlide && !lp.slideable && mSlideableView != null) {
        // Clip against the slider; no sense drawing what will immediately be covered,
        // Unless the panel is set to overlay content
        if (!mOverlayContent) {
            canvas.getClipBounds(mTmpRect);
            if (mIsSlidingUp) {
                mTmpRect.bottom = Math.min(mTmpRect.bottom, mSlideableView.getTop());
            } else {
                mTmpRect.top = Math.max(mTmpRect.top, mSlideableView.getBottom());
            }/*from  w  w  w. j  a v  a  2 s .  c om*/
            canvas.clipRect(mTmpRect);
        }
        if (mSlideOffset < 1) {
            drawScrim = true;
        }
    }

    result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(save);

    if (drawScrim) {
        final int baseAlpha = (mCoveredFadeColor & 0xff000000) >>> 24;
        final int imag = (int) (baseAlpha * (1 - mSlideOffset));
        final int color = imag << 24 | (mCoveredFadeColor & 0xffffff);
        mCoveredFadePaint.setColor(color);
        canvas.drawRect(mTmpRect, mCoveredFadePaint);
    }

    return result;
}