Example usage for android.graphics Canvas restore

List of usage examples for android.graphics Canvas restore

Introduction

In this page you can find the example usage for android.graphics Canvas restore.

Prototype

public void restore() 

Source Link

Document

This call balances a previous call to save(), and is used to remove all modifications to the matrix/clip state since the last save call.

Usage

From source file:com.android.mail.ui.FolderDisplayer.java

public static void drawFolder(Canvas canvas, float x, float y, int width, int height, String name, int fgColor,
        int bgColor, FolderDisplayer.FolderDrawableResources res, BidiFormatter formatter, Paint paint) {
    canvas.save();/*  ww  w  .ja  v  a  2 s.  com*/
    canvas.translate(x, y + res.folderVerticalOffset);

    // Draw the box.
    paint.setColor(bgColor);
    paint.setStyle(Paint.Style.FILL);
    final RectF rect = new RectF(0, 0, width, height);
    canvas.drawRoundRect(rect, res.folderRoundedCornerRadius, res.folderRoundedCornerRadius, paint);

    // Draw the text based on the language locale and layout direction.
    paint.setColor(fgColor);
    paint.setStyle(Paint.Style.FILL);

    // Compute the text/gradient indices
    final int textLength = (int) paint.measureText(name);
    final int gradientX0;
    final int gradientX1;
    final int textX;

    /***************************************************************************************************
     * width               - the actual folder chip rectangle.                                         *
     * textLength          - the length of the folder's full name (can be longer than                  *
     *                         the actual chip, which is what overflow gradient is for).               *
     * innerPadding        - the padding between the text and the chip edge.                           *
     * overflowPadding     - the padding between start of overflow and the chip edge.                  *
     *                                                                                                 *
     *                                                                                                 *
     * text is in a RTL language                                                                       *
     *                                                                                                 *
     *                   index-0                                                                       *
     *                      |<---------------------------- width ---------------------------->|        *
     *        |<-------------------------textLength------------------>|                       |        *
     *        |             |<----- overflowPadding ----->|                                   |        *
     *        |             |<- innerPadding ->|<-------->|<--------->|<- horizontalPadding ->|        *
     *       textX                            gX1        gX0                                           *
     *                                                                                                 *
     *                                                                                                 *
     * text is in a LTR language.                                                                      *
     *                                                                                                 *
     *     index-0                                                                                     *
     *        |<------------------------------ width ------------------------------->|                 *
     *        |                       |<-------------------------textLength-------------------->|      *
     *        |                                   |<-------- overflowPadding ------->|                 *
     *        |<- horizontalPadding ->|<--------->|<-------->|<- horizontalPadding ->|                 *
     *                              textX        gX0        gX1                                        *
     *                                                                                                 *
     **************************************************************************************************/
    if (formatter.isRtl(name)) {
        gradientX0 = res.overflowGradientPadding;
        gradientX1 = res.folderHorizontalPadding;
        textX = width - res.folderHorizontalPadding - textLength;
    } else {
        gradientX0 = width - res.overflowGradientPadding;
        gradientX1 = width - res.folderHorizontalPadding;
        textX = res.folderHorizontalPadding;
    }

    // Draw the text and the possible overflow gradient
    // Overflow happens when the text is longer than the chip width minus side paddings.
    if (textLength > width - 2 * res.folderHorizontalPadding) {
        final Shader shader = new LinearGradient(gradientX0, 0, gradientX1, 0, fgColor,
                Utils.getTransparentColor(fgColor), Shader.TileMode.CLAMP);
        paint.setShader(shader);
    }
    final int textY = height / 2 - (int) (paint.descent() + paint.ascent()) / 2;
    canvas.drawText(name, textX, textY, paint);
    paint.setShader(null);

    canvas.restore();
}

From source file:com.marlonjones.voidlauncher.CellLayout.java

