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:de.telekom.pde.codelibrary.ui.layout.PDESwipeRefreshLayout.java

@Override
public void draw(@NonNull Canvas canvas) {
    super.draw(canvas);

    int width = mBounds.width();
    int height = mBounds.height();
    int cx = width / 2;
    int cy = height / 2;

    int restoreCount = canvas.save();
    canvas.clipRect(mBounds);// www  .j  ava 2  s . c  o m

    if (mRunning) {
        long now = AnimationUtils.currentAnimationTimeMillis();
        long elapsed = (now - mStartTime) % mAnimationDuration;
        float currentAngle = (elapsed / (mAnimationDuration / 100f)) * 3.6f;

        canvas.drawCircle(cx, cy, PDEBuildingUnits.BU(), mPaint);

        canvas.save();
        canvas.rotate(currentAngle, cx, cy);
        canvas.drawPath(mCirclePath, mWhitePaint);
        canvas.restore();

        ViewCompat.postInvalidateOnAnimation(this);
    } else {
        // Otherwise if we're in the middle of a trigger, draw that.
        if (mTriggerPercentage > 0 && mTriggerPercentage <= 1.0) {
            drawTrigger(canvas, cx, cy);
        }
    }
    canvas.restoreToCount(restoreCount);
}

From source file:com.acbelter.scheduleview.ScheduleView.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    changeClipRect(canvas);//from w ww  . j a v  a2  s.  co m

    canvas.save();
    canvas.translate(getPaddingLeft(), mListY + mInternalPaddingTop + getPaddingTop());
    ViewHolder holder = (ViewHolder) mTimeMark.getTag();
    for (int i = 0; i < mTimeMarks.size(); i++) {
        holder.time.setText(mTimeMarks.get(i));
        mTimeMark.layout(0, 0, mTimeMark.getMeasuredWidth(), mTimeMark.getMeasuredHeight());
        mTimeMark.draw(canvas);

        if (i != mTimeMarks.size() - 1) {
            canvas.translate(0, mTimeMarkHeight + mTimeMarksDistance);
        }
    }
    canvas.restore();

    drawEdgeEffects(canvas);
}

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   w  ww  .  j a  v a  2s.c om
            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:net.opacapp.multilinecollapsingtoolbar.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;
        // MODIFICATION: removed now unused "descent" variable declaration

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

        // BEGIN MODIFICATION: new drawing code
        if (drawTexture) {
            ascent = 0;//from  w w w .j a  v  a  2s  .c  om
        } else {
            ascent = mTextPaint.ascent() * mScale;
        }

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta rect in the text bounds
            canvas.drawRect(mCurrentBounds.left, y, mCurrentBounds.right, y + mTextLayout.getHeight() * mScale,
                    DEBUG_DRAW_PAINT);
        }
        if (mScale != 1f) {
            canvas.scale(mScale, mScale, x, y);
        }

        // Compute where to draw mTextLayout for this frame
        final float currentExpandedX = mCurrentDrawX + mTextLayout.getLineLeft(0) - mExpandedFirstLineDrawX * 2;
        if (drawTexture) {
            // If we should use a texture, draw it instead of text
            // Expanded text
            mTexturePaint.setAlpha((int) (mExpandedTextBlend * 255));
            canvas.drawBitmap(mExpandedTitleTexture, currentExpandedX, y, mTexturePaint);
            // Collapsed text
            mTexturePaint.setAlpha((int) (mCollapsedTextBlend * 255));
            canvas.drawBitmap(mCollapsedTitleTexture, x, y, mTexturePaint);
            // Cross-section between both texts (should stay at alpha = 255)
            mTexturePaint.setAlpha(255);
            canvas.drawBitmap(mCrossSectionTitleTexture, x, y, mTexturePaint);
        } else {
            // positon expanded text appropriately
            canvas.translate(currentExpandedX, y);
            // Expanded text
            mTextPaint.setAlpha((int) (mExpandedTextBlend * 255));
            mTextLayout.draw(canvas);

            // position the overlays
            canvas.translate(x - currentExpandedX, 0);

            // Collapsed text
            mTextPaint.setAlpha((int) (mCollapsedTextBlend * 255));
            canvas.drawText(mTextToDrawCollapsed, 0, mTextToDrawCollapsed.length(), 0, -ascent / mScale,
                    mTextPaint);
            // Cross-section between both texts (should stay at alpha = 255)
            mTextPaint.setAlpha(255);
            canvas.drawText(mTextToDraw, mTextLayout.getLineStart(0), mTextLayout.getLineEnd(0), 0,
                    -ascent / mScale, mTextPaint);
        }
        // END MODIFICATION
    }
    canvas.restoreToCount(saveCount);
}

