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: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);// w w  w.  java2  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 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/*from  w ww  .  ja va  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:com.achep.acdisplay.ui.widgets.CircleView.java

@Override
protected void onDraw(Canvas canvas) {
    final float ratio = calculateRatio();

    // Draw all corners
    drawCornerIcon(canvas, mDrawableLeftTopCorner, 0, 0 /* left top */);
    drawCornerIcon(canvas, mDrawableRightTopCorner, 1, 0 /* right top */);
    drawCornerIcon(canvas, mDrawableLeftBottomCorner, 0, 1 /* left bottom */);
    drawCornerIcon(canvas, mDrawableRightBottomCorner, 1, 1 /* right bottom */);

    // Darkening background
    int alpha = (int) (mDarkening * 255);
    alpha += (int) ((255 - alpha) * ratio * 0.7f); // Change alpha dynamically
    canvas.drawColor(//from   w  w w  .j  a va  2 s  .c  o  m
            Color.argb(alpha, Color.red(mOuterColor), Color.green(mOuterColor), Color.blue(mOuterColor)));

    // Draw unlock circle
    mPaint.setColor(mInnerColor);
    mPaint.setAlpha((int) (255 * Math.pow(ratio, 1f / 3f)));
    canvas.drawCircle(mPoint[0], mPoint[1], mRadiusDrawn, mPaint);

    if (ratio >= 0.5f) {
        // Draw unlock icon at the center of circle
        float scale = 0.5f + 0.5f * ratio;
        canvas.save();
        canvas.translate(mPoint[0] - mDrawable.getMinimumWidth() / 2 * scale,
                mPoint[1] - mDrawable.getMinimumHeight() / 2 * scale);
        canvas.scale(scale, scale);
        mDrawable.draw(canvas);
        canvas.restore();
    }
}

From source file:com.hippo.widget.recyclerview.EasyRecyclerView.java

@Override
protected void dispatchDraw(@NonNull Canvas canvas) {
    boolean clipToPadding = mClipToPadding;
    int saveCount = 0;
    if (clipToPadding) {
        saveCount = canvas.save();
        final int scrollX = getScrollX();
        final int scrollY = getScrollY();
        canvas.clipRect(scrollX + getPaddingLeft(), scrollY + getPaddingTop(),
                scrollX + getRight() - getLeft() - getPaddingRight(),
                scrollY + getBottom() - getTop() - getPaddingBottom());
    }//  ww  w  .  j a v  a  2  s. co  m

    // TODO disable selector drawable state change when need not to draw selector
    boolean drawSelector = mOnDrawSelectorListener == null || mSelectorPosition < 0
            || mSelectorPosition >= mAdapter.getItemCount()
            || mOnDrawSelectorListener.beforeDrawSelector(mSelectorPosition);
    final boolean drawSelectorOnTop = mDrawSelectorOnTop;

    if (drawSelector && !drawSelectorOnTop) {
        drawSelector(canvas);
    }

    super.dispatchDraw(canvas);

    if (drawSelector && drawSelectorOnTop) {
        drawSelector(canvas);
    }

    if (clipToPadding) {
        canvas.restoreToCount(saveCount);
    }
}

From source file:com.android.leanlauncher.Workspace.java

/**
 * Draw the View v into the given Canvas.
 *
 * @param v the view to draw//from w  w  w .  java  2  s .c om
 * @param destCanvas the canvas to draw on
 * @param padding the horizontal and vertical padding to use when drawing
 */
private static void drawDragView(View v, Canvas destCanvas, int padding) {
    final Rect clipRect = sTempRect;
    v.getDrawingRect(clipRect);

    destCanvas.save();
    if (v instanceof TextView) {
        Drawable d = ((TextView) v).getCompoundDrawables()[1];
        Rect bounds = getDrawableBounds(d);
        clipRect.set(0, 0, bounds.width() + padding, bounds.height() + padding);
        destCanvas.translate(padding / 2 - bounds.left, padding / 2 - bounds.top);
        d.draw(destCanvas);
    } else {
        destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
        destCanvas.clipRect(clipRect, Op.REPLACE);
        v.draw(destCanvas);
    }
    destCanvas.restore();
}

From source file:org.connectbot.TerminalView.java

