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.jzh.stuapp.view.MyViewPager.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 && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mLeftEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();

            canvas.rotate(270);//from  w  w w.  j  av a 2  s.  com
            canvas.translate(-height + getPaddingTop(), 0);
            mLeftEdge.setSize(height, getWidth());
            needsInvalidate |= mLeftEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!mRightEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int itemCount = mAdapter != null ? mAdapter.getCount() : 1;

            canvas.rotate(90);
            canvas.translate(-getPaddingTop(), -itemCount * (width + mPageMargin) + mPageMargin);
            mRightEdge.setSize(height, width);
            needsInvalidate |= mRightEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    } else {
        mLeftEdge.finish();
        mRightEdge.finish();
    }

    if (needsInvalidate) {
        invalidate();
    }
}

From source file:am.widget.indicatortabstrip.IndicatorTabStrip.java

/**
 * /*from   w  w  w.  ja  v a 2 s . co  m*/
 *
 * @param canvas 
 */
protected void drawIndicator(Canvas canvas) {
    if (mIndicator == null)
        return;
    final int indicatorWidth = getIndicatorWidth();
    final int indicatorHeight = getIndicatorHeight();
    if (indicatorWidth <= 0 || indicatorHeight <= 0)
        return;
    mIndicator.setBounds(0, 0, indicatorWidth, indicatorHeight);
    final float widthWithInterval = getItemWidth() + getIntervalWidth();
    final float currentCenter = ViewCompat.getPaddingStart(this) + mCurrentPager * widthWithInterval
            + widthWithInterval * 0.5f;
    final float nextCenter = ViewCompat.getPaddingStart(this) + mNextPager * widthWithInterval
            + widthWithInterval * 0.5f;
    final float moveCenter = currentCenter + (nextCenter - currentCenter) * mOffset;
    final float moveX = moveCenter - indicatorWidth * 0.5f;
    final float moveY = getHeight() - getPaddingBottom() - getDividerHeight() - indicatorHeight;
    canvas.save();
    canvas.translate(moveX, moveY);
    mIndicator.draw(canvas);
    canvas.restore();
}

From source file:com.shizhefei.view.largeimage.UpdateImageView.java

@SuppressLint("DrawAllocation")
@Override//from  w  w w. ja  v  a 2 s  .  co  m
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (getMeasuredWidth() == 0) {
        return;
    }
    if (mDrawable != null) {
        mDrawable.setBounds((int) mOffsetX, (int) mOffsetY, (int) (getMeasuredWidth() * mScale),
                (int) (getMeasuredHeight() * mScale));
        mDrawable.draw(canvas);
    } else if (mFactory != null) {
        Rect visibilityRect = tempRect;
        getVisibilityRect(visibilityRect);
        float width = mScale * getWidth();
        int imgWidth = imageBlockLoader.getWidth();

        float imageScale = imgWidth / width;

        // ?
        Rect imageRect = new Rect();
        imageRect.left = (int) Math.ceil((visibilityRect.left - mOffsetX) * imageScale);
        imageRect.top = (int) Math.ceil((visibilityRect.top - mOffsetY) * imageScale);
        imageRect.right = (int) Math.ceil((visibilityRect.right - mOffsetX) * imageScale);
        imageRect.bottom = (int) Math.ceil((visibilityRect.bottom - mOffsetY) * imageScale);

        List<DrawData> drawData = imageBlockLoader.getDrawData(imageScale, imageRect);

        if (drawData.isEmpty()) {
            if (defaultDrawable != null) {
                int height = (int) (1.0f * defaultDrawable.getIntrinsicHeight()
                        / defaultDrawable.getIntrinsicWidth() * getMeasuredWidth());
                int offset = (getMeasuredHeight() - height) / 2;
                defaultDrawable.setBounds((int) mOffsetX, (int) mOffsetY + offset,
                        (int) (getMeasuredWidth() * mScale), (int) (height * mScale));
                defaultDrawable.draw(canvas);
            }
        } else {
            int saveCount = canvas.save();
            for (DrawData data : drawData) {
                Rect drawRect = data.imageRect;
                drawRect.left = (int) (Math.ceil(drawRect.left / imageScale) + mOffsetX);
                drawRect.top = (int) (Math.ceil(drawRect.top / imageScale) + mOffsetY);
                drawRect.right = (int) (Math.ceil(drawRect.right / imageScale) + mOffsetX);
                drawRect.bottom = (int) (Math.ceil(drawRect.bottom / imageScale) + mOffsetY);
                canvas.drawBitmap(data.bitmap, data.srcRect, drawRect, null);
            }
            canvas.restoreToCount(saveCount);
        }
    }
}