From source file:com.mikelau.croperino.HighlightView.java

protected void draw(Canvas canvas) {

    if (mHidden) {
        return;//w w w.j  ava  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 if (mOvalo) {
            canvas.save();

            RectF r = new RectF(mDrawRect.left, mDrawRect.top, mDrawRect.right, mDrawRect.bottom);

            path.addOval(r, 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.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 {/*w  w  w  . j  a va2  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;
        }

        // 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.shizhefei.view.coolrefreshview.header.JellyHeader.java

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

    int totalDistance = totalDistance();
    int headerHeight;
    int pullHeight;
    int offsetToKeepHeaderWhileLoading = offsetToKeepHeaderWhileLoading();
    if (status == CoolRefreshView.PULL_STATUS_REFRESHING) {
        headerHeight = offsetToKeepHeaderWhileLoading;
        pullHeight = currentDistance;/*w ww.  java  2s  . co m*/
    } else {
        headerHeight = Math.min(currentDistance / 2, offsetToKeepHeaderWhileLoading);
        pullHeight = currentDistance;
    }
    if (headerHeight == 0) {
        return;
    }

    int saveCount = canvas.save();
    canvas.translate(0, totalDistance - currentDistance);

    final int width = canvas.getWidth();
    //??touchx ????touchx??
    mPointX = width / 2;

    final float mDisplayX = (mPointX - width / 2f) * 0.5f + width / 2f;
    mPaint.setColor(mColor);

    mPath.rewind();
    // ?
    mPath.moveTo(0, 0);
    mPath.lineTo(0, headerHeight);
    // ???
    mPath.quadTo(mDisplayX, pullHeight, width, headerHeight);
    mPath.lineTo(width, 0);
    mPath.close();
    canvas.drawPath(mPath, mPaint);
    canvas.restoreToCount(saveCount);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setOutlineProvider(mViewOutlineProvider);
    }
}

From source file:com.example.view.wheel.WheelView.java

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

    int top = itemsLayout.getLineTop(1);
    canvas.translate(0, -top + scrollingOffset);

    itemsPaint.setColor(ITEMS_TEXT_COLOR);
    itemsPaint.drawableState = getDrawableState();
    itemsLayout.draw(canvas);

    canvas.restore();
}

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

/**
 * //from   ww  w .java  2  s .  c  om
 * 
 * @param canvas
 */