@Override
protected void onDraw(Canvas canvas) {
    if (!mIsDragTarget) {
        return;//from   www.java 2 s  .  c  o m
    }

    // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
    // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
    // When we're small, we are either drawn normally or in the "accepts drops" state (during
    // a drag). However, we also drag the mini hover background *over* one of those two
    // backgrounds
    if (mBackgroundAlpha > 0.0f) {
        mBackground.draw(canvas);
    }

    final Paint paint = mDragOutlinePaint;
    for (int i = 0; i < mDragOutlines.length; i++) {
        final float alpha = mDragOutlineAlphas[i];
        if (alpha > 0) {
            final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
            paint.setAlpha((int) (alpha + .5f));
            canvas.drawBitmap(b, null, mDragOutlines[i], paint);
        }
    }

    if (DEBUG_VISUALIZE_OCCUPIED) {
        int[] pt = new int[2];
        ColorDrawable cd = new ColorDrawable(Color.RED);
        cd.setBounds(0, 0, mCellWidth, mCellHeight);
        for (int i = 0; i < mCountX; i++) {
            for (int j = 0; j < mCountY; j++) {
                if (mOccupied.cells[i][j]) {
                    cellToPoint(i, j, pt);
                    canvas.save();
                    canvas.translate(pt[0], pt[1]);
                    cd.draw(canvas);
                    canvas.restore();
                }
            }
        }
    }

    for (int i = 0; i < mFolderBackgrounds.size(); i++) {
        FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
        cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
        canvas.save();
        canvas.translate(mTempLocation[0], mTempLocation[1]);
        bg.drawBackground(canvas, mFolderBgPaint);
        if (!bg.isClipping) {
            bg.drawBackgroundStroke(canvas, mFolderBgPaint);
        }
        canvas.restore();
    }

    if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
        cellToPoint(mFolderLeaveBehind.delegateCellX, mFolderLeaveBehind.delegateCellY, mTempLocation);
        canvas.save();
        canvas.translate(mTempLocation[0], mTempLocation[1]);
        mFolderLeaveBehind.drawLeaveBehind(canvas, mFolderBgPaint);
        canvas.restore();
    }
}

From source file:org.chromium.chrome.browser.toolbar.ToolbarPhone.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    if (child == mLocationBar)
        return drawLocationBar(canvas, drawingTime);
    boolean clipped = false;

    if (mLocationBarBackground != null
            && ((mTabSwitcherState == STATIC_TAB && !mTabSwitcherModeViews.contains(child))
                    || (mTabSwitcherState != STATIC_TAB && mBrowsingModeViews.contains(child)))) {
        canvas.save();/*from ww w. j a  va2  s  . c  om*/

        int translationY = (int) mLocationBar.getTranslationY();
        int clipTop = mLocationBarBackgroundBounds.top - mLocationBarBackgroundPadding.top + translationY;
        if (mUrlExpansionPercent != 0f && clipTop < child.getBottom()) {
            // For other child views, use the inverse clipping of the URL viewport.
            // Only necessary during animations.
            // Hardware mode does not support unioned clip regions, so clip using the
            // appropriate bounds based on whether the child is to the left or right of the
            // location bar.
            boolean isLeft = (child == mNewTabButton || child == mHomeButton) ^ LocalizationUtils.isLayoutRtl();

            int clipBottom = mLocationBarBackgroundBounds.bottom + mLocationBarBackgroundPadding.bottom
                    + translationY;
            boolean verticalClip = false;
            if (translationY > 0f) {
                clipTop = child.getTop();
                clipBottom = clipTop;
                verticalClip = true;
            }

            if (isLeft) {
                int clipRight = verticalClip ? child.getMeasuredWidth()
                        : mLocationBarBackgroundBounds.left - mLocationBarBackgroundPadding.left;
                canvas.clipRect(0, clipTop, clipRight, clipBottom);
            } else {
                int clipLeft = verticalClip ? 0
                        : mLocationBarBackgroundBounds.right + mLocationBarBackgroundPadding.right;
                canvas.clipRect(clipLeft, clipTop, getMeasuredWidth(), clipBottom);
            }
        }
        clipped = true;
    }
    boolean retVal = super.drawChild(canvas, child, drawingTime);
    if (clipped)
        canvas.restore();
    return retVal;
}

From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java