From source file:com.marshalchen.common.uimodule.freeflow.core.FreeFlowContainer.java

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

    boolean needsInvalidate = false;

    final int height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
    final int width = getMeasuredWidth();

    if (!mLeftEdge.isFinished()) {
        final int restoreCount = canvas.save();

        canvas.rotate(270);//w  w  w.j av a  2s  .  co m
        canvas.translate(-height + getPaddingTop(), 0);// width);
        mLeftEdge.setSize(height, width);

        needsInvalidate = mLeftEdge.draw(canvas);
        canvas.restoreToCount(restoreCount);
    }

    if (!mTopEdge.isFinished()) {
        final int restoreCount = canvas.save();

        mTopEdge.setSize(width, height);

        needsInvalidate = mTopEdge.draw(canvas);
        canvas.restoreToCount(restoreCount);
    }

    if (!mRightEdge.isFinished()) {
        final int restoreCount = canvas.save();

        canvas.rotate(90);
        canvas.translate(0, -width);// width);
        mRightEdge.setSize(height, width);

        needsInvalidate = mRightEdge.draw(canvas);
        canvas.restoreToCount(restoreCount);
    }

    if (!mBottomEdge.isFinished()) {
        final int restoreCount = canvas.save();

        canvas.rotate(180);
        canvas.translate(-width + getPaddingTop(), -height);

        mBottomEdge.setSize(width, height);

        needsInvalidate = mBottomEdge.draw(canvas);
        canvas.restoreToCount(restoreCount);
    }

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

From source file:com.yimeng.babymom.view.LazyViewPager.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 && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mLeftEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();

            canvas.rotate(270);/*from   www .  j av a 2  s .c  om*/
            canvas.translate(-height + getPaddingTop(), 0);
            mLeftEdge.setSize(getWidth(), height);
            needsInvalidate = mLeftEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!mRightEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int itemCount = mAdapter != null ? mAdapter.getCount() : 1;

            canvas.rotate(90);
            canvas.translate(-getPaddingTop(), -itemCount * (width + mPageMargin) + mPageMargin);
            mRightEdge.setSize(width, height);
            needsInvalidate |= mRightEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    } else {
        mLeftEdge.finish();
        mRightEdge.finish();
    }

    if (needsInvalidate) {
        // Keep animating
        invalidate();
    }
}

From source file:org.akop.crosswords.view.CrosswordView.java

private void renderSelection(Canvas canvas, boolean clearSelection) {
    if (mSelectedWord == null) {
        return;/*from  ww  w  .j a v  a2  s  .  c  o  m*/
    }

    int startRow = mSelectedWord.getStartRow();
    int endRow = startRow;
    int startColumn = mSelectedWord.getStartColumn();
    int endColumn = startColumn;
    RectF cellRect = new RectF();

    canvas.save();
    canvas.scale(mRenderScale, mRenderScale);

    if (mSelectedWord.getDirection() == Crossword.Word.DIR_ACROSS) {
        endColumn += mSelectedWord.getLength() - 1;
    } else {
        endRow += mSelectedWord.getLength() - 1;
    }

    float top = mSelectedWord.getStartRow() * mCellSize;
    for (int row = startRow, index = 0; row <= endRow; row++, top += mCellSize) {
        float left = mSelectedWord.getStartColumn() * mCellSize;
        for (int column = startColumn; column <= endColumn; column++, left += mCellSize) {
            Cell cell = mPuzzleCells[row][column];
            if (cell != null) {
                // Draw the unselected cell
                Paint paint;
                if (clearSelection) {
                    paint = mCellFillPaint;
                } else {
                    if (index == mSelectedCell) {
                        paint = mSelectedCellFillPaint;
                    } else {
                        paint = mSelectedWordFillPaint;
                    }
                }

                cellRect.set(left, top, left + mCellSize, top + mCellSize);
                renderCell(canvas, cell, cellRect, paint);
            }

            index++;
        }
    }

    canvas.restore();
}

From source file:lagerimage.nicksong.view.largeimage.UpdateImageView.java

