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.hippo.widget.slidingdrawerlayout.SlidingDrawerLayout.java

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

    if (mFitPaddingTop != 0) {
        int saved = canvas.save();
        canvas.clipRect(0, 0, getWidth(), mFitPaddingTop);
        canvas.drawColor(mStatusBarColor);
        canvas.restoreToCount(saved);//from   w w  w .  j av  a2 s .  c  om
    }

    if (mFitPaddingBottom != 0) {
        int saved = canvas.save();
        int height = getHeight();
        canvas.clipRect(0, height - mFitPaddingBottom, getWidth(), height);
        canvas.drawColor(mNavigationBarColor);
        canvas.restoreToCount(saved);
    }
}

From source file:com.lab47billion.appchooser.HorizontalPicker.java

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

    int saveCount = canvas.getSaveCount();
    canvas.save();

    int selectedItem = mSelectedItem;

    float itemWithPadding = mItemWidth + mDividerSize;

    // translate horizontal to center
    canvas.translate(itemWithPadding * mSideItems, 0);

    if (mValues != null) {
        for (int i = 0; i < mValues.length; i++) {

            mTextPaint.setColor(getTextColor(i));

            // get text layout
            View layout = mLayouts[i][0];
            //                layout.draw(canvas);
            //NEW
            View layoutSub = mLayouts[i][1];
            int saveCountHeight = canvas.getSaveCount();
            canvas.save();// w ww . j a v a  2 s .c  o  m

            float x = 0;
            float lineWidth = 40f;
            if (lineWidth > mItemWidth) {
                if (isRtl(mValues[i])) {
                    x += (lineWidth - mItemWidth) / 2;
                } else {
                    x -= (lineWidth - mItemWidth) / 2;
                }
            }

            /*set select item top padding */
            int yTranslate;
            if (!isScrollingStart && i == selectedItem) {
                mTextPaint.setTextSize(mSelectedTextSize);
            } else {
                mTextPaint.setTextSize(mNormalTextSize);
            }

            // translate vertically to center
            if (layoutSub != null) {
                yTranslate = (canvas.getHeight() - (layout.getHeight() + layoutSub.getHeight())) / 2;
            } else {
                yTranslate = (canvas.getHeight() - layout.getHeight()) / 2;
            }

            if (i == selectedItem && topPadding != -1) {
                yTranslate = topPadding;
            }

            canvas.translate(-x, yTranslate);

            RectF clipBounds;
            if (x == 0) {
                clipBounds = mItemClipBounds;
            } else {
                clipBounds = mItemClipBoundsOffser;
                clipBounds.set(mItemClipBounds);
                clipBounds.offset(x, 0);
            }
            canvas.clipRect(clipBounds);
            layout.draw(canvas);

            if (layoutSub != null) {
                Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
                int heightText = (int) (Math.abs(fontMetrics.ascent) + Math.abs(fontMetrics.descent));
                canvas.translate(-x, (layout.getHeight() + heightText - getTopPadding()) / 2);
                canvas.clipRect(clipBounds);
                layoutSub.draw(canvas);
            }

            // restore vertical translation
            canvas.restoreToCount(saveCountHeight);

            // translate horizontal for 1 item
            canvas.translate(itemWithPadding, 0);
        }
    }

    // restore horizontal translation
    canvas.restoreToCount(saveCount);

    drawEdgeEffect(canvas, mLeftEdgeEffect, 270);
    drawEdgeEffect(canvas, mRightEdgeEffect, 90);
}

From source file:android.widget.PinnedHeaderListView.java