private void drawEventText(StaticLayout eventLayout, Rect rect, Canvas canvas, int top, int bottom,
        boolean center) {
    // drawEmptyRect(canvas, rect, 0xFFFF00FF); // for debugging

    int width = rect.right - rect.left;
    int height = rect.bottom - rect.top;

    // If the rectangle is too small for text, then return
    if (eventLayout == null || width < MIN_CELL_WIDTH_FOR_TEXT) {
        return;//from   w w  w  . ja v a  2 s .c om
    }

    int totalLineHeight = 0;
    int lineCount = eventLayout.getLineCount();
    for (int i = 0; i < lineCount; i++) {
        int lineBottom = eventLayout.getLineBottom(i);
        if (lineBottom <= height) {
            totalLineHeight = lineBottom;
        } else {
            break;
        }
    }

    // + 2 is small workaround when the font is slightly bigger then the rect. This will
    // still allow the text to be shown without overflowing into the other all day rects.
    if (totalLineHeight == 0 || rect.top > bottom || rect.top + totalLineHeight + 2 < top) {
        return;
    }

    // Use a StaticLayout to format the string.
    canvas.save();
    //  canvas.translate(rect.left, rect.top + (rect.bottom - rect.top / 2));
    int padding = center ? (rect.bottom - rect.top - totalLineHeight) / 2 : 0;
    canvas.translate(rect.left, rect.top + padding);
    rect.left = 0;
    rect.right = width;
    rect.top = 0;
    rect.bottom = totalLineHeight;

    // There's a bug somewhere. If this rect is outside of a previous
    // cliprect, this becomes a no-op. What happens is that the text draw
    // past the event rect. The current fix is to not draw the staticLayout
    // at all if it is completely out of bound.
    canvas.clipRect(rect);
    eventLayout.draw(canvas);
    canvas.restore();
}

From source file:com.codemx.launcher3.CellLayout.java

@Override
protected void onDraw(Canvas canvas) {
    if (!mIsDragTarget) {
        return;//from w  ww .  j a  v a2 s . co  m
    }

    // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
    // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
    // When we're small, we are either drawn normally or in the "accepts drops" state (during
    // a drag). However, we also drag the mini hover background *over* one of those two
    // backgrounds
    if (mBackgroundAlpha > 0.0f) {
        mBackground.draw(canvas);
    }

    final Paint paint = mDragOutlinePaint;
    for (int i = 0; i < mDragOutlines.length; i++) {
        final float alpha = mDragOutlineAlphas[i];
        if (alpha > 0) {
            final Rect r = mDragOutlines[i];
            mTempRect.set(r);
            Utilities.scaleRectAboutCenter(mTempRect, getChildrenScale());
            final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
            paint.setAlpha((int) (alpha + .5f));
            canvas.drawBitmap(b, null, mTempRect, paint);
        }
    }

    if (DEBUG_VISUALIZE_OCCUPIED) {
        int[] pt = new int[2];
        ColorDrawable cd = new ColorDrawable(Color.RED);
        cd.setBounds(0, 0, mCellWidth, mCellHeight);
        for (int i = 0; i < mCountX; i++) {
            for (int j = 0; j < mCountY; j++) {
                if (mOccupied[i][j]) {
                    cellToPoint(i, j, pt);
                    canvas.save();
                    canvas.translate(pt[0], pt[1]);
                    cd.draw(canvas);
                    canvas.restore();
                }
            }
        }
    }

    int previewOffset = FolderIcon.FolderRingAnimator.sPreviewSize;

    // The folder outer / inner ring image(s)
    DeviceProfile grid = mLauncher.getDeviceProfile();
    for (int i = 0; i < mFolderOuterRings.size(); i++) {
        FolderIcon.FolderRingAnimator fra = mFolderOuterRings.get(i);

        Drawable d;
        int width, height;
        cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
        View child = getChildAt(fra.mCellX, fra.mCellY);

        if (child != null) {
            int centerX = mTempLocation[0] + mCellWidth / 2;
            int centerY = mTempLocation[1] + previewOffset / 2 + child.getPaddingTop()
                    + grid.folderBackgroundOffset;

            // Draw outer ring, if it exists
            if (FolderIcon.HAS_OUTER_RING) {
                d = FolderIcon.FolderRingAnimator.sSharedOuterRingDrawable;
                width = (int) (fra.getOuterRingSize() * getChildrenScale());
                height = width;
                canvas.save();
                canvas.translate(centerX - width / 2, centerY - height / 2);
                d.setBounds(0, 0, width, height);
                d.draw(canvas);
                canvas.restore();
            }

            // Draw inner ring
            d = FolderIcon.FolderRingAnimator.sSharedInnerRingDrawable;
            width = (int) (fra.getInnerRingSize() * getChildrenScale());
            height = width;
            canvas.save();
            canvas.translate(centerX - width / 2, centerY - width / 2);
            d.setBounds(0, 0, width, height);
            d.draw(canvas);
            canvas.restore();
        }
    }

    if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
        Drawable d = FolderIcon.sSharedFolderLeaveBehind;
        int width = d.getIntrinsicWidth();
        int height = d.getIntrinsicHeight();

        cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
        View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
        if (child != null) {
            int centerX = mTempLocation[0] + mCellWidth / 2;
            int centerY = mTempLocation[1] + previewOffset / 2 + child.getPaddingTop()
                    + grid.folderBackgroundOffset;

            canvas.save();
            canvas.translate(centerX - width / 2, centerY - width / 2);
            d.setBounds(0, 0, width, height);
            d.draw(canvas);
            canvas.restore();
        }
    }
}

