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.visenze.visearch.camerademo.views.RoundImageView.java

@Override
protected void onDraw(Canvas canvas) {

    if (mBounds == null) {
        return;//from ww  w  .j  a v a 2  s. co m
    }

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

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

    canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));

    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);
        cacheCanvas
                .setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
        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:de.mrapp.android.util.view.ScrimInsetsLayout.java

@Override
public final void draw(final Canvas canvas) {
    super.draw(canvas);
    int width = getWidth();
    int height = getHeight();

    if (insets != null && insetDrawable != null) {
        int saveCount = canvas.save();
        canvas.translate(getScrollX(), getScrollY());
        insetDrawable.setBounds(0, 0, width, insets.top);
        insetDrawable.draw(canvas);/*from w ww . ja v  a  2s. c  om*/
        insetDrawable.setBounds(0, height - insets.bottom, width, height);
        insetDrawable.draw(canvas);
        insetDrawable.setBounds(0, insets.top, insets.left, height - insets.bottom);
        insetDrawable.draw(canvas);
        insetDrawable.setBounds(width - insets.right, insets.top, width, height - insets.bottom);
        insetDrawable.draw(canvas);
        canvas.restoreToCount(saveCount);
    }
}

From source file:org.telegram.ui.Components.AvatarDrawable.java

@Override
public void draw(Canvas canvas) {
    Rect bounds = getBounds();//w  w w.j a  v a  2  s .  c  om
    if (bounds == null) {
        return;
    }
    int size = bounds.width();
    paint.setColor(color);
    canvas.save();
    canvas.translate(bounds.left, bounds.top);
    canvas.drawCircle(size / 2, size / 2, size / 2, paint);

    if (drawBrodcast && broadcastDrawable != null) {
        int x = (size - broadcastDrawable.getIntrinsicWidth()) / 2;
        int y = (size - broadcastDrawable.getIntrinsicHeight()) / 2;
        broadcastDrawable.setBounds(x, y, x + broadcastDrawable.getIntrinsicWidth(),
                y + broadcastDrawable.getIntrinsicHeight());
        broadcastDrawable.draw(canvas);
    } else {
        if (textLayout != null) {
            canvas.translate((size - textWidth) / 2 - textLeft, (size - textHeight) / 2);
            textLayout.draw(canvas);
        } else if (drawPhoto && photoDrawable != null) {
            int x = (size - photoDrawable.getIntrinsicWidth()) / 2;
            int y = (size - photoDrawable.getIntrinsicHeight()) / 2;
            photoDrawable.setBounds(x, y, x + photoDrawable.getIntrinsicWidth(),
                    y + photoDrawable.getIntrinsicHeight());
            photoDrawable.draw(canvas);
        }
    }
    canvas.restore();
}

From source file:danonino.danonino_the_game.Core.GamePanel.java

@Override
public void draw(Canvas canvas) {
    final float scaleFactorX = getWidth() / (WIDTH * 1.f);
    final float scaleFactorY = getHeight() / (HEIGHT * 1.f);
    if (canvas != null) {
        final int savedState = canvas.save();
        canvas.scale(scaleFactorX, scaleFactorY);
        this.bg.draw(canvas);
        this.progress.draw(canvas);

        //draw butterflies
        for (Butterfly b : this.butterflies) {
            b.draw(canvas);//from   w  w  w.  j  av a 2  s  . c  o m
        }
        //draw enemies
        for (Fruit e : this.enemies) {
            e.draw(canvas);
        }
        this.player.draw(canvas);

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

        if (!this.gameOver) {
            if (this.joystickEnabled) {
                this.joystick.draw(canvas);
            }

            if (this.powerUpLabel != null) {
                this.powerUpLabel.draw(canvas);
            }
        } else {
            GameOver.draw(canvas);
        }
        this.bgFront.draw(canvas);
        canvas.restoreToCount(savedState);
    }
}

From source file:org.wikipedia.page.shareafact.SnippetImage.java

