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:com.huewu.pla.lib.internal.PLAListView.java

void drawOverscrollHeader(final Canvas canvas, final Drawable drawable, final Rect bounds) {
    final int height = drawable.getMinimumHeight();

    canvas.save();
    canvas.clipRect(bounds);/*from   www  .ja  v a  2 s . c  om*/

    final int span = bounds.bottom - bounds.top;
    if (span < height) {
        bounds.top = bounds.bottom - height;
    }

    drawable.setBounds(bounds);
    drawable.draw(canvas);

    canvas.restore();
}

From source file:android.support.v7.graphics.drawable.DrawerArrowDrawable.java

@Override
public void draw(Canvas canvas) {
    Rect bounds = getBounds();/*from   w  w w .j  a  va  2  s .  c  o  m*/

    final boolean flipToPointRight;
    switch (mDirection) {
    case ARROW_DIRECTION_LEFT:
        flipToPointRight = false;
        break;
    case ARROW_DIRECTION_RIGHT:
        flipToPointRight = true;
        break;
    case ARROW_DIRECTION_END:
        flipToPointRight = DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR;
        break;
    case ARROW_DIRECTION_START:
    default:
        flipToPointRight = DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
        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:com.commonsware.cwac.crossport.v7.graphics.drawable.DrawerArrowDrawable.java

@Override
public void draw(Canvas canvas) {
    Rect bounds = getBounds();//ww  w.j a v a 2 s .  co  m

    final boolean flipToPointRight;
    switch (mDirection) {
    case ARROW_DIRECTION_LEFT:
        flipToPointRight = false;
        break;
    case ARROW_DIRECTION_RIGHT:
        flipToPointRight = true;
        break;
    case ARROW_DIRECTION_END:
        flipToPointRight = DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR;
        break;
    case ARROW_DIRECTION_START:
    default:
        flipToPointRight = DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
        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.5f + 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:org.chromium.chrome.browser.toolbar.ToolbarPhone.java

@Override
public void draw(Canvas canvas) {
    // If capturing a texture of the toolbar, ensure the alpha is set prior to draw(...) being
    // called.  The alpha is being used prior to getting to draw(...), so updating the value
    // after this point was having no affect.
    if (mTextureCaptureMode)
        assert getAlpha() == 1f;

    // mClipRect can change in the draw call, so cache this value to ensure the canvas is
    // restored correctly.
    boolean shouldClip = !mTextureCaptureMode && mClipRect != null;
    if (shouldClip) {
        canvas.save();
        canvas.clipRect(mClipRect);//from   w w  w  .jav a  2s  . c  o m
    }
    super.draw(canvas);
    if (shouldClip) {
        canvas.restore();

        // Post an invalidate when the clip rect becomes null to ensure another draw pass occurs
        // and the full toolbar is drawn again.
        if (mClipRect == null)
            postInvalidate();
    }
}

From source file:com.jecelyin.editor.v2.widget.AnyDrawerLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final int height = getHeight();
    final boolean drawingContent = isContentView(child);
    int clipLeft = 0, clipRight = getWidth();
    int clipTop = 0;

    final int restoreCount = canvas.save();
    if (drawingContent) {
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View v = getChildAt(i);
            if (v == child || v.getVisibility() != VISIBLE || !hasOpaqueBackground(v) || !isDrawerView(v)
                    || v.getHeight() < height) {
                continue;
            }/*from www .  j a  va 2 s .  c o m*/

            if (checkDrawerViewAbsoluteGravity(v, Gravity.TOP)) {
                final int vbottom = v.getBottom();
                if (vbottom > clipTop)
                    clipTop = vbottom;
            } else if (checkDrawerViewAbsoluteGravity(v, Gravity.LEFT)) {
                final int vright = v.getRight();
                if (vright > clipLeft)
                    clipLeft = vright;
            } else {
                final int vleft = v.getLeft();
                if (vleft < clipRight)
                    clipRight = vleft;
            }
        }
        canvas.clipRect(clipLeft, clipTop, clipRight, getHeight());
    }
    final boolean result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(restoreCount);

    if (mScrimOpacity > 0 && drawingContent) {
        final int baseAlpha = (mScrimColor & 0xff000000) >>> 24;
        final int imag = (int) (baseAlpha * mScrimOpacity);
        final int color = imag << 24 | (mScrimColor & 0xffffff);
        mScrimPaint.setColor(color);

        canvas.drawRect(clipLeft, 0, clipRight, getHeight(), mScrimPaint);
    } else if (mShadowLeftResolved != null && checkDrawerViewAbsoluteGravity(child, Gravity.LEFT)) {
        final int shadowWidth = mShadowLeftResolved.getIntrinsicWidth();
        final int childRight = child.getRight();
        final int drawerPeekDistance = mLeftDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) childRight / drawerPeekDistance, 1.f));
        mShadowLeftResolved.setBounds(childRight, child.getTop(), childRight + shadowWidth, child.getBottom());
        mShadowLeftResolved.setAlpha((int) (0xff * alpha));
        mShadowLeftResolved.draw(canvas);
    } else if (mShadowRightResolved != null && checkDrawerViewAbsoluteGravity(child, Gravity.RIGHT)) {
        final int shadowWidth = mShadowRightResolved.getIntrinsicWidth();
        final int childLeft = child.getLeft();
        final int showing = getWidth() - childLeft;
        final int drawerPeekDistance = mRightDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) showing / drawerPeekDistance, 1.f));
        mShadowRightResolved.setBounds(childLeft - shadowWidth, child.getTop(), childLeft, child.getBottom());
        mShadowRightResolved.setAlpha((int) (0xff * alpha));
        mShadowRightResolved.draw(canvas);
    } else if (mShadowBottomResolved != null && checkDrawerViewAbsoluteGravity(child, Gravity.BOTTOM)) {
        final int shadowHeight = mShadowBottomResolved.getIntrinsicHeight();
        final int showing = getHeight() - child.getTop();
        final int drawerPeekDistance = mRightDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) showing / drawerPeekDistance, 1.f));
        mShadowBottomResolved.setBounds(child.getLeft(), child.getTop() - shadowHeight, child.getRight(),
                child.getBottom());
        mShadowBottomResolved.setAlpha((int) (0xff * alpha));
        mShadowBottomResolved.draw(canvas);
    }
    return result;
}