From source file:com.lite.android.launcher3.CellLayout.java

@Override
protected void onDraw(Canvas canvas) {
    if (!mIsDragTarget) {
        return;//from  w  w  w.  ja  v  a 2  s.  co m
    }

    // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
    // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
    // When we're small, we are either drawn normally or in the "accepts drops" state (during
    // a drag). However, we also drag the mini hover background *over* one of those two
    // backgrounds
    if (mBackgroundAlpha > 0.0f) {
        mBackground.draw(canvas);
    }

    final Paint paint = mDragOutlinePaint;
    for (int i = 0; i < mDragOutlines.length; i++) {
        final float alpha = mDragOutlineAlphas[i];
        if (alpha > 0) {
            final Rect r = mDragOutlines[i];
            mTempRect.set(r);
            Utilities.scaleRectAboutCenter(mTempRect, getChildrenScale());
            final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
            paint.setAlpha((int) (alpha + .5f));
            canvas.drawBitmap(b, null, mTempRect, paint);
        }
    }

    if (DEBUG_VISUALIZE_OCCUPIED) {
        int[] pt = new int[2];
        ColorDrawable cd = new ColorDrawable(Color.RED);
        cd.setBounds(0, 0, mCellWidth, mCellHeight);
        for (int i = 0; i < mCountX; i++) {
            for (int j = 0; j < mCountY; j++) {
                if (mOccupied[i][j]) {
                    cellToPoint(i, j, pt);
                    canvas.save();
                    canvas.translate(pt[0], pt[1]);
                    cd.draw(canvas);
                    canvas.restore();
                }
            }
        }
    }

    int previewOffset = FolderRingAnimator.sPreviewSize;

    // The folder outer / inner ring image(s)
    DeviceProfile grid = mLauncher.getDeviceProfile();
    for (int i = 0; i < mFolderOuterRings.size(); i++) {
        FolderRingAnimator fra = mFolderOuterRings.get(i);

        Drawable d;
        int width, height;
        cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
        View child = getChildAt(fra.mCellX, fra.mCellY);

        if (child != null) {
            int centerX = mTempLocation[0] + mCellWidth / 2;
            int centerY = mTempLocation[1] + previewOffset / 2 + child.getPaddingTop()
                    + grid.folderBackgroundOffset;

            // Draw outer ring, if it exists
            if (FolderIcon.HAS_OUTER_RING) {
                d = FolderRingAnimator.sSharedOuterRingDrawable;
                width = (int) (fra.getOuterRingSize() * getChildrenScale());
                height = width;
                canvas.save();
                canvas.translate(centerX - width / 2, centerY - height / 2);
                d.setBounds(0, 0, width, height);
                d.draw(canvas);
                canvas.restore();
            }

            // Draw inner ring
            d = FolderRingAnimator.sSharedInnerRingDrawable;
            if (d != null) {
                width = (int) (fra.getInnerRingSize() * getChildrenScale());
                height = width;
                canvas.save();
                canvas.translate(centerX - width / 2, centerY - width / 2);
                d.setBounds(0, 0, width, height);
                d.draw(canvas);
                canvas.restore();
            }
        }
    }

    if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
        Drawable d = FolderIcon.sSharedFolderLeaveBehind;
        int width = d.getIntrinsicWidth();
        int height = d.getIntrinsicHeight();

        cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
        View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
        if (child != null) {
            int centerX = mTempLocation[0] + mCellWidth / 2;
            int centerY = mTempLocation[1] + previewOffset / 2 + child.getPaddingTop()
                    + grid.folderBackgroundOffset;

            canvas.save();
            canvas.translate(centerX - width / 2, centerY - width / 2);
            d.setBounds(0, 0, width, height);
            d.draw(canvas);
            canvas.restore();
        }
    }
}

