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:android.support.v7.widget.SwitchCompat.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 av  a  2s .  co  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 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:am.widget.indicatortabstrip.IndicatorTabStrip.java

/**
 * ??/*from w  w  w.j a  va  2 s  .  com*/
 *
 * @param canvas     
 * @param position   ???
 * @param itemWidth  ?
 * @param itemHeight ?
 */
protected void drawItemGradient(Canvas canvas, int position, int itemWidth, int itemHeight) {
    if (mGradient == null || !mGradient.isStateful())
        return;
    final int normalColor = mGradient.getDefaultColor();
    final int selectedColor = mGradient.getColorForState(SELECTED_STATE_SET, normalColor);
    if (position == mNextPager) {
        mTextPaint.setColor(getColor(normalColor, selectedColor, mOffset));
    } else if (position == mCurrentPager) {
        mTextPaint.setColor(getColor(normalColor, selectedColor, 1 - mOffset));
    } else {
        mTextPaint.setColor(normalColor);
    }
    final float moveX = ViewCompat.getPaddingStart(this) + (itemWidth + getIntervalWidth()) * position;
    final float moveY = getPaddingTop();
    final int restWidth = position == getItemCount() - 1 ? getLastItemWidth() : itemWidth;
    canvas.save();
    canvas.translate(moveX, moveY);
    canvas.drawRect(0, 0, restWidth, itemHeight, mTextPaint);
    canvas.restore();
}

From source file:com.example.verticaldrawerlayout.VerticalDrawerLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final int height = getHeight();
    final boolean drawingContent = isContentView(child);
    int clipLeft = 0, clipRight = getWidth();

    final int restoreCount = canvas.save();
    if (drawingContent) {
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View v = getChildAt(i);
            if (v == child || v.getVisibility() != VISIBLE || !hasOpaqueBackground(v) || !isDrawerView(v)
                    || v.getHeight() < height) {
                continue;
            }// ww  w . j ava 2s . c om

            if (checkDrawerViewAbsoluteGravity(v, Gravity.TOP)) {
                final int vright = v.getRight();
                if (vright > clipLeft)
                    clipLeft = vright;
            } else {
                final int vleft = v.getLeft();
                if (vleft < clipRight)
                    clipRight = vleft;
            }
        }
        canvas.clipRect(clipLeft, 0, clipRight, getHeight());
    }
    final boolean result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(restoreCount);

    if (mScrimOpacity > 0 && drawingContent) {
        final int baseAlpha = (mScrimColor & 0xff000000) >>> 24;
        final int imag = (int) (baseAlpha * mScrimOpacity);
        final int color = imag << 24 | (mScrimColor & 0xffffff);
        mScrimPaint.setColor(color);

        canvas.drawRect(clipLeft, 0, clipRight, getHeight(), mScrimPaint);
    } else if (mShadowTop != null && checkDrawerViewAbsoluteGravity(child, Gravity.TOP)) {
        final int shadowWidth = mShadowTop.getIntrinsicWidth();
        final int childRight = child.getRight();
        final int drawerPeekDistance = mTopDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) childRight / drawerPeekDistance, 1.f));
        mShadowTop.setBounds(childRight, child.getTop(), childRight + shadowWidth, child.getBottom());
        mShadowTop.setAlpha((int) (0xff * alpha));
        mShadowTop.draw(canvas);
    } else if (mShadowBottom != null && checkDrawerViewAbsoluteGravity(child, Gravity.BOTTOM)) {
        final int shadowWidth = mShadowBottom.getIntrinsicWidth();
        final int childLeft = child.getLeft();
        final int showing = getWidth() - childLeft;
        final int drawerPeekDistance = mBottomDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) showing / drawerPeekDistance, 1.f));
        mShadowBottom.setBounds(childLeft - shadowWidth, child.getTop(), childLeft, child.getBottom());
        mShadowBottom.setAlpha((int) (0xff * alpha));
        mShadowBottom.draw(canvas);
    }
    return result;
}