From source file:com.ftinc.kit.attributr.ui.widget.StickyRecyclerHeadersElevationDecoration.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override/*from w w  w .j av a2 s.  c om*/
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(canvas, parent, state);
    int orientation = getOrientation(parent);
    mHeaderRects.clear();

    if (parent.getChildCount() > 0 && mAdapter.getItemCount() > 0) {

        // draw the first visible child's header at the top of the view
        View firstView = parent.getChildAt(0);
        int firstPosition = parent.getChildPosition(firstView);

        if (mAdapter.getHeaderId(firstPosition) > -1) {

            View firstHeader = getHeaderView(parent, firstPosition);
            View nextView = getNextView(parent);

            int translationX = Math.max(parent.getChildAt(0).getLeft() - firstHeader.getWidth(), 0);
            int translationY = Math.max(parent.getChildAt(0).getTop() - firstHeader.getHeight(), 0);
            int nextPosition = parent.getChildPosition(nextView);

            if (nextPosition > 0 && hasNewHeader(nextPosition)) {

                View secondHeader = getHeaderView(parent, nextPosition);

                //Translate the topmost header so the next header takes its place, if applicable
                if (orientation == LinearLayoutManager.VERTICAL
                        && nextView.getTop() - secondHeader.getHeight() - firstHeader.getHeight() < 0) {

                    translationY += nextView.getTop() - secondHeader.getHeight() - firstHeader.getHeight();

                } else if (orientation == LinearLayoutManager.HORIZONTAL
                        && nextView.getLeft() - secondHeader.getWidth() - firstHeader.getWidth() < 0) {

                    translationX += nextView.getLeft() - secondHeader.getWidth() - firstHeader.getWidth();

                }
            }

            boolean shouldDrawShadow = true;
            if (firstPosition == 0 && (firstView.getTop() == firstHeader.getHeight())) {
                shouldDrawShadow = false;
            }

            if (translationY == 0 && shouldDrawShadow) {

                ImageView shadow = ButterKnife.findById(firstHeader, R.id.shadow);
                shadow.setVisibility(View.VISIBLE);

            } else {

                ImageView shadow = ButterKnife.findById(firstHeader, R.id.shadow);
                shadow.setVisibility(View.GONE);

            }

            canvas.save();
            canvas.translate(translationX, translationY);
            firstHeader.draw(canvas);
            canvas.restore();
            mHeaderRects.put(firstPosition, new Rect(translationX, translationY,
                    translationX + firstHeader.getWidth(), translationY + firstHeader.getHeight()));

        }

        if (parent.getChildCount() > 1 && mAdapter.getItemCount() > 1) {
            for (int i = 1; i < parent.getChildCount(); i++) {

                int position = parent.getChildPosition(parent.getChildAt(i));
                if (hasNewHeader(position)) {

                    // this header is different than the previous, it must be drawn in the correct place
                    int translationX = 0;
                    int translationY = 0;
                    View header = getHeaderView(parent, position);

                    if (orientation == LinearLayoutManager.VERTICAL) {

                        translationY = parent.getChildAt(i).getTop() - header.getHeight();

                    } else {

                        translationX = parent.getChildAt(i).getLeft() - header.getWidth();

                    }

                    // don't render shadow
                    ImageView shadow = ButterKnife.findById(header, R.id.shadow);
                    shadow.setVisibility(View.GONE);

                    canvas.save();
                    canvas.translate(translationX, translationY);
                    header.draw(canvas);
                    canvas.restore();
                    mHeaderRects.put(position, new Rect(translationX, translationY,
                            translationX + header.getWidth(), translationY + header.getHeight()));
                }
            }
        }
    }
}