From source file:com.android.launcher3.CellLayout.java

@Override
protected void onDraw(Canvas canvas) {
    if (!mIsDragTarget) {
        return;/*  w  w w  .  ja v  a  2 s  .c  o  m*/
    }

    // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
    // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
    // When we're small, we are either drawn normally or in the "accepts drops" state (during
    // a drag). However, we also drag the mini hover background *over* one of those two
    // backgrounds
    if (mBackgroundAlpha > 0.0f) {
        mBackground.draw(canvas);
    }

    final Paint paint = mDragOutlinePaint;
    for (int i = 0; i < mDragOutlines.length; i++) {
        final float alpha = mDragOutlineAlphas[i];
        if (alpha > 0) {
            final Rect r = mDragOutlines[i];
            mTempRect.set(r);
            Utilities.scaleRectAboutCenter(mTempRect, getChildrenScale());
            final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
            paint.setAlpha((int) (alpha + .5f));
            canvas.drawBitmap(b, null, mTempRect, paint);
        }
    }

    if (DEBUG_VISUALIZE_OCCUPIED) {
        int[] pt = new int[2];
        ColorDrawable cd = new ColorDrawable(Color.RED);
        cd.setBounds(0, 0, mCellWidth, mCellHeight);
        for (int i = 0; i < mCountX; i++) {
            for (int j = 0; j < mCountY; j++) {
                if (mOccupied[i][j]) {
                    cellToPoint(i, j, pt);
                    canvas.save();
                    canvas.translate(pt[0], pt[1]);
                    cd.draw(canvas);
                    canvas.restore();
                }
            }
        }
    }

    int previewOffset = FolderRingAnimator.sPreviewSize;

    // The folder outer / inner ring image(s)
    DeviceProfile grid = mLauncher.getDeviceProfile();
    for (int i = 0; i < mFolderOuterRings.size(); i++) {
        FolderRingAnimator fra = mFolderOuterRings.get(i);

        Drawable d;
        int width, height;
        cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
        View child = getChildAt(fra.mCellX, fra.mCellY);

        if (child != null) {
            int centerX = mTempLocation[0] + mCellWidth / 2;
            int centerY = mTempLocation[1] + previewOffset / 2 + child.getPaddingTop()
                    + grid.folderBackgroundOffset;

            // Draw outer ring, if it exists
            if (FolderIcon.HAS_OUTER_RING) {
                d = FolderRingAnimator.sSharedOuterRingDrawable;
                width = (int) (fra.getOuterRingSize() * getChildrenScale());
                height = width;
                canvas.save();
                canvas.translate(centerX - width / 2, centerY - height / 2);
                d.setBounds(0, 0, width, height);
                d.draw(canvas);
                canvas.restore();
            }

            // Draw inner ring
            d = FolderRingAnimator.sSharedInnerRingDrawable;
            width = (int) (fra.getInnerRingSize() * getChildrenScale());
            height = width;
            canvas.save();
            canvas.translate(centerX - width / 2, centerY - width / 2);
            d.setBounds(0, 0, width, height);
            d.draw(canvas);
            canvas.restore();
        }
    }

    if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
        Drawable d = FolderIcon.sSharedFolderLeaveBehind;
        int width = d.getIntrinsicWidth();
        int height = d.getIntrinsicHeight();

        cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
        View child = getChildAt(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1]);
        if (child != null) {
            int centerX = mTempLocation[0] + mCellWidth / 2;
            int centerY = mTempLocation[1] + previewOffset / 2 + child.getPaddingTop()
                    + grid.folderBackgroundOffset;

            canvas.save();
            canvas.translate(centerX - width / 2, centerY - width / 2);
            d.setBounds(0, 0, width, height);
            d.draw(canvas);
            canvas.restore();
        }
    }
}