From source file:com.apptentive.android.sdk.view.ApptentiveNestedScrollView.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    if (mEdgeGlowTop != null) {
        final int scrollY = getScrollY();
        if (!mEdgeGlowTop.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();

            canvas.translate(getPaddingLeft(), Math.min(0, scrollY));
            mEdgeGlowTop.setSize(width, getHeight());
            if (mEdgeGlowTop.draw(canvas)) {
                ViewCompat.postInvalidateOnAnimation(this);
            }/*www .  j a  v  a2  s .c  o m*/
            canvas.restoreToCount(restoreCount);
        }
        if (!mEdgeGlowBottom.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();
            final int height = getHeight();

            canvas.translate(-width + getPaddingLeft(), Math.max(getScrollRange(), scrollY) + height);
            canvas.rotate(180, width, 0);
            mEdgeGlowBottom.setSize(width, height);
            if (mEdgeGlowBottom.draw(canvas)) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
            canvas.restoreToCount(restoreCount);
        }
    }
}

From source file:com.sanron.sunweather.view.v4.DrawerLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final int height = getHeight();
    final boolean drawingContent = isContentView(child);
    int clipLeft = 0, clipRight = getWidth();

    final int restoreCount = canvas.save();
    if (drawingContent) {
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View v = getChildAt(i);
            if (v == child || v.getVisibility() != VISIBLE || !hasOpaqueBackground(v) || !isDrawerView(v)
                    || v.getHeight() < height) {
                continue;
            }//  w ww.j a  va  2s. c  o  m

            if (checkDrawerViewAbsoluteGravity(v, Gravity.LEFT)) {
                final int vright = v.getRight();
                if (vright > clipLeft)
                    clipLeft = vright;
            } else {
                final int vleft = v.getLeft();
                if (vleft < clipRight)
                    clipRight = vleft;
            }
        }
        canvas.clipRect(clipLeft, 0, clipRight, getHeight());
    }
    final boolean result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(restoreCount);

    if (mShadowLeft != null && checkDrawerViewAbsoluteGravity(child, Gravity.LEFT)) {
        final int shadowWidth = mShadowLeft.getIntrinsicWidth();
        final int childRight = child.getRight();
        mShadowLeft.setBounds(childRight, child.getTop(), childRight + shadowWidth, child.getBottom());
        mShadowLeft.draw(canvas);
    } else if (mShadowRight != null && checkDrawerViewAbsoluteGravity(child, Gravity.RIGHT)) {
        final int shadowWidth = mShadowRight.getIntrinsicWidth();
        final int childLeft = child.getLeft();
        mShadowRight.setBounds(childLeft - shadowWidth, child.getTop(), childLeft, child.getBottom());
        mShadowRight.draw(canvas);
    }
    return result;
}

From source file:cnedu.ustcjd.widget.MultiSlider.java

@Override
protected synchronized void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // --> draw track
    if (mTrack != null) {
        // Translate canvas so a indeterminate circular progress bar with padding
        // rotates properly in its animation
        canvas.save();
        if (isLayoutRtl() && mMirrorForRtl) {
            canvas.translate(getWidth() - getPaddingRight(), getPaddingTop());
            canvas.scale(-1.0f, 1.0f);/*  w  ww.j ava  2  s . co  m*/
        } else {
            canvas.translate(getPaddingLeft(), getPaddingTop());
        }
        mTrack.draw(canvas);
        canvas.restore();
    }

    // --> draw ranges

    for (Thumb thumb : mThumbs) {
        if (thumb.getRange() != null) {
            canvas.save();
            if (isLayoutRtl() && mMirrorForRtl) {
                canvas.translate(getWidth() - getPaddingRight(), getPaddingTop());
                canvas.scale(-1.0f, 1.0f);
            } else {
                canvas.translate(getPaddingLeft(), getPaddingTop());
            }
            thumb.getRange().draw(canvas);

            canvas.restore();
        }
    }

    // --> then draw thumbs
    for (Thumb thumb : mThumbs) {
        if (thumb.getThumb() != null && !thumb.isInvisibleThumb()) {
            canvas.save();
            // Translate the padding. For the x, we need to allow the thumb to
            // draw in its extra space
            canvas.translate(getPaddingLeft() - thumb.getThumbOffset(), getPaddingTop());
            // float scale = mScaleMax > 0 ? (float) thumb.getValue() / (float) mScaleMax : 0;
            thumb.getThumb().draw(canvas);

            canvas.restore();
        }
    }
}