private static void drawWordmarkFromText(@NonNull Context context, @NonNull Canvas canvas,
        boolean isArticleRTL) {
    final int maxWidth = WIDTH - DESCRIPTION_WIDTH - 2 * HORIZONTAL_PADDING;
    final float fontSize = 20.0f;
    final float scaleX = 1.06f;

    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);//w w w. j  a  v  a2 s.  co m
    textPaint.setColor(Color.LTGRAY);
    textPaint.setTextSize(fontSize);
    textPaint.setTypeface(SERIF);
    textPaint.setTextScaleX(scaleX);

    Spanned wikipedia = StringUtil.fromHtml(context.getString(R.string.wp_stylized));
    Layout.Alignment align = L10nUtil.isDeviceRTL() ? ALIGN_OPPOSITE : ALIGN_NORMAL;
    StaticLayout wordmarkLayout = buildLayout(
            new TextLayoutParams(wikipedia, textPaint, maxWidth, 1.0f, align));
    final int width = (int) wordmarkLayout.getLineWidth(0);
    final int height = wordmarkLayout.getHeight();

    final int bottom = HEIGHT - BOTTOM_PADDING;
    final int top = bottom - height;

    int left = WIDTH - HORIZONTAL_PADDING - width;
    if (isArticleRTL) {
        left = HORIZONTAL_PADDING;
    }

    canvas.save(); // --
    canvas.translate(left, top);
    wordmarkLayout.draw(canvas);
    canvas.restore(); // --
}

From source file:com.bluepixel.android.sgpool.ui.widget.SwipeProgressBar.java

