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.CircularBorderDrawable.java

@Override
public void draw(Canvas canvas) {
    if (mInvalidateShader) {
        mPaint.setShader(createGradientShader());
        mInvalidateShader = false;// w  ww .  ja v a 2s .  c o m
    }

    final float halfBorderWidth = mPaint.getStrokeWidth() / 2f;
    final RectF rectF = mRectF;

    // We need to inset the oval bounds by half the border width. This is because stroke draws
    // the center of the border on the dimension. Whereas we want the stroke on the inside.
    copyBounds(mRect);
    rectF.set(mRect);
    rectF.left += halfBorderWidth;
    rectF.top += halfBorderWidth;
    rectF.right -= halfBorderWidth;
    rectF.bottom -= halfBorderWidth;

    canvas.save();
    canvas.rotate(mRotation, rectF.centerX(), rectF.centerY());
    // Draw the oval
    canvas.drawOval(rectF, mPaint);
    canvas.restore();
}

From source file:com.example.gatsu.theevent.HighlightView.java

protected void draw(Canvas canvas) {

    if (mHidden) {
        return;//  w  w w.jav a  2  s . c  o m
    }

    Path path = new Path();
    if (!hasFocus()) {
        mOutlinePaint.setColor(0xFF000000);
        canvas.drawRect(mDrawRect, mOutlinePaint);
    } else {
        Rect viewDrawingRect = new Rect();
        mContext.getDrawingRect(viewDrawingRect);
        if (mCircle) {

            canvas.save();

            float width = mDrawRect.width();
            float height = mDrawRect.height();
            path.addCircle(mDrawRect.left + (width / 2), mDrawRect.top + (height / 2), width / 2,
                    Path.Direction.CW);
            mOutlinePaint.setColor(0xFFEF04D6);

            canvas.clipPath(path, Region.Op.DIFFERENCE);
            canvas.drawRect(viewDrawingRect, hasFocus() ? mFocusPaint : mNoFocusPaint);

            canvas.restore();

        } else {

            Rect topRect = new Rect(viewDrawingRect.left, viewDrawingRect.top, viewDrawingRect.right,
                    mDrawRect.top);
            if (topRect.width() > 0 && topRect.height() > 0) {
                canvas.drawRect(topRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect bottomRect = new Rect(viewDrawingRect.left, mDrawRect.bottom, viewDrawingRect.right,
                    viewDrawingRect.bottom);
            if (bottomRect.width() > 0 && bottomRect.height() > 0) {
                canvas.drawRect(bottomRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect leftRect = new Rect(viewDrawingRect.left, topRect.bottom, mDrawRect.left, bottomRect.top);
            if (leftRect.width() > 0 && leftRect.height() > 0) {
                canvas.drawRect(leftRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect rightRect = new Rect(mDrawRect.right, topRect.bottom, viewDrawingRect.right, bottomRect.top);
            if (rightRect.width() > 0 && rightRect.height() > 0) {
                canvas.drawRect(rightRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }

            path.addRect(new RectF(mDrawRect), Path.Direction.CW);

            mOutlinePaint.setColor(0xFFFF8A00);

        }

        canvas.drawPath(path, mOutlinePaint);

        if (mMode == ModifyMode.Grow) {
            if (mCircle) {
                int width = mResizeDrawableDiagonal.getIntrinsicWidth();
                int height = mResizeDrawableDiagonal.getIntrinsicHeight();

                int d = (int) Math.round(Math.cos(/*45deg*/Math.PI / 4D) * (mDrawRect.width() / 2D));
                int x = mDrawRect.left + (mDrawRect.width() / 2) + d - width / 2;
                int y = mDrawRect.top + (mDrawRect.height() / 2) - d - height / 2;
                mResizeDrawableDiagonal.setBounds(x, y, x + mResizeDrawableDiagonal.getIntrinsicWidth(),
                        y + mResizeDrawableDiagonal.getIntrinsicHeight());
                mResizeDrawableDiagonal.draw(canvas);
            } else {
                int left = mDrawRect.left + 1;
                int right = mDrawRect.right + 1;
                int top = mDrawRect.top + 4;
                int bottom = mDrawRect.bottom + 3;

                int widthWidth = mResizeDrawableWidth.getIntrinsicWidth() / 2;
                int widthHeight = mResizeDrawableWidth.getIntrinsicHeight() / 2;
                int heightHeight = mResizeDrawableHeight.getIntrinsicHeight() / 2;
                int heightWidth = mResizeDrawableHeight.getIntrinsicWidth() / 2;

                int xMiddle = mDrawRect.left + ((mDrawRect.right - mDrawRect.left) / 2);
                int yMiddle = mDrawRect.top + ((mDrawRect.bottom - mDrawRect.top) / 2);

                mResizeDrawableWidth.setBounds(left - widthWidth, yMiddle - widthHeight, left + widthWidth,
                        yMiddle + widthHeight);
                mResizeDrawableWidth.draw(canvas);

                mResizeDrawableWidth.setBounds(right - widthWidth, yMiddle - widthHeight, right + widthWidth,
                        yMiddle + widthHeight);
                mResizeDrawableWidth.draw(canvas);

                mResizeDrawableHeight.setBounds(xMiddle - heightWidth, top - heightHeight,
                        xMiddle + heightWidth, top + heightHeight);
                mResizeDrawableHeight.draw(canvas);

                mResizeDrawableHeight.setBounds(xMiddle - heightWidth, bottom - heightHeight,
                        xMiddle + heightWidth, bottom + heightHeight);
                mResizeDrawableHeight.draw(canvas);
            }
        }
    }
}

From source file:com.ssn.currency.convertor.WheelView.java

/**
 * Draws items// w  ww .j a  va  2s  . com
 * @param canvas the canvas for drawing
 */
private void drawItems(Canvas canvas) {
    canvas.save();

    int top = (currentItem - firstItem) * getItemHeight() + (getItemHeight() - getHeight()) / 2;
    canvas.translate(PADDING, -top + scrollingOffset);

    itemsLayout.draw(canvas);

    canvas.restore();
}

From source file:org.caojun.library.cropimage.HighlightView.java

protected void draw(Canvas canvas) {

    if (mHidden) {
        return;/*from w ww. ja  v  a 2 s. co m*/
    }

    Path path = new Path();
    if (!hasFocus()) {
        mOutlinePaint.setColor(0xFF000000);
        canvas.drawRect(mDrawRect, mOutlinePaint);
    } else {
        Rect viewDrawingRect = new Rect();
        mView.getDrawingRect(viewDrawingRect);
        if (mCircle) {

            canvas.save();

            float width = mDrawRect.width();
            float height = mDrawRect.height();
            path.addCircle(mDrawRect.left + (width / 2), mDrawRect.top + (height / 2), width / 2,
                    Path.Direction.CW);
            mOutlinePaint.setColor(0xFFEF04D6);

            canvas.clipPath(path, Region.Op.DIFFERENCE);
            canvas.drawRect(viewDrawingRect, hasFocus() ? mFocusPaint : mNoFocusPaint);

            canvas.restore();

        } else {

            Rect topRect = new Rect(viewDrawingRect.left, viewDrawingRect.top, viewDrawingRect.right,
                    mDrawRect.top);
            if (topRect.width() > 0 && topRect.height() > 0) {
                canvas.drawRect(topRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect bottomRect = new Rect(viewDrawingRect.left, mDrawRect.bottom, viewDrawingRect.right,
                    viewDrawingRect.bottom);
            if (bottomRect.width() > 0 && bottomRect.height() > 0) {
                canvas.drawRect(bottomRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect leftRect = new Rect(viewDrawingRect.left, topRect.bottom, mDrawRect.left, bottomRect.top);
            if (leftRect.width() > 0 && leftRect.height() > 0) {
                canvas.drawRect(leftRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect rightRect = new Rect(mDrawRect.right, topRect.bottom, viewDrawingRect.right, bottomRect.top);
            if (rightRect.width() > 0 && rightRect.height() > 0) {
                canvas.drawRect(rightRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }

            path.addRect(new RectF(mDrawRect), Path.Direction.CW);

            mOutlinePaint.setColor(0xFFFF8A00);

        }

        canvas.drawPath(path, mOutlinePaint);

        if (mMode == ModifyMode.Grow) {
            if (mCircle) {
                int width = mResizeDrawableDiagonal.getIntrinsicWidth();
                int height = mResizeDrawableDiagonal.getIntrinsicHeight();

                int d = (int) Math.round(Math.cos(/*45deg*/Math.PI / 4D) * (mDrawRect.width() / 2D));
                int x = mDrawRect.left + (mDrawRect.width() / 2) + d - width / 2;
                int y = mDrawRect.top + (mDrawRect.height() / 2) - d - height / 2;
                mResizeDrawableDiagonal.setBounds(x, y, x + mResizeDrawableDiagonal.getIntrinsicWidth(),
                        y + mResizeDrawableDiagonal.getIntrinsicHeight());
                mResizeDrawableDiagonal.draw(canvas);
            } else {
                int left = mDrawRect.left + 1;
                int right = mDrawRect.right + 1;
                int top = mDrawRect.top + 4;
                int bottom = mDrawRect.bottom + 3;

                int widthWidth = mResizeDrawableWidth.getIntrinsicWidth() / 2;
                int widthHeight = mResizeDrawableWidth.getIntrinsicHeight() / 2;
                int heightHeight = mResizeDrawableHeight.getIntrinsicHeight() / 2;
                int heightWidth = mResizeDrawableHeight.getIntrinsicWidth() / 2;

                int xMiddle = mDrawRect.left + ((mDrawRect.right - mDrawRect.left) / 2);
                int yMiddle = mDrawRect.top + ((mDrawRect.bottom - mDrawRect.top) / 2);

                mResizeDrawableWidth.setBounds(left - widthWidth, yMiddle - widthHeight, left + widthWidth,
                        yMiddle + widthHeight);
                mResizeDrawableWidth.draw(canvas);

                mResizeDrawableWidth.setBounds(right - widthWidth, yMiddle - widthHeight, right + widthWidth,
                        yMiddle + widthHeight);
                mResizeDrawableWidth.draw(canvas);

                mResizeDrawableHeight.setBounds(xMiddle - heightWidth, top - heightHeight,
                        xMiddle + heightWidth, top + heightHeight);
                mResizeDrawableHeight.draw(canvas);

                mResizeDrawableHeight.setBounds(xMiddle - heightWidth, bottom - heightHeight,
                        xMiddle + heightWidth, bottom + heightHeight);
                mResizeDrawableHeight.draw(canvas);
            }
        }
    }
}

From source file:com.ttxgps.zoom.GestureImageView.java

@Override
protected void onDraw(Canvas canvas) {
    if (layout) {
        if (drawable != null && !isRecycled()) {
            canvas.save();

            float adjustedScale = scale * scaleAdjust;

            canvas.translate(x, y);/*from   w  w  w  .jav  a2s  .  c  o m*/

            if (rotation != 0.0f) {
                canvas.rotate(rotation);
            }

            if (adjustedScale != 1.0f) {
                canvas.scale(adjustedScale, adjustedScale);
            }

            drawable.draw(canvas);

            canvas.restore();
        }

        if (drawLock.availablePermits() <= 0) {
            drawLock.release();
        }
    }
}

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

/**
 * Tab//w w  w  .j a v a  2 s . c  o m
 * 
 * @param canvas
 */
private void drawTabBG(Canvas canvas) {
    if (showTabColor) {
        mTextPaint.setColor(tabColorNormal);
        canvas.save();
        canvas.translate(getPaddingLeft(), 0);
        if (tabs.size() > 0)
            for (int i = 0; i < tabs.size(); i++) {
                if (i == nextPager) {
                    mTextPaint.setColor(getColor(tabColorNormal, tabColorSelected, textSizeOffset));
                } else if (i == currectPager) {
                    mTextPaint.setColor(getColor(tabColorNormal, tabColorSelected, 1 - textSizeOffset));
                } else {
                    mTextPaint.setColor(tabColorNormal);
                }
                canvas.drawRect(0, getPaddingTop(), itemWidth, getHeight() - getPaddingBottom(), mTextPaint);
                canvas.translate(itemWidth + intervalWidth, 0);
            }
        else {
            mTextPaint.setColor(tabColorSelected);
            canvas.drawRect(0, getPaddingTop(), itemWidth + intervalWidth + getPaddingLeft(),
                    getHeight() - getPaddingBottom(), mTextPaint);
        }
        canvas.restore();
    }
}

From source file:com.github.jokar.rxupload.widget.ProgressDownloadView.java

/**
 * MARK: Overrides//from  ww  w . j a  va  2s.  c om
 */

@Override
protected void onDraw(Canvas canvas) {
    if (mPathWhite != null && mPathBlack != null) {

        float textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 4.0f),
                mProgress * mWidth / 100 - (int) (mBubbleWidth / 4.0f));
        float textY = mHeight / 2 - mBubbleHeight / 2 + calculateDeltaY();

        switch (mState) {
        case STATE_WORKING:
            // Save and restore prevent the rest of the canvas to not be rotated
            canvas.save();
            float speed = (getProgress() - mTarget) / 20;
            mBubbleAngle += speed * 10;
            if (mBubbleAngle > 20) {
                mBubbleAngle = 20;
            }
            if (mBubbleAngle < -20) {
                mBubbleAngle = -20;
            }
            if (Math.abs(speed) < 1) {
                mSpeedAngle -= mBubbleAngle / 20;
                mSpeedAngle *= .9f;
            }
            mBubbleAngle += mSpeedAngle;

            canvas.rotate(mBubbleAngle, bubbleAnchorX, bubbleAnchorY);
            canvas.drawPath(mPathBubble, mPaintBubble);
            canvas.drawText(String.valueOf((int) mProgress) + " %", textX, textY, mPaintText);
            canvas.restore();
            break;
        case STATE_FAILED:
            canvas.save();
            canvas.rotate(mFailAngle, bubbleAnchorX, bubbleAnchorY);
            canvas.drawPath(mPathBubble, mPaintBubble);
            canvas.rotate(mFailAngle, bubbleAnchorX, textY - mBubbleHeight / 7);
            //                    mPaintText.setColor(getResources().getColor(R.color.red_wine));
            textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 3.2f),
                    mProgress * mWidth / 100 - (int) (mBubbleWidth / 3.2f));
            canvas.drawText(getResources().getString(R.string.failed), textX, textY, mPaintText);
            canvas.restore();
            break;
        case STATE_SUCCESS:
            canvas.save();
            //                    mPaintText.setColor(getResources().getColor(R.color.green_grass));
            textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 3.2f),
                    mProgress * mWidth / 100 - (int) (mBubbleWidth / 3.2f));
            Matrix flipMatrix = new Matrix();
            flipMatrix.setScale(mFlipFactor, 1, bubbleAnchorX, bubbleAnchorY);
            canvas.concat(flipMatrix);
            canvas.drawPath(mPathBubble, mPaintBubble);
            canvas.concat(flipMatrix);
            canvas.drawText(getResources().getString(R.string.done), textX, textY, mPaintText);
            canvas.restore();
            break;
        }

        canvas.drawPath(mPathBlack, mPaintBlack);
        canvas.drawPath(mPathWhite, mPaintWhite);
    }
}

From source file:com.evilduck.piano.views.instrument.PianoView.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    boolean needsInvalidate = false;

    final int overScrollMode = ViewCompat.getOverScrollMode(this);
    if (overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS
            || (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS)) {
        if (!leftEdgeEffect.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int width = getWidth();

            canvas.rotate(270);//from   ww w.  j  a  v  a  2 s  .c  o m
            canvas.translate(-height + getPaddingTop(), 0);
            leftEdgeEffect.setSize(height, width);
            needsInvalidate |= leftEdgeEffect.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!rightEdgeEffect.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();

            canvas.rotate(90);
            canvas.translate(-getPaddingTop(), -width);
            rightEdgeEffect.setSize(height, width);
            needsInvalidate |= rightEdgeEffect.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    } else {
        leftEdgeEffect.finish();
        rightEdgeEffect.finish();
    }

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

From source file:com.dat.complexrecyclerviewdemo.recyclerviewinsidecardviewdemo.StickyHeaderDecoration.java

/**
 * {@inheritDoc}/* w  w  w  .j  a  va 2s .  c o  m*/
 */
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    final int count = parent.getChildCount();
    long previousHeaderId = -1;

    for (int layoutPos = 0; layoutPos < count; layoutPos++) {
        final View child = parent.getChildAt(layoutPos);
        final int adapterPos = parent.getChildAdapterPosition(child);

        if (adapterPos != RecyclerView.NO_POSITION && hasHeader(adapterPos)) {
            long headerId = mAdapter.getHeaderId(adapterPos);

            if (headerId != previousHeaderId) {
                previousHeaderId = headerId;
                View header = getHeader(parent, adapterPos).itemView;
                canvas.save();

                final int left = child.getLeft();
                final int top = getHeaderTop(parent, child, header, adapterPos, layoutPos);
                canvas.translate(left, top - 4);
                header.setTranslationX(left);
                header.setTranslationY(top);
                header.draw(canvas);
                canvas.restore();
            }
        }
    }
}

From source file:ca.barrenechea.widget.recyclerview.decoration.StickyHeaderDecoration.java

/**
 * {@inheritDoc}//from   w  w  w .  ja va  2  s. co m
 */
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    final int count = parent.getChildCount();
    long previousHeaderId = -1;

    for (int layoutPos = 0; layoutPos < count; layoutPos++) {
        final View child = parent.getChildAt(layoutPos);
        final int adapterPos = parent.getChildAdapterPosition(child);

        if (adapterPos != RecyclerView.NO_POSITION && hasHeader(adapterPos)) {
            long headerId = mAdapter.getHeaderId(adapterPos);

            if (headerId != previousHeaderId) {
                previousHeaderId = headerId;
                View header = getHeader(parent, adapterPos).itemView;
                canvas.save();

                final int left = child.getLeft();
                final int top = getHeaderTop(parent, child, header, adapterPos, layoutPos);
                canvas.translate(left, top);

                header.setTranslationX(left);
                header.setTranslationY(top);
                header.draw(canvas);
                canvas.restore();
            }
        }
    }
}