From source file:org.telegram.ui.Cells.ProfileSearchCell.java

@Override
protected void onDraw(Canvas canvas) {
    if (user == null && chat == null && encryptedChat == null) {
        return;/*from  w  w  w  .j  av  a2  s.  c  o  m*/
    }

    if (useSeparator) {
        if (LocaleController.isRTL) {
            canvas.drawLine(0, getMeasuredHeight() - 1,
                    getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.leftBaseline),
                    getMeasuredHeight() - 1, linePaint);
        } else {
            canvas.drawLine(AndroidUtilities.dp(AndroidUtilities.leftBaseline), getMeasuredHeight() - 1,
                    getMeasuredWidth(), getMeasuredHeight() - 1, linePaint);
        }
    }

    if (drawAlpha != 1) {
        canvas.saveLayerAlpha(0, 0, canvas.getWidth(), canvas.getHeight(), (int) (255 * drawAlpha),
                Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
    }

    if (drawNameLock) {
        setDrawableBounds(lockDrawable, nameLockLeft, nameLockTop);
        lockDrawable.draw(canvas);
    } else if (drawNameGroup) {
        setDrawableBounds(groupDrawable, nameLockLeft, nameLockTop);
        groupDrawable.draw(canvas);
    } else if (drawNameBroadcast) {
        setDrawableBounds(broadcastDrawable, nameLockLeft, nameLockTop);
        broadcastDrawable.draw(canvas);
    } else if (drawNameBot) {
        setDrawableBounds(botDrawable, nameLockLeft, nameLockTop);
        botDrawable.draw(canvas);
    }

    if (nameLayout != null) {
        canvas.save();
        canvas.translate(nameLeft, nameTop);
        nameLayout.draw(canvas);
        canvas.restore();
        if (drawCheck) {
            if (LocaleController.isRTL) {
                setDrawableBounds(checkDrawable,
                        nameLeft - AndroidUtilities.dp(4) - checkDrawable.getIntrinsicWidth(), nameLockTop);
            } else {
                setDrawableBounds(checkDrawable,
                        nameLeft + (int) nameLayout.getLineWidth(0) + AndroidUtilities.dp(4), nameLockTop);
            }
            checkDrawable.draw(canvas);
        }
    }

    if (onlineLayout != null) {
        canvas.save();
        canvas.translate(onlineLeft, AndroidUtilities.dp(40));
        onlineLayout.draw(canvas);
        canvas.restore();
    }

    if (countLayout != null) {
        if (MessagesController.getInstance().isDialogMuted(dialog_id)) {
            setDrawableBounds(countDrawableGrey, countLeft - AndroidUtilities.dp(5.5f), countTop,
                    countWidth + AndroidUtilities.dp(11), countDrawableGrey.getIntrinsicHeight());
            countDrawableGrey.draw(canvas);
        } else {
            setDrawableBounds(countDrawable, countLeft - AndroidUtilities.dp(5.5f), countTop,
                    countWidth + AndroidUtilities.dp(11), countDrawable.getIntrinsicHeight());
            countDrawable.draw(canvas);
        }
        canvas.save();
        canvas.translate(countLeft, countTop + AndroidUtilities.dp(4));
        countLayout.draw(canvas);
        canvas.restore();
    }

    avatarImage.draw(canvas);
}

From source file:net.simonvt.staggeredgridview.StaggeredGridView.java

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

    if (topEdge != null) {
        boolean needsInvalidate = false;
        if (!topEdge.isFinished()) {
            topEdge.draw(canvas);/*  w  w  w  .  j  a va2  s .co  m*/
            needsInvalidate = true;
        }
        if (!bottomEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            canvas.translate(-width, getHeight());
            canvas.rotate(180, width, 0);
            bottomEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
            needsInvalidate = true;
        }

        if (needsInvalidate) {
            postInvalidateOnAnimation();
        }
    }

    if (!selectorRect.isEmpty()) {
        selector.setBounds(selectorRect);
        selector.draw(canvas);
    }
}

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

