Example usage for android.graphics Canvas save

List of usage examples for android.graphics Canvas save

Introduction

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

Prototype

public int save() 

Source Link

Document

Saves the current matrix and clip onto a private stack.

Usage

From source file:android.support.design.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;
        if (drawTexture) {
            ascent = mTextureAscent * mScale;
            descent = mTextureDescent * mScale;
        } else {//  www  . j a v  a  2 s  .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.gj.administrator.gjerp.view.CircleIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    Log.e("CircleIndicator", "onDraw()");
    super.onDraw(canvas);
    int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null,
            Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
                    | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
    for (ShapeHolder item : tabItems) {
        canvas.save();
        canvas.translate(item.getX(), item.getY());
        item.getShape().draw(canvas);//from w  ww.j  av  a  2 s .  co m
        canvas.restore();
    }

    if (movingItem != null) {
        canvas.save();
        canvas.translate(movingItem.getX(), movingItem.getY());
        movingItem.getShape().draw(canvas);
        canvas.restore();
    }
    canvas.restoreToCount(sc);
}

From source file:com.brian.common.view.DrawerArrowDrawable.java

@Override
public void draw(Canvas canvas) {
    if (mIsCirculate) {
        if (mProgress >= .995) {
            flipped = true;/*from ww w.  ja va  2s .c  om*/
        } else if (mProgress <= .005) {
            flipped = false;
        }
        if (flipped) {
            canvas.save();
            canvas.scale(1f, -1f, getIntrinsicWidth() / 2, getIntrinsicHeight() / 2);
        }
    }

    Rect bounds = getBounds();

    final boolean flipToPointRight;
    switch (mDirection) {
    case ARROW_DIRECTION_LEFT:
        flipToPointRight = false;
        break;
    case ARROW_DIRECTION_RIGHT:
        flipToPointRight = true;
        break;
    default:
        flipToPointRight = false;
        break;
    }

    // Interpolated widths of arrow bars

    float arrowHeadBarLength = (float) Math.sqrt(mArrowHeadLength * mArrowHeadLength * 2);
    arrowHeadBarLength = lerp(mBarLength, arrowHeadBarLength, mProgress);
    final float arrowShaftLength = lerp(mBarLength, mArrowShaftLength, mProgress);
    // Interpolated size of middle bar
    final float arrowShaftCut = Math.round(lerp(0, mMaxCutForBarSize, mProgress));
    // The rotation of the top and bottom bars (that make the arrow head)
    final float rotation = lerp(0, ARROW_HEAD_ANGLE, mProgress);

    // The whole canvas rotates as the transition happens
    final float canvasRotate = lerp(flipToPointRight ? 0 : -180, flipToPointRight ? 180 : 0, mProgress);

    final float arrowWidth = Math.round(arrowHeadBarLength * Math.cos(rotation));
    final float arrowHeight = Math.round(arrowHeadBarLength * Math.sin(rotation));

    mPath.rewind();
    final float topBottomBarOffset = lerp(mBarGap + mPaint.getStrokeWidth(), -mMaxCutForBarSize, mProgress);

    final float arrowEdge = -arrowShaftLength / 2;
    // draw middle bar
    mPath.moveTo(arrowEdge + arrowShaftCut, 0);
    mPath.rLineTo(arrowShaftLength - arrowShaftCut * 2, 0);

    // bottom bar
    mPath.moveTo(arrowEdge, topBottomBarOffset);
    mPath.rLineTo(arrowWidth, arrowHeight);

    // top bar
    mPath.moveTo(arrowEdge, -topBottomBarOffset);
    mPath.rLineTo(arrowWidth, -arrowHeight);

    mPath.close();

    canvas.save();

    // Rotate the whole canvas if spinning, if not, rotate it 180 to get
    // the arrow pointing the other way for RTL.
    final float barThickness = mPaint.getStrokeWidth();
    final int remainingSpace = (int) (bounds.height() - barThickness * 3 - mBarGap * 2);
    float yOffset = (remainingSpace / 4) * 2; // making sure it is a multiple of 2.
    yOffset += barThickness * 1.5 + mBarGap;

    canvas.translate(bounds.centerX(), yOffset);
    if (mSpin) {
        canvas.rotate(canvasRotate * ((mVerticalMirror ^ flipToPointRight) ? -1 : 1));
    } else if (flipToPointRight) {
        canvas.rotate(180);
    }
    canvas.drawPath(mPath, mPaint);

    canvas.restore();
}

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