From source file:org.chromium.chrome.browser.toolbar.ToolbarPhone.java

/**
 * When entering and exiting the TabSwitcher mode, we fade out or fade in the browsing
 * mode of the toolbar on top of the TabSwitcher mode version of it.  We do this by
 * drawing all of the browsing mode views on top of the android view.
 *//*from  w  w  w .j  av a2s. c om*/
private void drawTabSwitcherAnimationOverlay(Canvas canvas, float animationProgress) {
    if (!isNativeLibraryReady())
        return;

    float floatAlpha = 1 - animationProgress;
    int rgbAlpha = (int) (255 * floatAlpha);
    canvas.save();
    canvas.translate(0, -animationProgress * mBackgroundOverlayBounds.height());
    canvas.clipRect(mBackgroundOverlayBounds);

    float previousAlpha = 0.f;
    if (mHomeButton.getVisibility() != View.GONE) {
        // Draw the New Tab button used in the URL view.
        previousAlpha = mHomeButton.getAlpha();
        mHomeButton.setAlpha(previousAlpha * floatAlpha);
        drawChild(canvas, mHomeButton, SystemClock.uptimeMillis());
        mHomeButton.setAlpha(previousAlpha);
    }

    // Draw the location/URL bar.
    previousAlpha = mLocationBar.getAlpha();
    mLocationBar.setAlpha(previousAlpha * floatAlpha);
    // If the location bar is now fully transparent, do not bother drawing it.
    if (mLocationBar.getAlpha() != 0) {
        drawChild(canvas, mLocationBar, SystemClock.uptimeMillis());
    }
    mLocationBar.setAlpha(previousAlpha);

    // Draw the tab stack button and associated text.
    translateCanvasToView(this, mToolbarButtonsContainer, canvas);

    if (mTabSwitcherAnimationTabStackDrawable != null && mToggleTabStackButton != null
            && mUrlExpansionPercent != 1f) {
        // Draw the tab stack button image.
        canvas.save();
        translateCanvasToView(mToolbarButtonsContainer, mToggleTabStackButton, canvas);

        int backgroundWidth = mToggleTabStackButton.getDrawable().getIntrinsicWidth();
        int backgroundHeight = mToggleTabStackButton.getDrawable().getIntrinsicHeight();
        int backgroundLeft = (mToggleTabStackButton.getWidth() - mToggleTabStackButton.getPaddingLeft()
                - mToggleTabStackButton.getPaddingRight() - backgroundWidth) / 2;
        backgroundLeft += mToggleTabStackButton.getPaddingLeft();
        int backgroundTop = (mToggleTabStackButton.getHeight() - mToggleTabStackButton.getPaddingTop()
                - mToggleTabStackButton.getPaddingBottom() - backgroundHeight) / 2;
        backgroundTop += mToggleTabStackButton.getPaddingTop();
        canvas.translate(backgroundLeft, backgroundTop);

        mTabSwitcherAnimationTabStackDrawable.setAlpha(rgbAlpha);
        mTabSwitcherAnimationTabStackDrawable.draw(canvas);
        canvas.restore();
    }

    // Draw the menu button if necessary.
    if (!mShowMenuBadge && mTabSwitcherAnimationMenuDrawable != null && mUrlExpansionPercent != 1f) {
        mTabSwitcherAnimationMenuDrawable.setBounds(mMenuButton.getPaddingLeft(), mMenuButton.getPaddingTop(),
                mMenuButton.getWidth() - mMenuButton.getPaddingRight(),
                mMenuButton.getHeight() - mMenuButton.getPaddingBottom());
        translateCanvasToView(mToolbarButtonsContainer, mMenuButton, canvas);
        mTabSwitcherAnimationMenuDrawable.setAlpha(rgbAlpha);
        int color = mUseLightDrawablesForTextureCapture ? mLightModeDefaultColor : mDarkModeDefaultColor;
        mTabSwitcherAnimationMenuDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        mTabSwitcherAnimationMenuDrawable.draw(canvas);
    }

    // Draw the menu badge if necessary.
    Drawable badgeDrawable = mUseLightDrawablesForTextureCapture ? mTabSwitcherAnimationMenuBadgeLightDrawable
            : mTabSwitcherAnimationMenuBadgeDarkDrawable;
    if (mShowMenuBadge && badgeDrawable != null && mUrlExpansionPercent != 1f) {
        badgeDrawable.setBounds(mMenuBadge.getPaddingLeft(), mMenuBadge.getPaddingTop(),
                mMenuBadge.getWidth() - mMenuBadge.getPaddingRight(),
                mMenuBadge.getHeight() - mMenuBadge.getPaddingBottom());
        translateCanvasToView(mToolbarButtonsContainer, mMenuBadge, canvas);
        badgeDrawable.setAlpha(rgbAlpha);
        badgeDrawable.draw(canvas);
    }

    mLightDrawablesUsedForLastTextureCapture = mUseLightDrawablesForTextureCapture;

    canvas.restore();
}