@Override
protected void onDraw(Canvas canvas) {
    if (mCoordinates == null) {
        LogUtils.e(LOG_TAG, "null coordinates in ConversationItemView#onDraw");
        return;/*ww w  .  ja v  a2  s.c  o m*/
    }

    Utils.traceBeginSection("CIVC.draw");

    // Contact photo
    if (mGadgetMode == ConversationItemViewCoordinates.GADGET_CONTACT_PHOTO) {
        canvas.save();
        Utils.traceBeginSection("draw senders image");
        drawSendersImage(canvas);
        Utils.traceEndSection();
        canvas.restore();
    }

    // Senders.
    boolean isUnread = mHeader.unread;
    // Old style senders; apply text colors/ sizes/ styling.
    canvas.save();
    if (mHeader.sendersDisplayLayout != null) {
        sPaint.setTextSize(mCoordinates.sendersFontSize);
        sPaint.setTypeface(SendersView.getTypeface(isUnread));
        sPaint.setColor(sSendersTextColor);
        canvas.translate(mSendersX, mCoordinates.sendersY + mHeader.sendersDisplayLayout.getTopPadding());
        mHeader.sendersDisplayLayout.draw(canvas);
    } else {
        drawSenders(canvas);
    }
    canvas.restore();

    // Subject.
    sPaint.setTypeface(Typeface.DEFAULT);
    canvas.save();
    drawSubject(canvas);
    canvas.restore();

    canvas.save();
    drawSnippet(canvas);
    canvas.restore();

    // Folders.
    if (mConfig.areFoldersVisible()) {
        mHeader.folderDisplayer.drawFolders(canvas, mCoordinates, ViewUtils.isViewRtl(this));
    }

    // If this folder has a color (combined view/Email), show it here
    if (mConfig.isColorBlockVisible()) {
        sFoldersPaint.setColor(mHeader.conversation.color);
        sFoldersPaint.setStyle(Paint.Style.FILL);
        canvas.drawRect(mCoordinates.colorBlockX, mCoordinates.colorBlockY,
                mCoordinates.colorBlockX + mCoordinates.colorBlockWidth,
                mCoordinates.colorBlockY + mCoordinates.colorBlockHeight, sFoldersPaint);
    }

    // Draw the reply state. Draw nothing if neither replied nor forwarded.
    if (mConfig.isReplyStateVisible()) {
        if (mHeader.hasBeenRepliedTo && mHeader.hasBeenForwarded) {
            canvas.drawBitmap(STATE_REPLIED_AND_FORWARDED, mCoordinates.replyStateX, mCoordinates.replyStateY,
                    null);
        } else if (mHeader.hasBeenRepliedTo) {
            canvas.drawBitmap(STATE_REPLIED, mCoordinates.replyStateX, mCoordinates.replyStateY, null);
        } else if (mHeader.hasBeenForwarded) {
            canvas.drawBitmap(STATE_FORWARDED, mCoordinates.replyStateX, mCoordinates.replyStateY, null);
        } else if (mHeader.isInvite) {
            canvas.drawBitmap(STATE_CALENDAR_INVITE, mCoordinates.replyStateX, mCoordinates.replyStateY, null);
        }
    }

    if (mConfig.isPersonalIndicatorVisible()) {
        canvas.drawBitmap(mHeader.personalLevelBitmap, mCoordinates.personalIndicatorX,
                mCoordinates.personalIndicatorY, null);
    }

    // Info icon
    if (mHeader.infoIcon != null) {
        canvas.drawBitmap(mHeader.infoIcon, mInfoIconX, mCoordinates.infoIconY, sPaint);
    }

    // Date.
    sPaint.setTextSize(mCoordinates.dateFontSize);
    sPaint.setTypeface(isUnread ? SANS_SERIF_BOLD : SANS_SERIF_LIGHT);
    sPaint.setColor(isUnread ? sDateTextColorUnread : sDateTextColorRead);
    drawText(canvas, mHeader.dateText, mDateX, mCoordinates.dateYBaseline, sPaint);

    // Paper clip icon.
    if (mHeader.paperclip != null) {
        canvas.drawBitmap(mHeader.paperclip, mPaperclipX, mCoordinates.paperclipY, sPaint);
    }

    // Star.
    if (mStarEnabled) {
        canvas.drawBitmap(getStarBitmap(), mCoordinates.starX, mCoordinates.starY, sPaint);
    }

    // Divider.
    if (mDividerEnabled) {
        final int dividerBottomY = getHeight();
        final int dividerTopY = dividerBottomY - sDividerHeight;
        canvas.drawRect(0, dividerTopY, getWidth(), dividerBottomY, sDividerPaint);
    }

    // The focused bar
    final SwipeableListView listView = getListView();
    if (listView != null && listView.isConversationSelected(getConversation())) {
        final int w = FOCUSED_CONVERSATION_HIGHLIGHT.getIntrinsicWidth();
        final boolean isRtl = ViewUtils.isViewRtl(this);
        // This bar is on the right side of the conv list if it's RTL
        FOCUSED_CONVERSATION_HIGHLIGHT.setBounds((isRtl) ? getWidth() - w : 0, 0, (isRtl) ? getWidth() : w,
                getHeight());
        FOCUSED_CONVERSATION_HIGHLIGHT.draw(canvas);
    }

    Utils.traceEndSection();
}

