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.dream.yzbb.wolfkiller.widget.BezelImageView.java

@Override
protected void onDraw(Canvas canvas) {
    if (mBounds == null) {
        return;/*  w w  w . jav a2s . c o m*/
    }

    int width = mBounds.width();
    int height = mBounds.height();

    if (width == 0 || height == 0) {
        return;
    }

    if (!mCacheValid || width != mCachedWidth || height != mCachedHeight) {
        // Need to redraw the cache.
        if (width == mCachedWidth && height == mCachedHeight) {
            // Have a correct-sized bitmap cache already allocated. Just erase it.
            mCacheBitmap.eraseColor(0);
        } else {
            // Allocate a new bitmap with the correct dimensions.
            mCacheBitmap.recycle();
            //noinspection AndroidLintDrawAllocation
            mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            mCachedWidth = width;
            mCachedHeight = height;
        }

        Canvas cacheCanvas = new Canvas(mCacheBitmap);
        if (mMaskDrawable != null) {
            int sc = cacheCanvas.save();
            mMaskDrawable.draw(cacheCanvas);
            mMaskedPaint.setColorFilter((mDesaturateOnPress && isPressed())
                    ? mDesaturateColorFilter : null);
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else if (mDesaturateOnPress && isPressed()) {
            int sc = cacheCanvas.save();
            cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint);
            mMaskedPaint.setColorFilter(mDesaturateColorFilter);
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else {
            super.onDraw(cacheCanvas);
        }

        if (mBorderDrawable != null) {
            mBorderDrawable.draw(cacheCanvas);
        }
    }

    // Draw from cache.
    canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null);
}

From source file:com.popdeem.sdk.uikit.widget.PDUIBezelImageView.java

@Override
protected void onDraw(Canvas canvas) {
    if (mBounds == null) {
        return;//from   ww  w  .  j  a  va 2 s. c o  m
    }

    int width = mBounds.width();
    int height = mBounds.height();

    if (width == 0 || height == 0) {
        return;
    }

    if (!mCacheValid || width != mCachedWidth || height != mCachedHeight) {
        // Need to redraw the cache
        if (width == mCachedWidth && height == mCachedHeight) {
            // Have a correct-sized bitmap cache already allocated. Just erase it.
            mCacheBitmap.eraseColor(0);
        } else {
            // Allocate a new bitmap with the correct dimensions.
            mCacheBitmap.recycle();
            //noinspection AndroidLintDrawAllocation
            mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            mCachedWidth = width;
            mCachedHeight = height;
        }

        Canvas cacheCanvas = new Canvas(mCacheBitmap);
        if (mMaskDrawable != null) {
            int sc = cacheCanvas.save();
            mMaskDrawable.draw(cacheCanvas);
            mMaskedPaint.setColorFilter((mDesaturateOnPress && isPressed()) ? mDesaturateColorFilter : null);
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint, Canvas.ALL_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else if (mDesaturateOnPress && isPressed()) {
            int sc = cacheCanvas.save();
            cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint);
            mMaskedPaint.setColorFilter(mDesaturateColorFilter);
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint, Canvas.ALL_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else {
            super.onDraw(cacheCanvas);
        }

        if (mBorderDrawable != null) {
            mBorderDrawable.draw(cacheCanvas);
        }
    }

    // Draw from cache
    canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null);
}

From source file:com.flyn.smartandroid.views.DrawerArrowDrawable.java

@Override
public void draw(Canvas canvas) {
    if (flip) {/*  w  w w. ja  v  a 2  s. c  o  m*/
        canvas.save();
        canvas.scale(1f, -1f, getIntrinsicWidth() / 2, getIntrinsicHeight() / 2);
    }

    topLine.draw(canvas);
    middleLine.draw(canvas);
    bottomLine.draw(canvas);

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

From source file:io.palaima.debugdrawer.view.ScrimInsetsFrameLayout.java

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

    int width = getWidth();
    int height = getHeight();
    if (insets != null && insetForeground != null) {
        int sc = canvas.save();
        canvas.translate(getScrollX(), getScrollY());

        // Top/*  w  w  w  .  j a  v a2  s. co m*/
        tempRect.set(0, 0, width, insets.top);
        insetForeground.setBounds(tempRect);
        insetForeground.draw(canvas);

        // Bottom
        tempRect.set(0, height - insets.bottom, width, height);
        insetForeground.setBounds(tempRect);
        insetForeground.draw(canvas);

        // Left
        tempRect.set(0, insets.top, insets.left, height - insets.bottom);
        insetForeground.setBounds(tempRect);
        insetForeground.draw(canvas);

        // Right
        tempRect.set(width - insets.right, insets.top, width, height - insets.bottom);
        insetForeground.setBounds(tempRect);
        insetForeground.draw(canvas);

        canvas.restoreToCount(sc);
    }
}

