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:au.com.zacher.popularmovies.activity.layout.CollapsingTitleLayout.java

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

    final int toolbarHeight = this.mToolbar.getHeight();
    canvas.clipRect(0, 0, canvas.getWidth(),
            interpolate(canvas.getHeight(), toolbarHeight, this.mScrollOffset));

    // Now call super and let it draw the background, etc
    super.draw(canvas);

    if (this.mTitleToDraw != null) {
        float x = this.mTextLeft;
        float y = this.mTextTop;

        final float ascent = this.mTextPaint.ascent() * this.mScale;
        final float descent = this.mTextPaint.descent() * this.mScale;
        final float h = descent - ascent;

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta rect in the text bounds
            canvas.drawRect(this.mTextLeft, y - h + descent, this.mTextRight, y + descent, DEBUG_DRAW_PAINT);
        }//w w  w . j  a  v  a  2 s .  c  om

        if (this.mUseTexture) {
            y = y - h + descent;
        }

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

        if (this.mUseTexture && this.mExpandedTitleTexture != null) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(this.mExpandedTitleTexture, x, y, this.mTexturePaint);
        } else {
            canvas.drawText(this.mTitleToDraw, x, y, this.mTextPaint);
        }
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.example.stickablelistview.StickListView.java

/** Draws the overscroll edge glow effect on the left and right sides of the horizontal list */
private void drawEdgeGlow(Canvas canvas) {
    if (mEdgeGlowTop != null && !mEdgeGlowTop.isFinished() && isEdgeGlowEnabled()) {
        // The Edge glow is meant to come from the top of the screen, so rotate it to draw on the left side.
        final int restoreCount = canvas.save();
        final int width = getWidth();

        canvas.rotate(-90, 0, 0);//w  ww  .j a  v  a2  s . c o  m
        canvas.translate(0, -width + getPaddingRight());

        mEdgeGlowTop.setSize(getRenderWidth(), getRenderHeight());
        if (mEdgeGlowTop.draw(canvas)) {
            invalidate();
        }

        canvas.restoreToCount(restoreCount);
    } else if (mEdgeGlowBottom != null && !mEdgeGlowBottom.isFinished() && isEdgeGlowEnabled()) {
        // The Edge glow is meant to come from the top of the screen, so rotate it to draw on the right side.
        final int restoreCount = canvas.save();
        final int width = getWidth();

        canvas.rotate(90, 0, 0);
        canvas.translate(getPaddingTop(), -width);
        mEdgeGlowBottom.setSize(getRenderWidth(), getRenderHeight());
        if (mEdgeGlowBottom.draw(canvas)) {
            invalidate();
        }

        canvas.restoreToCount(restoreCount);
    }
}

From source file:com.simon.catkins.views.HorizontalListView.java

/** Draws the overscroll edge glow effect on the top and bottom sides of the vertical list */
private void drawEdgeGlow(Canvas canvas) {
    if (mEdgeGlowLeft != null && !mEdgeGlowLeft.isFinished() && isEdgeGlowEnabled()) {
        // The Edge glow is meant to come from the top of the screen, so rotate it to draw on the top side.
        final int restoreCount = canvas.save();
        final int height = getHeight();

        canvas.rotate(-90, 0, 0);//from w  w  w.ja  va2  s .  c o m
        canvas.translate(-height + getPaddingBottom(), 0);

        mEdgeGlowLeft.setSize(getRenderHeight(), getRenderWidth());
        if (mEdgeGlowLeft.draw(canvas)) {
            invalidate();
        }

        canvas.restoreToCount(restoreCount);
    } else if (mEdgeGlowRight != null && !mEdgeGlowRight.isFinished() && isEdgeGlowEnabled()) {
        // The Edge glow is meant to come from the top of the screen, so rotate it to draw on the bottom side.
        final int restoreCount = canvas.save();
        final int width = getWidth();

        canvas.rotate(90, 0, 0);
        canvas.translate(getPaddingTop(), -width);
        mEdgeGlowRight.setSize(getRenderHeight(), getRenderWidth());
        if (mEdgeGlowRight.draw(canvas)) {
            invalidate();
        }

        canvas.restoreToCount(restoreCount);
    }
}

From source file:aksha.upcomingdemo.HorizontalListView.java