From source file:com.pdftron.pdf.tools.Tool.java

public void onDraw(Canvas canvas, android.graphics.Matrix tfm) {
    // Draw page number
    if (mShowPageNum && mPageNumberIndicatorVisible) {
        int width = mPDFView.getWidth();
        int height = mPDFView.getHeight();
        int page_num = mPDFView.getCurrentPage();
        boolean restore = false;
        float yOffset = 0;

        try {/*from   ww  w.j a  v  a 2s  . c  o  m*/
            // During page sliding, PDFViewCtrl might apply extra transformation
            // matrix to Canvas for animation. However, page number should not
            // move; hence applying the inverse to offset it.
            if (!tfm.isIdentity()) {
                canvas.save();
                restore = true;
                tfm.invert(mTempMtx1);
                canvas.getMatrix(mTempMtx2);
                mTempMtx2.postConcat(mTempMtx1);
                canvas.setMatrix(mTempMtx2);

                // Workaround for bug found in Android > ICS with hardware acceleration turned
                // ON. See http://code.google.com/p/android/issues/detail?id=24517 for more info
                if (Build.VERSION.SDK_INT >= 14
                        /*Build.VERSION_CODES.ICE_CREAM_SANDWICH*/ && mPDFView.isHardwareAccelerated()) {
                    Rect rectangle = new Rect();
                    ((android.app.Activity) mPDFView.getContext()).getWindow().getDecorView()
                            .getWindowVisibleDisplayFrame(rectangle);
                    yOffset = rectangle.top;
                }
            }

            int page_count = mPDFView.getDoc().getPageCount();
            String str = String.format(getStringFromResId(R.string.tools_misc_pagerange), page_num, page_count);

            Rect r = new Rect();
            mPaint4PageNum.getTextBounds(str, 0, str.length(), r);
            float str_width = r.width();
            float str_height = r.height();

            float margin = str_height / 1.5f;
            float left = width - str_width * 1.5f - margin + mPDFView.getScrollX();

            float top = mPDFView.getScrollY() + height - mPageNumPosAdjust - str_height * 3.0f + yOffset;

            float right = left + str_width + margin * 2;
            float bottom = top + str_height + margin * 2;

            mTempPageDrawingRectF.set(left, top, right, bottom);
            mPaint4PageNum.setColor(
                    mPDFView.getContext().getResources().getColor(R.color.tools_pageindicator_background));
            canvas.drawRoundRect(mTempPageDrawingRectF, margin, margin, mPaint4PageNum);

            mPaint4PageNum
                    .setColor(mPDFView.getContext().getResources().getColor(R.color.tools_pageindicator_text));
            left += margin;
            top += str_height / 2 + margin + mPaint4PageNum.descent();

            canvas.drawText(str, left, top - 0.5f, mPaint4PageNum);

        } catch (Exception e) {

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

From source file:com.jjoe64.graphview.GridLabelRenderer.java

/**
 * draws the horizontal steps/*w ww.  java  2  s  .  c  om*/
 * vertical lines and horizontal labels
 *
 * @param canvas canvas
 */
protected void drawHorizontalSteps(Canvas canvas) {
    // draw horizontal steps (vertical lines and horizontal labels)
    mPaintLabel.setColor(getHorizontalLabelsColor());
    int i = 0;
    for (Map.Entry<Integer, Double> e : mStepsHorizontal.entrySet()) {
        // draw line
        if (mStyles.highlightZeroLines) {
            if (e.getValue() == 0d) {
                mPaintLine.setStrokeWidth(5);
            } else {
                mPaintLine.setStrokeWidth(0);
            }
        }
        if (mStyles.gridStyle.drawVertical()) {
            canvas.drawLine(mGraphView.getGraphContentLeft() + e.getKey(), mGraphView.getGraphContentTop(),
                    mGraphView.getGraphContentLeft() + e.getKey(),
                    mGraphView.getGraphContentTop() + mGraphView.getGraphContentHeight(), mPaintLine);
        }

        // draw label
        if (isHorizontalLabelsVisible()) {
            if (mStyles.horizontalLabelsAngle > 0f && mStyles.horizontalLabelsAngle <= 180f) {
                if (mStyles.horizontalLabelsAngle < 90f) {
                    mPaintLabel.setTextAlign((Paint.Align.RIGHT));
                } else if (mStyles.horizontalLabelsAngle <= 180f) {
                    mPaintLabel.setTextAlign((Paint.Align.LEFT));
                }
            } else {
                mPaintLabel.setTextAlign(Paint.Align.CENTER);
                if (i == mStepsHorizontal.size() - 1)
                    mPaintLabel.setTextAlign(Paint.Align.RIGHT);
                if (i == 0)
                    mPaintLabel.setTextAlign(Paint.Align.LEFT);
            }

            // multiline labels
            String label = mLabelFormatter.formatLabel(e.getValue(), true);
            if (label == null) {
                label = "";
            }
            String[] lines = label.split("\n");

            // If labels are angled, calculate adjustment to line them up with the grid
            int labelWidthAdj = 0;
            if (mStyles.horizontalLabelsAngle > 0f && mStyles.horizontalLabelsAngle <= 180f) {
                Rect textBounds = new Rect();
                mPaintLabel.getTextBounds(lines[0], 0, lines[0].length(), textBounds);
                labelWidthAdj = (int) Math
                        .abs(textBounds.width() * Math.cos(Math.toRadians(mStyles.horizontalLabelsAngle)));
            }
            for (int li = 0; li < lines.length; li++) {
                // for the last line y = height
                float y = (canvas.getHeight() - mStyles.padding - getHorizontalAxisTitleHeight())
                        - (lines.length - li - 1) * getTextSize() * 1.1f + mStyles.labelsSpace;
                float x = mGraphView.getGraphContentLeft() + e.getKey();
                if (mStyles.horizontalLabelsAngle > 0 && mStyles.horizontalLabelsAngle < 90f) {
                    canvas.save();
                    canvas.rotate(mStyles.horizontalLabelsAngle, x + labelWidthAdj, y);
                    canvas.drawText(lines[li], x + labelWidthAdj, y, mPaintLabel);
                    canvas.restore();
                } else if (mStyles.horizontalLabelsAngle > 0 && mStyles.horizontalLabelsAngle <= 180f) {
                    canvas.save();
                    canvas.rotate(mStyles.horizontalLabelsAngle - 180f, x - labelWidthAdj, y);
                    canvas.drawText(lines[li], x - labelWidthAdj, y, mPaintLabel);
                    canvas.restore();
                } else {
                    canvas.drawText(lines[li], x, y, mPaintLabel);
                }
            }
        }
        i++;
    }
}