From source file:com.odoo.followup.orm.widgets.BezelImageView.java

@Override
protected void onDraw(Canvas canvas) {
    if (mBounds == null) {
        return;//from   w ww. j  a  va 2  s  .  c om
    }

    int width = mBounds.width();
    int height = mBounds.height();

    if (width == 0 || height == 0) {
        return;
    }

    if (!mCacheValid || width != mCachedWidth || height != mCachedHeight) {
        // Need to redraw the cache
        if (width == mCachedWidth && height == mCachedHeight) {
            // Have a correct-sized bitmap cache already allocated. Just
            // erase it.
            mCacheBitmap.eraseColor(0);
        } else {
            // Allocate a new bitmap with the correct dimensions.
            mCacheBitmap.recycle();
            // noinspection AndroidLintDrawAllocation
            mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            mCachedWidth = width;
            mCachedHeight = height;
        }

        Canvas cacheCanvas = new Canvas(mCacheBitmap);
        if (mMaskDrawable != null) {
            int sc = cacheCanvas.save();
            mMaskDrawable.draw(cacheCanvas);
            mMaskedPaint.setColorFilter((mDesaturateOnPress && isPressed()) ? mDesaturateColorFilter : null);
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else if (mDesaturateOnPress && isPressed()) {
            int sc = cacheCanvas.save();
            cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint);
            mMaskedPaint.setColorFilter(mDesaturateColorFilter);
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else {
            super.onDraw(cacheCanvas);
        }

        if (mBorderDrawable != null) {
            mBorderDrawable.draw(cacheCanvas);
        }
    }

    // Draw from cache
    canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null);
}

From source file:im.vector.view.SimpleDividerItemDecoration.java

@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    if (parent.getLayoutManager() == null) {
        return;//from w w w  .j  a va2 s. c  o m
    }
    if (mOrientation == HORIZONTAL || mLeftMargin <= 0) {
        super.onDraw(canvas, parent, state);
    } else {
        canvas.save();
        int right = parent.getWidth() - parent.getPaddingRight();

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            View nextChild = parent.getChildAt(i + 1);
            if (!String.valueOf(child.getTag()).contains(NO_DIVIDER_TAG)
                    && (nextChild != null && !String.valueOf(nextChild.getTag()).contains(NO_DIVIDER_TAG))) {
                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

                int top = child.getBottom() + params.bottomMargin;
                int bottom = top + mDivider.getIntrinsicHeight();

                mDivider.setBounds(mLeftMargin, top, right, bottom);
                mDivider.draw(canvas);
            }
        }
        canvas.restore();
    }
}

From source file:com.android.mail.browse.SubjectAndFolderView.java

public void setFolders(ConversationViewHeaderCallbacks callbacks, Account account, Conversation conv) {
    mVisibleFolders = true;// w  w  w  .  j ava  2 s  .c  o m
    final BidiFormatter bidiFormatter = getBidiFormatter();
    final String wrappedSubject = mSubject == null ? "" : bidiFormatter.unicodeWrap(mSubject);
    final SpannableStringBuilder sb = new SpannableStringBuilder(wrappedSubject);
    sb.append('\u0020');
    final Settings settings = account.settings;
    final int start = sb.length();
    if (settings.importanceMarkersEnabled && conv.isImportant()) {
        sb.append(".\u0020");
        sb.setSpan(new ReplacementSpan() {
            @Override
            public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
                return mImportanceMarkerDrawable.getIntrinsicWidth();
            }

            @Override
            public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top,
                    int baseline, int bottom, Paint paint) {
                canvas.save();
                final int transY = baseline + mChipVerticalOffset
                        - mImportanceMarkerDrawable.getIntrinsicHeight();
                canvas.translate(x, transY);
                mImportanceMarkerDrawable.draw(canvas);
                canvas.restore();
            }
        }, start, start + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    }

    mFolderDisplayer.loadConversationFolders(conv, null /* ignoreFolder */, -1 /* ignoreFolderType */);
    mFolderDisplayer.constructFolderChips(sb);

    final int end = sb.length();
    sb.setSpan(new ChangeLabelsSpan(callbacks), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    setText(sb);
    setMovementMethod(LinkMovementMethod.getInstance());
}