/** Draws the overscroll edge glow effect on the left and right sides of the horizontal list */
private void drawEdgeGlow(Canvas canvas) {
    if (mEdgeGlowLeft != null && !mEdgeGlowLeft.isFinished() && isEdgeGlowEnabled()) {
        // The Edge glow is meant to come from the top of the screen, so rotate it to draw on the left side.
        final int restoreCount = canvas.save();
        final int height = getHeight();

        canvas.rotate(-90, 0, 0);//from  w w w .j  ava  2s  .  co m
        canvas.translate(-height + getPaddingBottom(), 0);

        mEdgeGlowLeft.setSize(getRenderHeight(), getRenderWidth());
        if (mEdgeGlowLeft.draw(canvas)) {
            invalidate();
        }

        canvas.restoreToCount(restoreCount);
    } else if (mEdgeGlowRight != null && !mEdgeGlowRight.isFinished() && isEdgeGlowEnabled()) {
        // The Edge glow is meant to come from the top of the screen, so rotate it to draw on the right side.
        final int restoreCount = canvas.save();
        final int width = getWidth();

        canvas.rotate(90, 0, 0);
        canvas.translate(getPaddingTop(), -width);
        mEdgeGlowRight.setSize(getRenderHeight(), getRenderWidth());
        if (mEdgeGlowRight.draw(canvas)) {
            invalidate();
        }

        canvas.restoreToCount(restoreCount);
    }
}

From source file:com.jjoe64.graphview.Viewport.java

/**
 * Draws the overscroll "glow" at the four edges of the chart region, if necessary.
 *
 * @see EdgeEffectCompat/*www  .  j  a v a  2s.  co m*/
 */
private void drawEdgeEffectsUnclipped(Canvas canvas) {
    // The methods below rotate and translate the canvas as needed before drawing the glow,
    // since EdgeEffectCompat always draws a top-glow at 0,0.

    boolean needsInvalidate = false;

    if (!mEdgeEffectTop.isFinished()) {
        final int restoreCount = canvas.save();
        canvas.translate(mGraphView.getGraphContentLeft(), mGraphView.getGraphContentTop());
        mEdgeEffectTop.setSize(mGraphView.getGraphContentWidth(), mGraphView.getGraphContentHeight());
        if (mEdgeEffectTop.draw(canvas)) {
            needsInvalidate = true;
        }
        canvas.restoreToCount(restoreCount);
    }

    if (!mEdgeEffectBottom.isFinished()) {
        final int restoreCount = canvas.save();
        canvas.translate(mGraphView.getGraphContentLeft(),
                mGraphView.getGraphContentTop() + mGraphView.getGraphContentHeight());
        canvas.rotate(180, mGraphView.getGraphContentWidth() / 2, 0);
        mEdgeEffectBottom.setSize(mGraphView.getGraphContentWidth(), mGraphView.getGraphContentHeight());
        if (mEdgeEffectBottom.draw(canvas)) {
            needsInvalidate = true;
        }
        canvas.restoreToCount(restoreCount);
    }

    if (!mEdgeEffectLeft.isFinished()) {
        final int restoreCount = canvas.save();
        canvas.translate(mGraphView.getGraphContentLeft(),
                mGraphView.getGraphContentTop() + mGraphView.getGraphContentHeight());
        canvas.rotate(-90, 0, 0);
        mEdgeEffectLeft.setSize(mGraphView.getGraphContentHeight(), mGraphView.getGraphContentWidth());
        if (mEdgeEffectLeft.draw(canvas)) {
            needsInvalidate = true;
        }
        canvas.restoreToCount(restoreCount);
    }

    if (!mEdgeEffectRight.isFinished()) {
        final int restoreCount = canvas.save();
        canvas.translate(mGraphView.getGraphContentLeft() + mGraphView.getGraphContentWidth(),
                mGraphView.getGraphContentTop());
        canvas.rotate(90, 0, 0);
        mEdgeEffectRight.setSize(mGraphView.getGraphContentHeight(), mGraphView.getGraphContentWidth());
        if (mEdgeEffectRight.draw(canvas)) {
            needsInvalidate = true;
        }
        canvas.restoreToCount(restoreCount);
    }

    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(mGraphView);
    }
}

From source file:org.telegram.ui.ActionBar.ActionBarLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    int width = getWidth() - getPaddingLeft() - getPaddingRight();
    int translationX = (int) innerTranslationX + getPaddingRight();
    int clipLeft = getPaddingLeft();
    int clipRight = width + getPaddingLeft();

    if (child == containerViewBack) {
        clipRight = translationX;/*from ww  w. ja  va 2s .c  om*/
    } else if (child == containerView) {
        clipLeft = translationX;
    }

    final int restoreCount = canvas.save();
    if (!transitionAnimationInProgress) {
        canvas.clipRect(clipLeft, 0, clipRight, getHeight());
    }
    final boolean result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(restoreCount);

    if (translationX != 0) {
        if (child == containerView) {
            final float alpha = Math.max(0,
                    Math.min((width - translationX) / (float) AndroidUtilities.dp(20), 1.0f));
            layerShadowDrawable.setBounds(translationX - layerShadowDrawable.getIntrinsicWidth(),
                    child.getTop(), translationX, child.getBottom());
            layerShadowDrawable.setAlpha((int) (0xff * alpha));
            layerShadowDrawable.draw(canvas);
        } else if (child == containerViewBack) {
            float opacity = Math.min(0.8f, (width - translationX) / (float) width);
            if (opacity < 0) {
                opacity = 0;
            }
            scrimPaint.setColor((int) (((0x99000000 & 0xff000000) >>> 24) * opacity) << 24);
            canvas.drawRect(clipLeft, 0, clipRight, getHeight(), scrimPaint);
        }
    }

    return result;
}

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