From source file:ca.co.rufus.androidboilerplate.ui.DebugDrawerLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final int height = getHeight();
    final boolean drawingContent = isContentView(child);
    int clipLeft = 0, clipRight = getWidth();

    final int restoreCount = canvas.save();
    if (drawingContent) {
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View v = getChildAt(i);
            if (v == child || v.getVisibility() != VISIBLE || !hasOpaqueBackground(v) || !isDrawerView(v)
                    || v.getHeight() < height) {
                continue;
            }// ww w  .j  av a 2 s. c om

            if (checkDrawerViewAbsoluteGravity(v, Gravity.LEFT)) {
                final int vright = v.getRight();
                if (vright > clipLeft)
                    clipLeft = vright;
            } else {
                final int vleft = v.getLeft();
                if (vleft < clipRight)
                    clipRight = vleft;
            }
        }
        canvas.clipRect(clipLeft, 0, clipRight, getHeight());
    }
    final boolean result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(restoreCount);

    if (mScrimOpacity > 0 && drawingContent) {
        final int baseAlpha = (mScrimColor & 0xff000000) >>> 24;
        final int imag = (int) (baseAlpha * mScrimOpacity);
        final int color = imag << 24 | (mScrimColor & 0xffffff);
        mScrimPaint.setColor(color);

        canvas.drawRect(clipLeft, 0, clipRight, getHeight(), mScrimPaint);
    } else if (mShadowLeft != null && checkDrawerViewAbsoluteGravity(child, Gravity.LEFT)) {
        final int shadowWidth = mShadowLeft.getIntrinsicWidth();
        final int childRight = child.getRight();
        final int drawerPeekDistance = mLeftDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) childRight / drawerPeekDistance, 1.f));
        mShadowLeft.setBounds(childRight, child.getTop(), childRight + shadowWidth, child.getBottom());
        mShadowLeft.setAlpha((int) (0xff * alpha));
        mShadowLeft.draw(canvas);
    } else if (mShadowRight != null && checkDrawerViewAbsoluteGravity(child, Gravity.RIGHT)) {
        final int shadowWidth = mShadowRight.getIntrinsicWidth();
        final int childLeft = child.getLeft();
        final int showing = getWidth() - childLeft;
        final int drawerPeekDistance = mRightDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) showing / drawerPeekDistance, 1.f));
        mShadowRight.setBounds(childLeft - shadowWidth, child.getTop(), childLeft, child.getBottom());
        mShadowRight.setAlpha((int) (0xff * alpha));
        mShadowRight.draw(canvas);
    }
    return result;
}