From source file:com.github.czy1121.view.CornerLabelView.java

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

    float xy = mHeight / SQRT2;
    canvas.save();
    canvas.translate(xy, xy);//from w  w  w .j a v a 2 s  .  co  m
    canvas.rotate((mIsTop ? 1 : -1) * (mIsLeft ? -45 : 45));
    canvas.drawPath(calcPath(), mPaint);

    mText1.draw(canvas, mPaddingBottom, mIsTop);
    if (mIsTriangle) {
        mText2.draw(canvas, mPaddingBottom + mPaddingCenter + mText1.height, mIsTop);
    }
    canvas.restore();
}

From source file:com.abewy.android.apps.klyph.messenger.widget.BezelImageView.java

@Override
protected void onDraw(Canvas canvas) {
    if (mBounds == null) {
        return;/*  w  w w .ja va2 s  .  co m*/
    }

    int width = mBounds.width();
    int height = mBounds.height();

    if (width == 0 || height == 0) {
        return;
    }

    if (!mCacheValid || width != mCachedWidth || height != mCachedHeight) {
        // Need to redraw the cache
        if (width == mCachedWidth && height == mCachedHeight) {
            // Have a correct-sized bitmap cache already allocated. Just erase it.
            mCacheBitmap.eraseColor(0);
        } else {
            // Allocate a new bitmap with the correct dimensions.
            mCacheBitmap.recycle();
            // noinspection AndroidLintDrawAllocation
            mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            mCachedWidth = width;
            mCachedHeight = height;
        }

        Canvas cacheCanvas = new Canvas(mCacheBitmap);
        if (mMaskDrawable != null && mMaskEnabled == true) {
            int sc = cacheCanvas.save();
            mMaskDrawable.draw(cacheCanvas);
            mMaskedPaint.setColorFilter((mDesaturateOnPress && isPressed()) ? mDesaturateColorFilter : null);
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else if (mDesaturateOnPress && isPressed()) {
            int sc = cacheCanvas.save();
            cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint);
            mMaskedPaint.setColorFilter(mDesaturateColorFilter);
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else {
            super.onDraw(cacheCanvas);
        }

        if (mBorderDrawable != null) {
            mBorderDrawable.draw(cacheCanvas);
        }
    }

    // Draw from cache
    canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null);
}

From source file:com.crs4.roodin.moduletester.CustomView.java

/**
 * @return/*ww  w  .j  a  v  a2 s. com*/
 */
private Picture drawBarreds() {
    Picture pictureContent = new Picture();
    Canvas canvasContent = pictureContent.beginRecording(getWidth(), getHeight());
    Paint paintContent = new Paint(Paint.ANTI_ALIAS_FLAG);
    canvasContent.save();

    paintContent.setColor(Color.BLUE);
    paintContent.setStyle(Style.STROKE);
    paintContent.setStrokeWidth(2);
    paintContent.setAlpha(50);

    try {
        cellDimension = block.getCellDimension();
        JSONArray barred = block.getBarred();

        int rows = block.getShape().getInt(0);
        int cols = block.getShape().getInt(1);

        // this code draws all the shape:
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {

                canvasContent.drawRect(cellDimension * j, cellDimension * i, cellDimension * j + cellDimension,
                        cellDimension * i + cellDimension, paintContent);
            }
        }
        paintContent.setColor(Color.RED);
        paintContent.setStyle(Style.FILL);
        paintContent.setAlpha(50);

        for (int i = 0; i < barred.length(); i++) {
            JSONArray pos = barred.getJSONArray(i);
            canvasContent.drawRect(cellDimension * pos.getInt(1), cellDimension * pos.getInt(0),
                    cellDimension * pos.getInt(1) + cellDimension,
                    cellDimension * pos.getInt(0) + cellDimension, paintContent);
        }

        this.paintBoxList(canvasContent, paintContent);

    } catch (JSONException e) {
        e.printStackTrace();
        Log.e("Exception in DrawComponents.drawBarreds", "" + e.getMessage());
    }

    canvasContent.restore();
    pictureContent.endRecording();
    return pictureContent;

}