@Override
public void onDraw(Canvas canvas) {
    if (bridge.bitmap != null) {
        // draw the bitmap
        bridge.onDraw();/*from  w  ww . ja v  a  2s.com*/

        // draw the bridge bitmap if it exists
        canvas.drawBitmap(bridge.bitmap, 0, 0, paint);

        // also draw cursor if visible
        if (bridge.buffer.isCursorVisible()) {
            int cursorColumn = bridge.buffer.getCursorColumn();
            final int cursorRow = bridge.buffer.getCursorRow();

            final int columns = bridge.buffer.getColumns();

            if (cursorColumn == columns)
                cursorColumn = columns - 1;

            if (cursorColumn < 0 || cursorRow < 0)
                return;

            int currentAttribute = bridge.buffer.getAttributes(cursorColumn, cursorRow);
            boolean onWideCharacter = (currentAttribute & VDUBuffer.FULLWIDTH) != 0;

            int x = cursorColumn * bridge.charWidth;
            int y = (bridge.buffer.getCursorRow() + bridge.buffer.screenBase - bridge.buffer.windowBase)
                    * bridge.charHeight;

            // Save the current clip and translation
            canvas.save();

            canvas.translate(x, y);
            canvas.clipRect(0, 0, bridge.charWidth * (onWideCharacter ? 2 : 1), bridge.charHeight);
            canvas.drawPaint(cursorPaint);

            final int deadKey = bridge.getKeyHandler().getDeadKey();
            if (deadKey != 0) {
                canvas.drawText(new char[] { (char) deadKey }, 0, 1, 0, 0, cursorStrokePaint);
            }

            // Make sure we scale our decorations to the correct size.
            canvas.concat(scaleMatrix);

            int metaState = bridge.getKeyHandler().getMetaState();

            if ((metaState & TerminalKeyListener.OUR_SHIFT_ON) != 0)
                canvas.drawPath(shiftCursor, cursorStrokePaint);
            else if ((metaState & TerminalKeyListener.OUR_SHIFT_LOCK) != 0)
                canvas.drawPath(shiftCursor, cursorPaint);

            if ((metaState & TerminalKeyListener.OUR_ALT_ON) != 0)
                canvas.drawPath(altCursor, cursorStrokePaint);
            else if ((metaState & TerminalKeyListener.OUR_ALT_LOCK) != 0)
                canvas.drawPath(altCursor, cursorPaint);

            if ((metaState & TerminalKeyListener.OUR_CTRL_ON) != 0)
                canvas.drawPath(ctrlCursor, cursorStrokePaint);
            else if ((metaState & TerminalKeyListener.OUR_CTRL_LOCK) != 0)
                canvas.drawPath(ctrlCursor, cursorPaint);

            // Restore previous clip region
            canvas.restore();
        }

        // draw any highlighted area
        if (bridge.isSelectingForCopy()) {
            SelectionArea area = bridge.getSelectionArea();
            canvas.save(Canvas.CLIP_SAVE_FLAG);
            canvas.clipRect(area.getLeft() * bridge.charWidth, area.getTop() * bridge.charHeight,
                    (area.getRight() + 1) * bridge.charWidth, (area.getBottom() + 1) * bridge.charHeight);
            canvas.drawPaint(cursorPaint);
            canvas.restore();
        }
    }
}

From source file:com.irccloud.android.DrawerArrowDrawable.java

@Override
public void draw(Canvas canvas) {
    Rect bounds = getBounds();// w w  w  . j  av a 2 s.  c o  m
    final boolean isRtl = isLayoutRtl();
    // Interpolated widths of arrow bars
    final float arrowSize = lerp(mBarSize, mTopBottomArrowSize, mProgress);
    final float middleBarSize = lerp(mBarSize, mMiddleArrowSize, mProgress);
    // Interpolated size of middle bar
    final float middleBarCut = lerp(0, mBarThickness / 2, 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(isRtl ? 0 : -180, isRtl ? 180 : 0, mProgress);
    final float topBottomBarOffset = lerp(mBarGap + mBarThickness, 0, mProgress);
    mPath.rewind();

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

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

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

    // bottom bar
    mPath.moveTo(arrowEdge, -topBottomBarOffset);
    mPath.rLineTo(arrowWidth, -arrowHeight);
    mPath.moveTo(0, 0);
    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.
    if (mSpin) {
        canvas.rotate(canvasRotate * ((mVerticalMirror ^ isRtl) ? -1 : 1), bounds.centerX(), bounds.centerY());
    } else if (isRtl) {
        canvas.rotate(180, bounds.centerX(), bounds.centerY());
    }
    canvas.translate(bounds.centerX(), bounds.centerY());
    canvas.drawPath(mPath, mPaint);

    canvas.restore();
}

From source file:com.camnter.easyrecyclerviewsidebar.EasyRecyclerViewSidebar.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int viewWidth = this.getWidth();
    int viewHeight = this.getHeight();
    int viewHalfWidth = viewWidth / 2;
    this.sectionHeight = viewHeight / MAX_SECTION_COUNT;
    boolean isPreviousImage = false;
    boolean isPreviousLetter = false;
    float allSectionHeight;
    if (this.sections.size() > 0) {
        allSectionHeight = this.sectionHeight * this.sections.size();
        this.drawBeginY = (viewHeight - allSectionHeight) / 2;
        this.drawEndY = this.drawBeginY + allSectionHeight;
        float top = viewHeight / 2 - allSectionHeight / 2 + this.sectionHeight / 2 - this.sectionFontSize / 2;
        for (int i = 0; i < this.sections.size(); i++) {
            EasySection section = this.sections.get(i);
            if (section instanceof EasyImageSection) {
                EasyImageSection imageSection = (EasyImageSection) section;
                this.setPaintShader(imageSection);
                if (isPreviousLetter) {
                    top -= this.letterSize - (Math.max(this.letterHeight, this.sectionHeight)
                            - Math.min(this.letterHeight, this.sectionHeight));
                }//from ww  w.  j a  va2s .  c o  m
                canvas.save();
                canvas.translate(viewHalfWidth - this.letterSize / 2, top + this.sectionHeight * i);
                Paint imagePaint = this.imagePaints.get(imageSection.hashCode());
                switch (imageSection.imageType) {
                case EasyImageSection.ROUND: {
                    canvas.drawRoundRect(this.imageSectionRect, this.imageSectionBorderRadius,
                            this.imageSectionBorderRadius, imagePaint);
                    break;
                }
                case EasyImageSection.CIRCLE: {
                    canvas.drawRoundRect(this.imageSectionRect, this.imageSectionCircleBorderRadius,
                            this.imageSectionCircleBorderRadius, imagePaint);
                    break;
                }
                }
                canvas.restore();
                isPreviousImage = true;
                isPreviousLetter = false;
            } else {
                if (isPreviousImage) {
                    top = top + this.letterSize;
                }
                canvas.drawText(section.letter, viewHalfWidth, top + this.sectionHeight * i, this.letterPaint);
                isPreviousImage = false;
                isPreviousLetter = true;
            }
        }
    } else {
        this.sectionHeight = 0;
    }
}