From source file:administrator.example.com.myscrollview.VerticalViewPager.java

@Override
public void draw(Canvas canvas) {
    // XXX ?//from  www  .java 2s .c  om
    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 && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mTopEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();

            canvas.rotate(270);
            canvas.translate(-width + getPaddingLeft(), 0);
            mTopEdge.setSize(width, getHeight());
            needsInvalidate |= mTopEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        } /* end of if */
        if (!mBottomEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();
            final int height = getHeight();
            final int itemCount = mAdapter != null ? mAdapter.getCount() : 1;

            canvas.rotate(180);
            canvas.translate(-width + getPaddingLeft(), -itemCount * (height + mPageMargin) + mPageMargin);
            mBottomEdge.setSize(width, height);
            needsInvalidate |= mBottomEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        } /* end of if */
    } else {
        mTopEdge.finish();
        mBottomEdge.finish();
    } /* end of if */

    if (needsInvalidate) {
        // Keep animating
        invalidate();
    } /* end of if */
}

From source file:com.lanou.mirror.tool.VerticalViewPager.java

@Override
public void draw(Canvas canvas) {
    // XXX /*  w  w  w  .j  a va  2 s. com*/
    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 && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mTopEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();

            canvas.rotate(270);
            canvas.translate(-width + getPaddingLeft(), 0);
            mTopEdge.setSize(width, getHeight());
            needsInvalidate |= mTopEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        } /* end of if */
        if (!mBottomEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();
            final int height = getHeight();
            final int itemCount = mAdapter != null ? mAdapter.getCount() : 1;

            canvas.rotate(180);
            canvas.translate(-width + getPaddingLeft(), -itemCount * (height + mPageMargin) + mPageMargin);
            mBottomEdge.setSize(width, height);
            needsInvalidate |= mBottomEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        } /* end of if */
    } else {
        mTopEdge.finish();
        mBottomEdge.finish();
    } /* end of if */

    if (needsInvalidate) {
        // Keep animating
        invalidate();
    } /* end of if */
}

From source file:com.bluetoothlamp.tiny.view.verticalview.VerticalViewPager.java

@Override
public void draw(Canvas canvas) {
    // XXX ??/*  ww  w.j a  v  a  2 s  .  co m*/
    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 && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mTopEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();

            canvas.rotate(270);
            canvas.translate(-width + getPaddingLeft(), 0);
            mTopEdge.setSize(width, getHeight());
            needsInvalidate |= mTopEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        } /* end of if */
        if (!mBottomEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();
            final int height = getHeight();
            final int itemCount = mAdapter != null ? mAdapter.getCount() : 1;

            canvas.rotate(180);
            canvas.translate(-width + getPaddingLeft(), -itemCount * (height + mPageMargin) + mPageMargin);
            mBottomEdge.setSize(width, height);
            needsInvalidate |= mBottomEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        } /* end of if */
    } else {
        mTopEdge.finish();
        mBottomEdge.finish();
    } /* end of if */

    if (needsInvalidate) {
        // Keep animating
        invalidate();
    } /* end of if */
}

From source file:com.huewu.pla.lib.internal.PLAListView.java

/**
 * Draws a divider for the given child in the given bounds.
 * //w  w w.j a v  a 2  s  . c  o  m
 * @param canvas The canvas to draw to.
 * @param bounds The bounds of the divider.
 * @param childIndex The index of child (of the View) above the divider.
 *            This will be -1 if there is no child above the divider to be
 *            drawn.
 */
void drawDivider(final Canvas canvas, final Rect bounds, final int childIndex) {
    // This widget draws the same divider for all children
    final Drawable divider = mDivider;
    final boolean clipDivider = mClipDivider;

    if (!clipDivider) {
        divider.setBounds(bounds);
    } else {
        canvas.save();
        canvas.clipRect(bounds);
    }

    divider.draw(canvas);

    if (clipDivider) {
        canvas.restore();
    }
}