@SuppressLint("DrawAllocation")
@Override/*from   ww w  .j  a v  a  2 s . c o  m*/
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (getMeasuredWidth() == 0) {
        return;
    }
    if (mDrawable != null) {
        mDrawable.setBounds((int) mOffsetX, (int) mOffsetY, (int) (getMeasuredWidth() * mScale),
                (int) (getMeasuredHeight() * mScale));
        mDrawable.draw(canvas);
    } else if (mFactory != null) {
        Rect visibilityRect = tempRect;
        getVisibilityRect(visibilityRect);
        float width = mScale * getWidth();
        int imgWidth = imageBlockLoader.getWidth();

        float imageScale = imgWidth / width;

        // ?
        Rect imageRect = new Rect();
        imageRect.left = (int) Math.ceil((visibilityRect.left - mOffsetX) * imageScale);
        imageRect.top = (int) Math.ceil((visibilityRect.top - mOffsetY) * imageScale);
        imageRect.right = (int) Math.ceil((visibilityRect.right - mOffsetX) * imageScale);
        imageRect.bottom = (int) Math.ceil((visibilityRect.bottom - mOffsetY) * imageScale);

        List<BlockImageLoader.DrawData> drawData = imageBlockLoader.getDrawData(imageScale, imageRect);

        if (drawData.isEmpty()) {
            if (defaultDrawable != null) {
                int height = (int) (1.0f * defaultDrawable.getIntrinsicHeight()
                        / defaultDrawable.getIntrinsicWidth() * getMeasuredWidth());
                int offset = (getMeasuredHeight() - height) / 2;
                defaultDrawable.setBounds((int) mOffsetX, (int) mOffsetY + offset,
                        (int) (getMeasuredWidth() * mScale), (int) (height * mScale));
                defaultDrawable.draw(canvas);
            }
        } else {
            int saveCount = canvas.save();
            for (BlockImageLoader.DrawData data : drawData) {
                Rect drawRect = data.imageRect;
                drawRect.left = (int) (Math.ceil(drawRect.left / imageScale) + mOffsetX);
                drawRect.top = (int) (Math.ceil(drawRect.top / imageScale) + mOffsetY);
                drawRect.right = (int) (Math.ceil(drawRect.right / imageScale) + mOffsetX);
                drawRect.bottom = (int) (Math.ceil(drawRect.bottom / imageScale) + mOffsetY);
                canvas.drawBitmap(data.bitmap, data.srcRect, drawRect, null);
            }
            canvas.restoreToCount(saveCount);
        }
    }
}

From source file:com.cnpeng.cnpeng_mydemosfrom2016_12.a_12_GetLocalFiles_VP_FM.CustomNoPreLoadViewPager.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 && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mLeftEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();

            canvas.rotate(270);/* w w  w  . ja v a2s  .  c  o  m*/
            canvas.translate(-height + getPaddingTop(), 0);
            mLeftEdge.setSize(height, getWidth());
            needsInvalidate |= mLeftEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!mRightEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int itemCount = mAdapter != null ? mAdapter.getCount() : 1;

            canvas.rotate(90);
            canvas.translate(-getPaddingTop(), -itemCount * (width + mPageMargin) + mPageMargin);
            mRightEdge.setSize(height, width);
            needsInvalidate |= mRightEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    } else {
        mLeftEdge.finish();
        mRightEdge.finish();
    }

    if (needsInvalidate) {
        // Keep animating  
        invalidate();
    }
}