void draw(Canvas canvas) {
    final int width = mBounds.width();
    final int cx = width / 2;
    final int cy = mBounds.top + mBounds.height() / 2;
    boolean drawTriggerWhileFinishing = false;
    int restoreCount = canvas.save();
    canvas.clipRect(mBounds);//from  ww  w . ja  v a  2s.  c  o m
    if (mRunning || (mFinishTime > 0)) {
        long now = AnimationUtils.currentAnimationTimeMillis();
        long elapsed = (now - mStartTime) % ANIMATION_DURATION_MS;
        long iterations = (now - mStartTime) / ANIMATION_DURATION_MS;
        float rawProgress = (elapsed / (ANIMATION_DURATION_MS / 100f));
        // If we're not running anymore, that means we're running through
        // the finish animation.
        if (!mRunning) {
            // If the finish animation is done, don't draw anything, and
            // don't repost.
            if ((now - mFinishTime) >= FINISH_ANIMATION_DURATION_MS) {
                mFinishTime = 0;
                return;
            }
            // Otherwise, use a 0 opacity alpha layer to clear the animation
            // from the inside out. This layer will prevent the circles from
            // drawing within its bounds.
            long finishElapsed = (now - mFinishTime) % FINISH_ANIMATION_DURATION_MS;
            float finishProgress = (finishElapsed / (FINISH_ANIMATION_DURATION_MS / 100f));
            float pct = (finishProgress / 100f);
            // Radius of the circle is half of the screen.
            float clearRadius = width / 2 * INTERPOLATOR.getInterpolation(pct);
            mClipRect.set(cx - clearRadius, mBounds.top, cx + clearRadius, mBounds.bottom);
            canvas.saveLayerAlpha(mClipRect, 0, 0);
            // Only draw the trigger if there is a space in the center of
            // this refreshing view that needs to be filled in by the
            // trigger. If the progress view is just still animating, let it
            // continue animating.
            drawTriggerWhileFinishing = true;
        }
        // First fill in with the last color that would have finished drawing.
        if (iterations == 0) {
            canvas.drawColor(mColor1);
        } else {
            if (rawProgress >= 0 && rawProgress < 25) {
                canvas.drawColor(mColor4);
            } else if (rawProgress >= 25 && rawProgress < 50) {
                canvas.drawColor(mColor1);
            } else if (rawProgress >= 50 && rawProgress < 75) {
                canvas.drawColor(mColor2);
            } else {
                canvas.drawColor(mColor3);
            }
        }
        // Then draw up to 4 overlapping concentric circles of varying radii, based on how far
        // along we are in the cycle.
        // progress 0-50 draw mColor2
        // progress 25-75 draw mColor3
        // progress 50-100 draw mColor4
        // progress 75 (wrap to 25) draw mColor1
        if ((rawProgress >= 0 && rawProgress <= 25)) {
            float pct = (((rawProgress + 25) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor1, pct);
        }
        if (rawProgress >= 0 && rawProgress <= 50) {
            float pct = ((rawProgress * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor2, pct);
        }
        if (rawProgress >= 25 && rawProgress <= 75) {
            float pct = (((rawProgress - 25) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor3, pct);
        }
        if (rawProgress >= 50 && rawProgress <= 100) {
            float pct = (((rawProgress - 50) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor4, pct);
        }
        if ((rawProgress >= 75 && rawProgress <= 100)) {
            float pct = (((rawProgress - 75) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor1, pct);
        }
        if (mTriggerPercentage > 0 && drawTriggerWhileFinishing) {
            // There is some portion of trigger to draw. Restore the canvas,
            // then draw the trigger. Otherwise, the trigger does not appear
            // until after the bar has finished animating and appears to
            // just jump in at a larger width than expected.
            canvas.restoreToCount(restoreCount);
            restoreCount = canvas.save();
            canvas.clipRect(mBounds);
            drawTrigger(canvas, cx, cy);
        }
        // Keep running until we finish out the last cycle.
        ViewCompat.postInvalidateOnAnimation(mParent);
    } 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.justwayward.reader.view.RVPIndicator.java

/**
 * (?view)/*from   ww  w .  jav  a 2 s .  c  o  m*/
 */
@Override
protected void dispatchDraw(Canvas canvas) {
    // ?
    canvas.save();

    switch (mIndicatorStyle) {

    case STYLE_BITMAP:

        canvas.translate(mTranslationX, 0);
        canvas.drawBitmap(mBitmap, null, mRectF, mPaint);

        break;
    case STYLE_LINE:

        canvas.translate(mTranslationX, getHeight() - mIndicatorHeight);
        canvas.drawRect(mRectF, mPaint);

        break;
    case STYLE_SQUARE:

        canvas.translate(mTranslationX, 0);
        canvas.drawRect(mRectF, mPaint);

        break;
    case STYLE_TRIANGLE:

        canvas.translate(mTranslationX, 0);
        // 
        // mPaint.setPathEffect(new CornerPathEffect(10));
        mPath = new Path();
        int midOfTab = getWidth() / mTabVisibleCount / 2;
        mPath.moveTo(midOfTab, getHeight() - mIndicatorHeight);
        mPath.lineTo(midOfTab - mIndicatorWidth / 2, getHeight());
        mPath.lineTo(midOfTab + mIndicatorWidth / 2, getHeight());
        mPath.close();
        canvas.drawPath(mPath, mPaint);

        break;
    }

    // ??
    canvas.restore();
    super.dispatchDraw(canvas);
}

From source file:org.chromium.chrome.browser.omnibox.LocationBarPhone.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    boolean needsCanvasRestore = false;
    if (child == mUrlBar && mUrlActionsContainer.getVisibility() == VISIBLE) {
        canvas.save();

        // Clip the URL bar contents to ensure they do not draw under the URL actions during
        // focus animations.  Based on the RTL state of the location bar, the url actions
        // container can be on the left or right side, so clip accordingly.
        if (mUrlBar.getLeft() < mUrlActionsContainer.getLeft()) {
            canvas.clipRect(0, 0, (int) mUrlActionsContainer.getX(), getBottom());
        } else {/*w  ww .  jav a2 s . c  o  m*/
            canvas.clipRect(mUrlActionsContainer.getX() + mUrlActionsContainer.getWidth(), 0, getWidth(),
                    getBottom());
        }
        needsCanvasRestore = true;
    }
    boolean retVal = super.drawChild(canvas, child, drawingTime);
    if (needsCanvasRestore) {
        canvas.restore();
    }
    return retVal;
}

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

/**
 * This method draws dimmed area around the crop bounds.
 *
 * @param canvas - valid canvas object/*  w  ww. j  a  v a  2  s . c o m*/
 */
protected void drawDimmedLayer(@NonNull Canvas canvas) {
    canvas.save();
    if (mCircleDimmedLayer)
        canvas.clipPath(mCircularPath, Region.Op.DIFFERENCE);
    else
        canvas.clipRect(mCropViewRect, Region.Op.DIFFERENCE);
    canvas.drawColor(mDimmedColor);
    canvas.restore();

    if (mCircleDimmedLayer) { // Draw 1px stroke to fix antialias
        canvas.drawCircle(mCropViewRect.centerX(), mCropViewRect.centerY(),
                Math.min(mCropViewRect.width(), mCropViewRect.height()) / 2.f, mDimmedStrokePaint);
    }
}

From source file:com.amitupadhyay.aboutexample.util.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;
        final float descent;
        // Update the TextPaint to the current text size
        mTextPaint.setTextSize(mCurrentTextSize);
        if (drawTexture) {
            ascent = mTextureAscent * mScale;
            descent = mTextureDescent * mScale;
        } else {/*from   ww  w  .  ja va  2s.  c  o  m*/
            ascent = mTextPaint.ascent() * mScale;
            descent = mTextPaint.descent() * mScale;
        }
        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta rect in the text bounds
            canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent,
                    DEBUG_DRAW_PAINT);
        }
        if (drawTexture) {
            y += ascent;
        }
        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);
}