@Override
protected void dispatchDraw(final Canvas canvas) {
    final long currentTime = mAnimating ? System.currentTimeMillis() : 0;

    int top = 0;/*from w  w w .  j a  va  2 s  . co m*/
    final int right = 0;
    int bottom = getBottom();
    boolean hasVisibleHeaders = false;
    for (int i = 0; i < mSize; i++) {
        final PinnedHeader header = mHeaders[i];
        if (header.visible) {
            hasVisibleHeaders = true;
            if (header.state == BOTTOM && header.y < bottom)
                bottom = header.y;
            else if (header.state == TOP || header.state == FADING) {
                final int newTop = header.y + header.height;
                if (newTop > top)
                    top = newTop;
            }
        }
    }

    if (hasVisibleHeaders)
        canvas.save();

    super.dispatchDraw(canvas);

    if (hasVisibleHeaders) {
        canvas.restore();

        // If the first item is visible and if it has a positive top that is greater than the
        // first header's assigned y-value, use that for the first header's y value. This way,
        // the header inherits any padding applied to the list view.
        if (mSize > 0 && getFirstVisiblePosition() == 0) {
            final View firstChild = getChildAt(0);
            final PinnedHeader firstHeader = mHeaders[0];

            if (firstHeader != null) {
                final int firstHeaderTop = firstChild != null ? firstChild.getTop() : 0;
                firstHeader.y = Math.max(firstHeader.y, firstHeaderTop);
            }
        }

        // First draw top headers, then the bottom ones to handle the Z axis correctly
        for (int i = mSize; --i >= 0;) {
            final PinnedHeader header = mHeaders[i];
            if (header.visible && (header.state == TOP || header.state == FADING))
                drawHeader(canvas, header, currentTime);
        }

        for (int i = 0; i < mSize; i++) {
            final PinnedHeader header = mHeaders[i];
            if (header.visible && header.state == BOTTOM)
                drawHeader(canvas, header, currentTime);
        }
    }

    invalidateIfAnimating();
}

From source file:com.example.nitish.welcomapp.widgetpt.PeriodicTableView.java

/**
 * Draw the edge effects to the supplied Canvas.
 *
 * @param canvas The Canvas/*ww w. j  a va2  s  .c om*/
 */
private void drawEdgeEffects(@NonNull Canvas canvas) {
    boolean invalidate = false;

    if (!mEdgeEffectTop.isFinished()) {
        mEdgeEffectTop.draw(canvas);
        invalidate = true;
    }
    if (!mEdgeEffectBottom.isFinished()) {
        canvas.save();
        canvas.rotate(180, getWidth() / 2, getHeight() / 2);
        mEdgeEffectBottom.draw(canvas);
        canvas.restore();
        invalidate = true;
    }
    if (!mEdgeEffectLeft.isFinished()) {
        canvas.save();
        canvas.translate(0, getHeight());
        canvas.rotate(-90);
        mEdgeEffectLeft.draw(canvas);
        canvas.restore();
        invalidate = true;
    }
    if (!mEdgeEffectRight.isFinished()) {
        canvas.save();
        canvas.translate(getWidth(), 0);
        canvas.rotate(90, 0, 0);
        mEdgeEffectRight.draw(canvas);
        canvas.restore();
        invalidate = true;
    }

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

From source file:am.widget.tabstrip.HorizontalLinearTabStripLayout.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    final int count = getChildCount();
    if (mCenter == null && !isShowingDividers() && count <= 0)
        return;/*from w w w.  j a va 2  s. com*/
    final int paddingStart = ViewCompat.getPaddingStart(this);
    final int paddingTop = getPaddingTop();
    final int paddingBottom = getPaddingBottom();
    final int childWidth = mChildWidth;
    final boolean show = isShowingDividers();
    final Drawable dd = mDivider;
    final int divider = show ? dd.getIntrinsicWidth() : 0;
    final int padding = mDividerPadding;
    int start = paddingStart;
    if (show)
        dd.setBounds(1, paddingTop + padding, divider + 1, getHeight() - paddingBottom - padding);
    if (count == 1) {
        if (show && (mShowDividers & SHOW_DIVIDER_BEGINNING) == SHOW_DIVIDER_BEGINNING) {
            canvas.save();
            canvas.translate(start, 0);
            dd.draw(canvas);
            canvas.restore();
            start += divider;
        }
        start += childWidth;
        if (show && (mShowDividers & SHOW_DIVIDER_END) == SHOW_DIVIDER_END) {
            canvas.save();
            canvas.translate(start, 0);
            dd.draw(canvas);
            canvas.restore();
        }
    } else {
        final boolean middle = (mShowDividers & SHOW_DIVIDER_MIDDLE) == SHOW_DIVIDER_MIDDLE;
        for (int i = 0; i < count; i++) {
            if (i == 0) {
                if (show && (mShowDividers & SHOW_DIVIDER_BEGINNING) == SHOW_DIVIDER_BEGINNING) {
                    canvas.save();
                    canvas.translate(start, 0);
                    dd.draw(canvas);
                    canvas.restore();
                    start += divider;
                }
                start += childWidth;
            } else if (i == count - 1) {
                if (show && middle) {
                    canvas.save();
                    canvas.translate(start, 0);
                    dd.draw(canvas);
                    canvas.restore();
                    start += divider;
                }
                start += childWidth;
                if (show && (mShowDividers & SHOW_DIVIDER_END) == SHOW_DIVIDER_END) {
                    start = getWidth() - ViewCompat.getPaddingEnd(this) - divider;
                    canvas.save();
                    canvas.translate(start, 0);
                    dd.draw(canvas);
                    canvas.restore();
                    break;
                }
            } else {
                if (count % 2 == 0 && count / 2 == i && mCenter != null) {
                    if (show && middle && mCenterAsItem) {
                        canvas.save();
                        canvas.translate(start, 0);
                        dd.draw(canvas);
                        canvas.restore();
                        start += divider;
                    }
                    final Drawable center = mCenter;
                    final int p = mCenterPadding;
                    center.setBounds(0, paddingTop + p, center.getIntrinsicWidth(),
                            getHeight() - paddingBottom - p);
                    canvas.save();
                    canvas.translate(start, 0);
                    center.draw(canvas);
                    canvas.restore();
                    start += center.getIntrinsicWidth();
                    if (show && middle && mCenterAsItem) {
                        canvas.save();
                        canvas.translate(start, 0);
                        dd.draw(canvas);
                        canvas.restore();
                        start += divider;
                    }
                } else {
                    if (show && middle) {
                        canvas.save();
                        canvas.translate(start, 0);
                        dd.draw(canvas);
                        canvas.restore();
                        start += divider;
                    }
                }
                start += childWidth;
            }
        }
    }
}