private void drawTag(Canvas canvas) {
    // TODO
    if (mTagAdapter != null) {
        canvas.save();
        float canvasOffset = 0;
        int x = getPaddingLeft();
        int y = getPaddingTop();
        float textTop = 0;
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTextAlign(Align.LEFT);
        for (int position = 0; position < tabs.size(); position++) {
            canvas.translate(canvasOffset, 0);
            canvasOffset = itemWidth + intervalWidth;
            canvas.save();
            if (mTagAdapter.isEnable(position)) {
                int textWidth;
                int textHeight;
                mTextPaint.setTextSize(mTagAdapter.getTextSize(position));
                String tag = mTagAdapter.getTag(position) == null ? "" : mTagAdapter.getTag(position);
                textWidth = (int) Math.ceil(mTextPaint.measureText(tag));
                FontMetrics fontMetrics = mTextPaint.getFontMetrics();
                textHeight = (int) Math.ceil(fontMetrics.descent - fontMetrics.ascent);
                textTop = textHeight - (-fontMetrics.ascent - fontMetrics.descent
                        + (fontMetrics.bottom - fontMetrics.descent) * density);
                textHeight += textTop;
                if ("".equals(tag)) {
                    textHeight = 0;
                }
                final Drawable drawable = mTagAdapter.getBackground(position);
                int drawableWidth = drawable == null ? 0 : drawable.getMinimumWidth();
                int drawableHeight = drawable == null ? 0 : drawable.getMinimumHeight();
                float offsetX = Math.max(textWidth + mTagAdapter.getPaddingLeft(position)
                        + mTagAdapter.getPaddingRight(position), drawableWidth);
                float offsetTextX = (offsetX - (textWidth + mTagAdapter.getPaddingLeft(position)
                        + mTagAdapter.getPaddingRight(position))) * 0.5f;
                float offsetY = Math.max(textHeight + mTagAdapter.getPaddingBottom(position)
                        + mTagAdapter.getPaddingTop(position), drawableHeight);
                float offsetTextY = (offsetY - (textHeight + mTagAdapter.getPaddingBottom(position)
                        + mTagAdapter.getPaddingTop(position))) * 0.5f;

                mTextPaint.setTextSize(showTextScale ? textSize * (1 + magnification) : textSize);
                float myTextWidth = mTextPaint.measureText(tabs.get(position));
                TagAlign ta = mTagAdapter.getTagAlign(position);
                switch (ta) {
                default:
                case LEFTTOP:
                    canvas.translate(mTagAdapter.getMarginLeft(position), mTagAdapter.getMarginTop(position));
                    break;
                case LEFTCENTER:
                    canvas.translate(mTagAdapter.getMarginLeft(position),
                            (getHeight() - underLineHeight - offsetY) / 2);
                    break;
                case LEFTBOTTOM:
                    canvas.translate(mTagAdapter.getMarginLeft(position),
                            getHeight() - offsetY - underLineHeight - mTagAdapter.getMarginBottom(position));
                    break;
                case RIGHTTOP:
                    canvas.translate(itemWidth - offsetX - mTagAdapter.getMarginRight(position),
                            mTagAdapter.getMarginTop(position));
                    break;
                case RIGHTCENTER:
                    canvas.translate(itemWidth - offsetX - mTagAdapter.getMarginRight(position),
                            (getHeight() - underLineHeight - offsetY) / 2);
                    break;
                case RIGHTBOTTOM:
                    canvas.translate(itemWidth - offsetX - mTagAdapter.getMarginRight(position),
                            getHeight() - offsetY - underLineHeight - mTagAdapter.getMarginBottom(position));
                    break;
                case LEFTTOPTEXT:
                    canvas.translate(
                            (itemWidth - myTextWidth) / 2 - offsetX - mTagAdapter.getMarginRight(position),
                            mTagAdapter.getMarginTop(position));
                    break;
                case LEFTCENTERTEXT:
                    canvas.translate(
                            (itemWidth - myTextWidth) / 2 - offsetX - mTagAdapter.getMarginRight(position),
                            (getHeight() - underLineHeight - offsetY) / 2);
                    break;
                case LEFTBOTTOMTEXT:
                    canvas.translate(
                            (itemWidth - myTextWidth) / 2 - offsetX - mTagAdapter.getMarginRight(position),
                            getHeight() - offsetY - underLineHeight - mTagAdapter.getMarginBottom(position));
                    break;
                case RIGHTTOPTEXT:
                    canvas.translate((itemWidth + myTextWidth) / 2 + mTagAdapter.getMarginLeft(position),
                            mTagAdapter.getMarginTop(position));
                    break;
                case RIGHTCENTERTEXT:
                    canvas.translate((itemWidth + myTextWidth) / 2 + mTagAdapter.getMarginLeft(position),
                            (getHeight() - underLineHeight - offsetY) / 2);
                    break;

                case RIGHTBOTTOMTEXT:
                    canvas.translate((itemWidth + myTextWidth) / 2 + mTagAdapter.getMarginLeft(position),
                            getHeight() - offsetY - underLineHeight - mTagAdapter.getMarginBottom(position));
                    break;

                }
                mTextPaint.setTextSize(mTagAdapter.getTextSize(position));
                if (drawable != null) {
                    drawable.setBounds(x, y, (int) (x + offsetX), (int) (y + offsetY));
                    drawable.draw(canvas);
                }
                canvas.drawText(tag, x + offsetTextX + mTagAdapter.getPaddingLeft(position),
                        y + offsetY - textTop - offsetTextY - mTagAdapter.getPaddingTop(position), mTextPaint);
            }
            canvas.restore();
        }
        canvas.restore();
    }
}

From source file:android.wuliqing.com.mylibrary.header.MaterialProgressDrawable.java

@Override
public void draw(Canvas c) {
    if (mShadow != null) {
        mShadow.getPaint().setColor(mBackgroundColor);
        mShadow.draw(c);/*from w  ww.  j  av a  2 s .  co m*/
    }

    final Rect bounds = getBounds();
    final int saveCount = c.save();
    c.rotate(mRotation, bounds.exactCenterX(), bounds.exactCenterY());
    mRing.draw(c, bounds);
    c.restoreToCount(saveCount);
}