From source file:rikka.akashitoolkit.ui.widget.IconSwitchCompat.java

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

    final Rect padding = mTempRect;
    final Drawable trackDrawable = mTrackDrawable;
    if (trackDrawable != null) {
        trackDrawable.getPadding(padding);
    } else {/*ww w . j  a v  a 2s  .c o m*/
        padding.setEmpty();
    }

    final int switchTop = mSwitchTop;
    final int switchBottom = mSwitchBottom;
    final int switchInnerTop = switchTop + padding.top;
    final int switchInnerBottom = switchBottom - padding.bottom;

    final Drawable thumbDrawable = mThumbDrawable;
    if (trackDrawable != null) {
        if (mSplitTrack && thumbDrawable != null) {
            final Rect insets = DrawableUtils.getOpticalBounds(thumbDrawable);
            thumbDrawable.copyBounds(padding);
            padding.left += insets.left;
            padding.right -= insets.right;

            final int saveCount = canvas.save();
            canvas.clipRect(padding, Region.Op.DIFFERENCE);
            trackDrawable.draw(canvas);
            canvas.restoreToCount(saveCount);
        } else {
            trackDrawable.draw(canvas);
        }
    }

    final int saveCount = canvas.save();

    if (thumbDrawable != null) {
        thumbDrawable.draw(canvas);
    }

    final Drawable iconDrawable = mIconDrawable;
    if (iconDrawable != null) {
        if (mSplitTrack) {
            final Rect insets = DrawableUtils.getOpticalBounds(iconDrawable);
            iconDrawable.copyBounds(padding);
            padding.left += insets.left;
            padding.right -= insets.right;

            //final int saveCount = canvas.save();
            canvas.clipRect(padding, Region.Op.DIFFERENCE);
            iconDrawable.draw(canvas);
            canvas.restoreToCount(saveCount);
        } else {
            iconDrawable.draw(canvas);
        }
    }

    final Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
    if (switchText != null) {
        final int drawableState[] = getDrawableState();
        if (mTextColors != null) {
            mTextPaint.setColor(mTextColors.getColorForState(drawableState, 0));
        }
        mTextPaint.drawableState = drawableState;

        final int cX;
        if (thumbDrawable != null) {
            final Rect bounds = thumbDrawable.getBounds();
            cX = bounds.left + bounds.right;
        } else {
            cX = getWidth();
        }

        final int left = cX / 2 - switchText.getWidth() / 2;
        final int top = (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2;
        canvas.translate(left, top);
        switchText.draw(canvas);
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.yanzhenjie.durban.view.OverlayView.java

/**
 * This method draws crop bounds (empty rectangle)
 * and crop guidelines (vertical and horizontal lines inside the crop bounds) if needed.
 *
 * @param canvas - valid canvas object/*from ww w. j  ava 2 s  .co m*/
 */
protected void drawCropGrid(@NonNull Canvas canvas) {
    if (mShowCropGrid) {
        if (mGridPoints == null && !mCropViewRect.isEmpty()) {

            mGridPoints = new float[(mCropGridRowCount) * 4 + (mCropGridColumnCount) * 4];

            int index = 0;
            for (int i = 0; i < mCropGridRowCount; i++) {
                mGridPoints[index++] = mCropViewRect.left;
                mGridPoints[index++] = (mCropViewRect.height()
                        * (((float) i + 1.0f) / (float) (mCropGridRowCount + 1))) + mCropViewRect.top;
                mGridPoints[index++] = mCropViewRect.right;
                mGridPoints[index++] = (mCropViewRect.height()
                        * (((float) i + 1.0f) / (float) (mCropGridRowCount + 1))) + mCropViewRect.top;
            }

            for (int i = 0; i < mCropGridColumnCount; i++) {
                mGridPoints[index++] = (mCropViewRect.width()
                        * (((float) i + 1.0f) / (float) (mCropGridColumnCount + 1))) + mCropViewRect.left;
                mGridPoints[index++] = mCropViewRect.top;
                mGridPoints[index++] = (mCropViewRect.width()
                        * (((float) i + 1.0f) / (float) (mCropGridColumnCount + 1))) + mCropViewRect.left;
                mGridPoints[index++] = mCropViewRect.bottom;
            }
        }

        if (mGridPoints != null)
            canvas.drawLines(mGridPoints, mCropGridPaint);
    }

    if (mShowCropFrame)
        canvas.drawRect(mCropViewRect, mCropFramePaint);

    if (mFreestyleCropMode != FREESTYLE_CROP_MODE_DISABLE) {
        canvas.save();

        mTempRect.set(mCropViewRect);
        mTempRect.inset(mCropRectCornerTouchAreaLineLength, -mCropRectCornerTouchAreaLineLength);
        canvas.clipRect(mTempRect, Region.Op.DIFFERENCE);

        mTempRect.set(mCropViewRect);
        mTempRect.inset(-mCropRectCornerTouchAreaLineLength, mCropRectCornerTouchAreaLineLength);
        canvas.clipRect(mTempRect, Region.Op.DIFFERENCE);

        canvas.drawRect(mCropViewRect, mCropFrameCornersPaint);

        canvas.restore();
    }
}