private void drawShadow(Canvas canvas) {
    final int rotateSaved = canvas.save();
    canvas.rotate(mRotation, mContentBounds.centerX(), mContentBounds.centerY());

    final float edgeShadowTop = -mCornerRadius - mShadowSize;
    final float shadowOffset = mCornerRadius;
    final boolean drawHorizontalEdges = mContentBounds.width() - 2 * shadowOffset > 0;
    final boolean drawVerticalEdges = mContentBounds.height() - 2 * shadowOffset > 0;

    final float shadowOffsetTop = mRawShadowSize - (mRawShadowSize * SHADOW_TOP_SCALE);
    final float shadowOffsetHorizontal = mRawShadowSize - (mRawShadowSize * SHADOW_HORIZ_SCALE);
    final float shadowOffsetBottom = mRawShadowSize - (mRawShadowSize * SHADOW_BOTTOM_SCALE);

    final float shadowScaleHorizontal = shadowOffset / (shadowOffset + shadowOffsetHorizontal);
    final float shadowScaleTop = shadowOffset / (shadowOffset + shadowOffsetTop);
    final float shadowScaleBottom = shadowOffset / (shadowOffset + shadowOffsetBottom);

    // LT// www  .jav  a  2 s. c o  m
    int saved = canvas.save();
    canvas.translate(mContentBounds.left + shadowOffset, mContentBounds.top + shadowOffset);
    canvas.scale(shadowScaleHorizontal, shadowScaleTop);
    canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
    if (drawHorizontalEdges) {
        // TE
        canvas.scale(1f / shadowScaleHorizontal, 1f);
        canvas.drawRect(0, edgeShadowTop, mContentBounds.width() - 2 * shadowOffset, -mCornerRadius,
                mEdgeShadowPaint);
    }
    canvas.restoreToCount(saved);
    // RB
    saved = canvas.save();
    canvas.translate(mContentBounds.right - shadowOffset, mContentBounds.bottom - shadowOffset);
    canvas.scale(shadowScaleHorizontal, shadowScaleBottom);
    canvas.rotate(180f);
    canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
    if (drawHorizontalEdges) {
        // BE
        canvas.scale(1f / shadowScaleHorizontal, 1f);
        canvas.drawRect(0, edgeShadowTop, mContentBounds.width() - 2 * shadowOffset,
                -mCornerRadius + mShadowSize, mEdgeShadowPaint);
    }
    canvas.restoreToCount(saved);
    // LB
    saved = canvas.save();
    canvas.translate(mContentBounds.left + shadowOffset, mContentBounds.bottom - shadowOffset);
    canvas.scale(shadowScaleHorizontal, shadowScaleBottom);
    canvas.rotate(270f);
    canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
    if (drawVerticalEdges) {
        // LE
        canvas.scale(1f / shadowScaleBottom, 1f);
        canvas.drawRect(0, edgeShadowTop, mContentBounds.height() - 2 * shadowOffset, -mCornerRadius,
                mEdgeShadowPaint);
    }
    canvas.restoreToCount(saved);
    // RT
    saved = canvas.save();
    canvas.translate(mContentBounds.right - shadowOffset, mContentBounds.top + shadowOffset);
    canvas.scale(shadowScaleHorizontal, shadowScaleTop);
    canvas.rotate(90f);
    canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
    if (drawVerticalEdges) {
        // RE
        canvas.scale(1f / shadowScaleTop, 1f);
        canvas.drawRect(0, edgeShadowTop, mContentBounds.height() - 2 * shadowOffset, -mCornerRadius,
                mEdgeShadowPaint);
    }
    canvas.restoreToCount(saved);

    canvas.restoreToCount(rotateSaved);
}

From source file:uk.ac.kent.jb509.shopper.utils.SlidingUpPanelLayout.java

@Override
protected boolean drawChild(final Canvas canvas, final View child, final 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.
        canvas.getClipBounds(mTmpRect);//from   w ww.j  a v a  2s . c om
        mTmpRect.bottom = Math.min(mTmpRect.bottom, mSlideableView.getTop());

        if (!mIsTransparent) {
            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;
}

From source file:org.buffer.android.buffertextinputlayout.util.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;
        if (drawTexture) {
            ascent = mTextureAscent * mScale;
            descent = mTextureDescent * mScale;
        } else {/*  w  ww .j  a  v  a  2s. c o  m*/
            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;
        }
        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.amitupadhyay.aboutexample.util.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 ww .j  a v  a  2s  .  c o 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);
}