From source file:cl.monsoon.s1next.widget.PhotoView.java

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

    // draw the photo
    if (mDrawable != null) {
        int saveCount = canvas.getSaveCount();
        canvas.save();

        if (mDrawMatrix != null) {
            canvas.concat(mDrawMatrix);//from   www.j  av a 2  s .  c  o m
        }
        mDrawable.draw(canvas);

        canvas.restoreToCount(saveCount);

        if (mVideoBlob != null) {
            final Bitmap videoImage = (mVideoReady ? sVideoImage : sVideoNotReadyImage);
            final int drawLeft = (getWidth() - videoImage.getWidth()) / 2;
            final int drawTop = (getHeight() - videoImage.getHeight()) / 2;
            canvas.drawBitmap(videoImage, drawLeft, drawTop, null);
        }

        // Extract the drawable's bounds (in our own copy, to not alter the image)
        mTranslateRect.set(mDrawable.getBounds());
        if (mDrawMatrix != null) {
            mDrawMatrix.mapRect(mTranslateRect);
        }

        if (mAllowCrop) {
            int previousSaveCount = canvas.getSaveCount();
            canvas.drawRect(0, 0, getWidth(), getHeight(), sCropDimPaint);
            canvas.save();
            canvas.clipRect(mCropRect);

            if (mDrawMatrix != null) {
                canvas.concat(mDrawMatrix);
            }

            mDrawable.draw(canvas);
            canvas.restoreToCount(previousSaveCount);
            canvas.drawRect(mCropRect, sCropPaint);
        }
    }
}

From source file:martin.app.bitunion.widget.PhotoView.java

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

    // draw the photo
    if (mDrawable != null) {
        int saveCount = canvas.getSaveCount();
        canvas.save();

        if (mDrawMatrix != null) {
            canvas.concat(mDrawMatrix);//w  w  w .j  a va 2  s .  com
        }
        mDrawable.draw(canvas);

        canvas.restoreToCount(saveCount);

        if (mVideoBlob != null) {
            final Bitmap videoImage = (mVideoReady ? sVideoImage : sVideoNotReadyImage);
            final int drawLeft = (getWidth() - videoImage.getWidth()) / 2;
            final int drawTop = (getHeight() - videoImage.getHeight()) / 2;
            canvas.drawBitmap(videoImage, drawLeft, drawTop, null);
        }

        // Extract the drawable's bounds (in our own copy, to not alter the image)
        mTranslateRect.set(mDrawable.getBounds());
        if (mDrawMatrix != null) {
            mDrawMatrix.mapRect(mTranslateRect);
        }

        if (mAllowCrop) {
            int previousSaveCount = canvas.getSaveCount();
            canvas.drawRect(0, 0, getWidth(), getHeight(), sCropDimPaint);
            canvas.save();
            canvas.clipRect(mCropRect);

            if (mDrawMatrix != null) {
                canvas.concat(mDrawMatrix);
            }

            mDrawable.draw(canvas);
            canvas.restoreToCount(previousSaveCount);
            canvas.drawRect(mCropRect, sCropPaint);
        }
    }
}