/**
 * Draw the tick marks./* w w w.j  a v a2s. com*/
 */
void drawTickMarks(Canvas canvas) {
    if (mTickMark != null) {
        final int count = mView.getMax();
        if (count > 1) {
            final int w = mTickMark.getIntrinsicWidth();
            final int h = mTickMark.getIntrinsicHeight();
            final int halfW = w >= 0 ? w / 2 : 1;
            final int halfH = h >= 0 ? h / 2 : 1;
            mTickMark.setBounds(-halfW, -halfH, halfW, halfH);

            final float spacing = (mView.getWidth() - mView.getPaddingLeft() - mView.getPaddingRight())
                    / (float) count;
            final int saveCount = canvas.save();
            canvas.translate(mView.getPaddingLeft(), mView.getHeight() / 2);
            for (int i = 0; i <= count; i++) {
                mTickMark.draw(canvas);
                canvas.translate(spacing, 0);
            }
            canvas.restoreToCount(saveCount);
        }
    }
}

From source file:com.emmaguy.todayilearned.PanView.java

/**
 * Draws the overscroll "glow" at the four edges, if necessary
 *
 * @see EdgeEffect/* ww w. j  a v  a2s .c  om*/
 */
private void drawEdgeEffects(Canvas canvas) {
    // The methods below rotate and translate the canvas as needed before drawing the glow,
    // since EdgeEffect always draws a top-glow at 0,0.

    boolean needsInvalidate = false;

    if (!mEdgeEffectTop.isFinished()) {
        final int restoreCount = canvas.save();
        mEdgeEffectTop.setSize(mWidth, mHeight);
        if (mEdgeEffectTop.draw(canvas)) {
            needsInvalidate = true;
        }
        canvas.restoreToCount(restoreCount);
    }

    if (!mEdgeEffectBottom.isFinished()) {
        final int restoreCount = canvas.save();
        canvas.translate(-mWidth, mHeight);
        canvas.rotate(180, mWidth, 0);
        mEdgeEffectBottom.setSize(mWidth, mHeight);
        if (mEdgeEffectBottom.draw(canvas)) {
            needsInvalidate = true;
        }
        canvas.restoreToCount(restoreCount);
    }

    if (!mEdgeEffectLeft.isFinished()) {
        final int restoreCount = canvas.save();
        canvas.translate(0, mHeight);
        canvas.rotate(-90, 0, 0);
        //noinspection SuspiciousNameCombination
        mEdgeEffectLeft.setSize(mHeight, mWidth);
        if (mEdgeEffectLeft.draw(canvas)) {
            needsInvalidate = true;
        }
        canvas.restoreToCount(restoreCount);
    }

    if (!mEdgeEffectRight.isFinished()) {
        final int restoreCount = canvas.save();
        canvas.translate(mWidth, 0);
        canvas.rotate(90, 0, 0);
        //noinspection SuspiciousNameCombination
        mEdgeEffectRight.setSize(mHeight, mWidth);
        if (mEdgeEffectRight.draw(canvas)) {
            needsInvalidate = true;
        }
        canvas.restoreToCount(restoreCount);
    }

    if (needsInvalidate) {
        invalidate();
    }
}

From source file:com.example.carlitos.swipeitemrecycler.view.animation.swipe_item.swipeable.RemovingItemDecorator.java

private void fillSwipingItemBackground(Canvas c, Drawable drawable, float scale) {
    final Rect bounds = mSwipingItemBounds;
    final int translationX = mTranslationX;
    final int translationY = mTranslationY;
    final float hScale = (mHorizontal) ? 1.0f : scale;
    final float vScale = (mHorizontal) ? scale : 1.0f;

    int width = (int) (hScale * bounds.width() + 0.5f);
    int height = (int) (vScale * bounds.height() + 0.5f);

    if ((height == 0) || (width == 0) || (drawable == null)) {
        return;/*from  w  ww.  ja  v  a  2s  .co  m*/
    }

    final int savedCount = c.save();

    c.clipRect(bounds.left + translationX, bounds.top + translationY, bounds.left + translationX + width,
            bounds.top + translationY + height);

    // c.drawColor(0xffff0000); // <-- debug

    c.translate(bounds.left + translationX - (bounds.width() - width) / 2,
            bounds.top + translationY - (bounds.height() - height) / 2);
    drawable.setBounds(0, 0, bounds.width(), bounds.height());

    drawable.draw(c);

    c.restoreToCount(savedCount);
}