From source file:com.mylikes.likes.etchasketch.Slate.java

private void drawStrokeDebugInfo(Canvas c) {
    final int ROW_HEIGHT = 24;
    final int ROW_MARGIN = 6;
    final int COLUMN_WIDTH = 55;

    final float FIRM_PRESSURE_LOW = 0.85f;
    final float FIRM_PRESSURE_HIGH = 1.25f;

    if (mStrokeDebugGraph == null) {
        final int width = c.getWidth() - 128;
        final int height = ROW_HEIGHT * mStrokes.length + 2 * ROW_MARGIN;
        mStrokeDebugGraph = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        if (mStrokeDebugGraph == null) {
            throw new RuntimeException(
                    "drawStrokeDebugInfo: couldn't create debug bitmap (" + width + "x" + height + ")");
        }//from  w w  w  . ja v  a2s.  co  m
        mGraphPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    Canvas graph = new Canvas(mStrokeDebugGraph);
    graph.save();
    graph.clipRect(new Rect(0, 0, COLUMN_WIDTH, graph.getHeight()));
    graph.drawColor(0, PorterDuff.Mode.CLEAR);
    graph.restore();

    int left = 4;
    int bottom = graph.getHeight() - ROW_MARGIN;
    final int STEP = 4;
    for (MarkersPlotter st : mStrokes) {
        float r = st.getLastPressure();

        if (r >= FIRM_PRESSURE_LOW && r <= FIRM_PRESSURE_HIGH)
            mGraphPaint1.setColor(0xFF33FF33);
        else if (r < FIRM_PRESSURE_LOW)
            mGraphPaint1.setColor(0xFF808080);
        else
            mGraphPaint1.setColor(0xFFFF8000);

        String s = (r < 0) ? "--"
                : String.format("%s %.4f", ((st.getLastTool() == MotionEvent.TOOL_TYPE_STYLUS) ? "S" : "F"), r);

        graph.drawText(s, left, bottom - 2, mGraphPaint1);

        if (mGraphX + COLUMN_WIDTH > graph.getWidth()) {
            mGraphX = 0;
            graph.save();
            graph.clipRect(new Rect(30, 0, graph.getWidth(), graph.getHeight()));
            graph.drawColor(0, PorterDuff.Mode.CLEAR);
            graph.restore();
        }

        if (r >= 0) {
            int barsize = (int) (r * ROW_HEIGHT);
            graph.drawRect(mGraphX + COLUMN_WIDTH, bottom - barsize, mGraphX + COLUMN_WIDTH + STEP, bottom,
                    mGraphPaint1);
        } else {
            graph.drawPoint(mGraphX + COLUMN_WIDTH + STEP, bottom, mGraphPaint1);
        }
        bottom -= (ROW_HEIGHT + ROW_MARGIN);
    }

    mGraphX += STEP;

    final int x = 96;
    final int y = 64;

    c.drawBitmap(mStrokeDebugGraph, x, y, null);
    invalidate(new Rect(x, y, x + c.getWidth(), y + c.getHeight()));
}

From source file:ir.hatamiarash.calendar.view.QiblaCompassView.java

public void drawDial(Canvas canvas) {
    // over here//from  w  w  w . java2 s .  c om
    circlePaint.reset();
    circlePaint.setColor(ContextCompat.getColor(getContext(), R.color.qibla_color));
    circlePaint.setStrokeWidth(1);
    circlePaint.setStyle(Paint.Style.STROKE); // Sadece Cember ciziyor.

    int textHeight = (int) textPaint.measureText("yY");
    markerPaint.reset();
    markerPaint.setColor(ContextCompat.getColor(getContext(), R.color.qibla_color));
    // Draw the background
    canvas.drawCircle(px, py, Radius, circlePaint);
    canvas.drawCircle(px, py, Radius - 20, circlePaint);
    // Rotate our perspective so that the "top" is
    // facing the current bearing.

    int textWidth = (int) textPaint.measureText("W");
    int cardinalX = px - textWidth / 2;
    int cardinalY = py - Radius + textHeight;

    // Draw the marker every 15 degrees and text every 45.
    for (int i = 0; i < 24; i++) {
        // Draw a marker.
        canvas.drawLine(px, py - Radius, px, py - Radius + 10, markerPaint);
        canvas.save();
        canvas.translate(0, textHeight);
        // Draw the cardinal points
        if (i % 6 == 0) {
            String dirString = "";
            switch (i) {
            case (0): {
                dirString = northString;
                break;
            }
            case (6):
                dirString = eastString;
                break;
            case (12):
                dirString = southString;
                break;
            case (18):
                dirString = westString;
                break;
            }
            canvas.drawText(dirString, cardinalX, cardinalY, textPaint);
        } else if (i % 3 == 0) {
            // Draw the text every alternate 45deg
            String angle = String.valueOf(i * 15);
            float angleTextWidth = textPaint.measureText(angle);
            int angleTextX = (int) (px - angleTextWidth / 2);
            int angleTextY = py - Radius + textHeight;
            canvas.drawText(angle, angleTextX, angleTextY, textPaint);
        }
        canvas.restore();
        canvas.rotate(15, px, py);
    }
}

From source file:cn.androidy.androiddevelopmentpatterns.interactivechart.InteractiveLineGraphView.java

/**
 * Draws the overscroll "glow" at the four edges of the chart region, if necessary. The edges
 * of the chart region are stored in {@link #mContentRect}.
 *
 * @see EdgeEffectCompat/*from   w w  w. j ava  2s.  c om*/
 */
private void drawEdgeEffectsUnclipped(Canvas canvas) {
    // The methods below rotate and translate the canvas as needed before drawing the glow,
    // since EdgeEffectCompat always draws a top-glow at 0,0.

    boolean needsInvalidate = false;

    if (!mEdgeEffectTop.isFinished()) {
        final int restoreCount = canvas.save();
        canvas.translate(mContentRect.left, mContentRect.top);
        mEdgeEffectTop.setSize(mContentRect.width(), mContentRect.height());
        if (mEdgeEffectTop.draw(canvas)) {
            needsInvalidate = true;
        }
        canvas.restoreToCount(restoreCount);
    }

    if (!mEdgeEffectBottom.isFinished()) {
        final int restoreCount = canvas.save();
        canvas.translate(2 * mContentRect.left - mContentRect.right, mContentRect.bottom);
        canvas.rotate(180, mContentRect.width(), 0);
        mEdgeEffectBottom.setSize(mContentRect.width(), mContentRect.height());
        if (mEdgeEffectBottom.draw(canvas)) {
            needsInvalidate = true;
        }
        canvas.restoreToCount(restoreCount);
    }

    if (!mEdgeEffectLeft.isFinished()) {
        final int restoreCount = canvas.save();
        canvas.translate(mContentRect.left, mContentRect.bottom);
        canvas.rotate(-90, 0, 0);
        mEdgeEffectLeft.setSize(mContentRect.height(), mContentRect.width());
        if (mEdgeEffectLeft.draw(canvas)) {
            needsInvalidate = true;
        }
        canvas.restoreToCount(restoreCount);
    }

    if (!mEdgeEffectRight.isFinished()) {
        final int restoreCount = canvas.save();
        canvas.translate(mContentRect.right, mContentRect.top);
        canvas.rotate(90, 0, 0);
        mEdgeEffectRight.setSize(mContentRect.height(), mContentRect.width());
        if (mEdgeEffectRight.draw(canvas)) {
            needsInvalidate = true;
        }
        canvas.restoreToCount(restoreCount);
    }

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

From source file:com.byagowi.persiancalendar.view.QiblaCompassView.java

public void drawDial(Canvas canvas) {
    // over here//from  w  w w.j a va  2  s.  co  m
    circlePaint.reset();
    circlePaint.setColor(ContextCompat.getColor(getContext(), R.color.qibla_color));
    circlePaint.setStrokeWidth(1);
    circlePaint.setStyle(Paint.Style.STROKE); // Sadece Cember ciziyor.

    int textHeight = (int) textPaint.measureText("yY");
    markerPaint.reset();
    markerPaint.setColor(ContextCompat.getColor(getContext(), R.color.qibla_color));
    // Draw the background
    canvas.drawCircle(px, py, Radius, circlePaint);
    canvas.drawCircle(px, py, Radius - 20, circlePaint);
    // Rotate our perspective so that the "top" is
    // facing the current bearing.

    int textWidth = (int) textPaint.measureText("W");
    int cardinalX = px - textWidth / 2;
    int cardinalY = py - Radius + textHeight;

    // Draw the marker every 15 degrees and text every 45.
    for (int i = 0; i < 24; i++) {
        // Draw a marker.
        canvas.drawLine(px, py - Radius, px, py - Radius + 10, markerPaint);
        canvas.save();
        canvas.translate(0, textHeight);
        // Draw the cardinal points
        if (i % 6 == 0) {
            String dirString = "";
            switch (i) {
            case (0): {
                dirString = northString;
                break;
            }
            case (6):
                dirString = eastString;
                break;
            case (12):
                dirString = southString;
                break;
            case (18):
                dirString = westString;
                break;
            }
            canvas.drawText(dirString, cardinalX, cardinalY, textPaint);
        } else if (i % 3 == 0) {
            // Draw the text every alternate 45deg
            String angle = String.valueOf(i * 15);
            float angleTextWidth = textPaint.measureText(angle);
            int angleTextX = (int) (px - angleTextWidth / 2);
            int angleTextY = py - Radius + textHeight;
            canvas.drawText(angle, angleTextX, angleTextY, textPaint);
        }
        canvas.restore();

        canvas.rotate(15, px, py);
    }

}

From source file:com.android.launcher3.folder.FolderIcon.java

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

    if (mReferenceDrawable != null) {
        computePreviewDrawingParams(mReferenceDrawable);
    }/*  ww  w.ja  v  a  2  s.  com*/

    if (!mBackground.drawingDelegated()) {
        mBackground.drawBackground(canvas, mBgPaint);
    }

    if (mFolder == null)
        return;
    if (mFolder.getItemCount() == 0 && !mAnimating)
        return;

    canvas.save();

    if (mPreviewLayoutRule.clipToBackground()) {
        mBackground.clipCanvas(canvas);
    }

    // The items are drawn in coordinates relative to the preview offset
    canvas.translate(mBackground.basePreviewOffsetX, mBackground.basePreviewOffsetY);

    // The first item should be drawn last (ie. on top of later items)
    for (int i = mDrawingParams.size() - 1; i >= 0; i--) {
        PreviewItemDrawingParams p = mDrawingParams.get(i);
        if (!p.hidden) {
            drawPreviewItem(canvas, p);
        }
    }
    canvas.restore();

    if (mPreviewLayoutRule.clipToBackground() && !mBackground.drawingDelegated()) {
        mBackground.drawBackgroundStroke(canvas, mBgPaint);
    }
}