From source file:android.support.wear.widget.drawer.PageIndicatorView.java

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

    if (mNumberOfPositions > 1) {
        float dotCenterLeft = getPaddingLeft() + (mDotSpacing / 2f);
        float dotCenterTop = getHeight() / 2f;
        canvas.save();
        canvas.translate(dotCenterLeft, dotCenterTop);
        for (int i = 0; i < mNumberOfPositions; i++) {
            if (i == mSelectedPosition) {
                float radius = mDotRadiusSelected + mDotShadowRadius;
                canvas.drawCircle(mDotShadowDx, mDotShadowDy, radius, mDotPaintShadowSelected);
                canvas.drawCircle(0, 0, mDotRadiusSelected, mDotPaintSelected);
            } else {
                float radius = mDotRadius + mDotShadowRadius;
                canvas.drawCircle(mDotShadowDx, mDotShadowDy, radius, mDotPaintShadow);
                canvas.drawCircle(0, 0, mDotRadius, mDotPaint);
            }/* w w  w  .j ava 2 s.co m*/
            canvas.translate(mDotSpacing, 0);
        }
        canvas.restore();
    }
}

From source file:com.am.pagergradienttab.view.PagerGradientTabStrip.java

/**
 * /*ww  w  .j  av a2  s  .c om*/
 * 
 * @param canvas
 */
private void drawIndicator(Canvas canvas) {

    canvas.save();
    canvas.translate(
            getPaddingLeft() + currectPager * (itemWidth + intervalWidth) + indicatorPadding
                    + indicatorOffset * (itemWidth + intervalWidth),
            getHeight() - underLineHeight - getPaddingBottom() - indicatorHeight);
    if (showIndicator) {
        mTextPaint.setColor(indicatorColor);
        if (tabs.size() > 1)
            canvas.drawRect(0, 0, itemWidth - 2 * indicatorPadding, indicatorHeight, mTextPaint);
        else if (tabs.size() > 0)
            canvas.drawRect(itemWidth / 4, 0, itemWidth - itemWidth / 4 - 2 * indicatorPadding, indicatorHeight,
                    mTextPaint);
        else
            canvas.drawRect(itemWidth / 4, 0, itemWidth - itemWidth / 4 - 1 * indicatorPadding, indicatorHeight,
                    mTextPaint);
    }
    canvas.restore();
}

From source file:com.apptentive.android.sdk.module.messagecenter.view.MessageCenterListView.java

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

    if (stickyWrapper != null) {

        int pLeft = getListPaddingLeft();
        int pTop = getListPaddingTop();
        View view = stickyWrapper.view;
        int headerTop = view.getTop();
        pLeft += stickyWrapper.additionalIndent;
        // draw child
        canvas.save();

        int clipHeight = view.getHeight() + (shadowDrawable == null ? 0 : shadowHeight);
        canvas.clipRect(pLeft, pTop, pLeft + view.getWidth() - 2 * stickyWrapper.additionalIndent,
                pTop + clipHeight);// www .j  a  va  2 s . c om

        canvas.translate(pLeft - stickyWrapper.additionalIndent, pTop - headerTop);
        drawChild(canvas, stickyWrapper.view, getDrawingTime());

        if (shadowDrawable != null) {
            shadowDrawable.setBounds(stickyWrapper.view.getLeft(), stickyWrapper.view.getBottom(),
                    stickyWrapper.view.getRight(), stickyWrapper.view.getBottom() + shadowHeight);
            shadowDrawable.draw(canvas);
        }

        canvas.restore();
    }
}

From source file:ezy.ui.view.ViewPagerIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int flags = Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
            | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG;
    int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, flags);

    int wg = mItemWidth + mItemGap;
    int x = (getWidth() - mWidth) / 2;
    int y = (getHeight() - mItemHeight) / 2;

    mItemDrawable.setBounds(0, 0, mItemWidth, mItemHeight);
    mItemDrawableSelected.setBounds(0, 0, mItemWidth, mItemHeight);

    for (int i = 0; i < mItemCount; i++) {
        canvas.save();
        canvas.translate(x + i * wg, y);
        mItemDrawable.draw(canvas);//from   ww  w .  j av  a  2s .  c  om
        canvas.restore();
    }

    canvas.save();
    canvas.translate(x + (mPosition + mPositionOffset) * wg, y);
    mItemDrawableSelected.draw(canvas);
    canvas.restore();

    canvas.restoreToCount(sc);
}