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.sanjie.zy.adpter.decoration.StickyHeaderDecoration.java

/**
 * {@inheritDoc}//from ww  w  .  j  av a 2  s .c om
 */
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    if (parent.getAdapter() == null) {
        return;
    }

    final int count = parent.getChildCount();
    long previousHeaderId = -1;

    for (int layoutPos = 0; layoutPos < count; layoutPos++) {
        final View child = parent.getChildAt(layoutPos);
        int adapterPos = parent.getChildAdapterPosition(child);

        if (!mIncludeHeader) {
            if (parent.getAdapter() instanceof ZYRecyclerViewAdapter) {
                int headerCount = ((ZYRecyclerViewAdapter) parent.getAdapter()).getHeaderCount();
                int footerCount = ((ZYRecyclerViewAdapter) parent.getAdapter()).getFooterCount();
                int dataCount = ((ZYRecyclerViewAdapter) parent.getAdapter()).getDataCount();
                if (adapterPos < headerCount) {
                    continue;
                }
                if (adapterPos >= headerCount + dataCount) {
                    continue;
                }
                if (adapterPos >= headerCount) {
                    adapterPos -= headerCount;
                }

            }
        }

        if (adapterPos != RecyclerView.NO_POSITION && hasHeader(adapterPos)) {
            long headerId = mAdapter.getHeaderId(adapterPos);

            if (headerId != previousHeaderId) {
                previousHeaderId = headerId;
                View header = getHeader(parent, adapterPos).itemView;
                canvas.save();

                final int left = child.getLeft();
                final int top = getHeaderTop(parent, child, header, adapterPos, layoutPos);
                canvas.translate(left, top);

                header.setTranslationX(left);
                header.setTranslationY(top);
                header.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 ww  w .  ja  va 2  s.com*/
    }

    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:com.android.launcher3.Utilities.java

/**
 * @param scale the scale to apply before drawing {@param icon} on the canvas
 *//*ww  w . j av  a2s  .  com*/
public static Bitmap createIconBitmap(Drawable icon, Context context, float scale) {
    synchronized (sCanvas) {
        final int iconBitmapSize = getIconBitmapSize();

        int width = iconBitmapSize;
        int height = iconBitmapSize;

        if (icon instanceof PaintDrawable) {
            PaintDrawable painter = (PaintDrawable) icon;
            painter.setIntrinsicWidth(width);
            painter.setIntrinsicHeight(height);
        } else if (icon instanceof BitmapDrawable) {
            // Ensure the bitmap has a density.
            BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
            Bitmap bitmap = bitmapDrawable.getBitmap();
            if (bitmap != null && bitmap.getDensity() == Bitmap.DENSITY_NONE) {
                bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
            }
        }
        int sourceWidth = icon.getIntrinsicWidth();
        int sourceHeight = icon.getIntrinsicHeight();
        if (sourceWidth > 0 && sourceHeight > 0) {
            // Scale the icon proportionally to the icon dimensions
            final float ratio = (float) sourceWidth / sourceHeight;
            if (sourceWidth > sourceHeight) {
                height = (int) (width / ratio);
            } else if (sourceHeight > sourceWidth) {
                width = (int) (height * ratio);
            }
        }

        // no intrinsic size --> use default size
        int textureWidth = iconBitmapSize;
        int textureHeight = iconBitmapSize;

        final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, Bitmap.Config.ARGB_8888);
        final Canvas canvas = sCanvas;
        canvas.setBitmap(bitmap);

        final int left = (textureWidth - width) / 2;
        final int top = (textureHeight - height) / 2;

        @SuppressWarnings("all") // suppress dead code warning
        final boolean debug = false;
        if (debug) {
            // draw a big box for the icon for debugging
            canvas.drawColor(sColors[sColorIndex]);
            if (++sColorIndex >= sColors.length)
                sColorIndex = 0;
            Paint debugPaint = new Paint();
            debugPaint.setColor(0xffcccc00);
            canvas.drawRect(left, top, left + width, top + height, debugPaint);
        }

        sOldBounds.set(icon.getBounds());
        icon.setBounds(left, top, left + width, top + height);
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.scale(scale, scale, textureWidth / 2, textureHeight / 2);
        icon.draw(canvas);
        canvas.restore();
        icon.setBounds(sOldBounds);
        canvas.setBitmap(null);

        return bitmap;
    }
}

From source file:am.widget.indicatortabstrip.IndicatorTabStrip.java

/**
 * //from   w  ww .  java  2 s .c  o m
 *
 * @param canvas 
 */
protected void drawIndicator(Canvas canvas) {
    if (mIndicator == null)
        return;
    final int indicatorWidth = getIndicatorWidth();
    final int indicatorHeight = getIndicatorHeight();
    if (indicatorWidth <= 0 || indicatorHeight <= 0)
        return;
    mIndicator.setBounds(0, 0, indicatorWidth, indicatorHeight);
    final float widthWithInterval = getItemWidth() + getIntervalWidth();
    final float currentCenter = ViewCompat.getPaddingStart(this) + mCurrentPager * widthWithInterval
            + widthWithInterval * 0.5f;
    final float nextCenter = ViewCompat.getPaddingStart(this) + mNextPager * widthWithInterval
            + widthWithInterval * 0.5f;
    final float moveCenter = currentCenter + (nextCenter - currentCenter) * mOffset;
    final float moveX = moveCenter - indicatorWidth * 0.5f;
    final float moveY = getHeight() - getPaddingBottom() - getDividerHeight() - indicatorHeight;
    canvas.save();
    canvas.translate(moveX, moveY);
    mIndicator.draw(canvas);
    canvas.restore();
}

From source file:com.actionbarsherlock.internal.widget.IcsProgressBar.java

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

    Drawable d = mCurrentDrawable;/*  ww  w.  j  a v  a  2 s  .  c  om*/
    if (d != null) {
        // Translate canvas so a indeterminate circular progress bar with padding
        // rotates properly in its animation
        canvas.save();
        canvas.translate(getPaddingLeft() + mIndeterminateRealLeft, getPaddingTop() + mIndeterminateRealTop);
        long time = getDrawingTime();
        if (mAnimation != null) {
            mAnimation.getTransformation(time, mTransformation);
            float scale = mTransformation.getAlpha();
            try {
                mInDrawing = true;
                d.setLevel((int) (scale * MAX_LEVEL));
            } finally {
                mInDrawing = false;
            }
            if (SystemClock.uptimeMillis() - mLastDrawTime >= mAnimationResolution) {
                mLastDrawTime = SystemClock.uptimeMillis();
                postInvalidateDelayed(mAnimationResolution);
            }
        }
        d.draw(canvas);
        canvas.restore();
        if (mShouldStartAnimationDrawable && d instanceof Animatable) {
            ((Animatable) d).start();
            mShouldStartAnimationDrawable = false;
        }
    }
}

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;/*from ww w.  j av a  2 s.c om*/
    }

    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:com.android.gallery3d.filtershow.imageshow.ImageShow.java

@Override
public void onDraw(Canvas canvas) {
    mPaint.reset();//from  w ww .  ja  va 2 s .c  om
    mPaint.setAntiAlias(true);
    mPaint.setFilterBitmap(true);
    MasterImage.getImage().setImageShowSize(getWidth() - 2 * mShadowMargin, getHeight() - 2 * mShadowMargin);

    MasterImage img = MasterImage.getImage();
    // Hide the loading indicator as needed
    if (mActivity.isLoadingVisible() && getFilteredImage() != null) {
        if ((img.getLoadedPreset() == null)
                || (img.getLoadedPreset() != null && img.getLoadedPreset().equals(img.getCurrentPreset()))) {
            mActivity.stopLoadingIndicator();
        } else if (img.getLoadedPreset() != null) {
            return;
        }
        mActivity.stopLoadingIndicator();
    }

    canvas.save();

    mShadowDrawn = false;

    Bitmap highresPreview = MasterImage.getImage().getHighresImage();
    Bitmap fullHighres = MasterImage.getImage().getPartialImage();

    boolean isDoingNewLookAnimation = MasterImage.getImage().onGoingNewLookAnimation();

    if (highresPreview == null || isDoingNewLookAnimation) {
        drawImageAndAnimate(canvas, getFilteredImage());
    } else {
        drawImageAndAnimate(canvas, highresPreview);
    }

    drawHighresImage(canvas, fullHighres);
    drawCompareImage(canvas, getGeometryOnlyImage());

    canvas.restore();

    if (!mEdgeEffect.isFinished()) {
        canvas.save();
        float dx = (getHeight() - getWidth()) / 2f;
        if (getWidth() > getHeight()) {
            dx = -(getWidth() - getHeight()) / 2f;
        }
        if (mCurrentEdgeEffect == EDGE_BOTTOM) {
            canvas.rotate(180, getWidth() / 2, getHeight() / 2);
        } else if (mCurrentEdgeEffect == EDGE_RIGHT) {
            canvas.rotate(90, getWidth() / 2, getHeight() / 2);
            canvas.translate(0, dx);
        } else if (mCurrentEdgeEffect == EDGE_LEFT) {
            canvas.rotate(270, getWidth() / 2, getHeight() / 2);
            canvas.translate(0, dx);
        }
        if (mCurrentEdgeEffect != 0) {
            mEdgeEffect.draw(canvas);
        }
        canvas.restore();
        invalidate();
    } else {
        mCurrentEdgeEffect = 0;
    }
}

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

@Override
public void draw(Canvas canvas) {
    if (!mCustomShadowsEnabled) {
        super.draw(canvas);
        return;/*ww  w . j a v a2 s  .  c  o m*/
    }

    final Drawable background = mBackground;
    if (background != null) {
        final int scrollX = getScrollX();
        final int scrollY = getScrollY();

        if (mBackgroundSizeChanged) {
            background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
            mBackgroundSizeChanged = false;
        }

        if ((scrollX | scrollY) == 0) {
            background.draw(canvas);
        } else {
            canvas.translate(scrollX, scrollY);
            background.draw(canvas);
            canvas.translate(-scrollX, -scrollY);
        }
    }

    // If text is transparent, don't draw any shadow
    if (getCurrentTextColor() == ContextCompat.getColor(getContext(), android.R.color.transparent)) {
        getPaint().clearShadowLayer();
        super.draw(canvas);
        return;
    }

    // We enhance the shadow by drawing the shadow twice
    float density = getResources().getDisplayMetrics().density;
    getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
    super.draw(canvas);
    canvas.save(Canvas.CLIP_SAVE_FLAG);
    canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(), getScrollX() + getWidth(),
            getScrollY() + getHeight(), Region.Op.INTERSECT);
    getPaint().setShadowLayer(density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
    super.draw(canvas);
    canvas.restore();
}

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();/* w w w.j ava 2 s .  c o m*/
        if (isLayoutRtl() && mMirrorForRtl) {
            canvas.translate(getWidth() - getPaddingRight(), getPaddingTop());
            canvas.scale(-1.0f, 1.0f);
        } 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.androfarsh.widget.DragGridLayout.java

private void drawHighlight(final View child, final Canvas canvas) {
    if ((mHighlightDrawable != null)) {
        requestFreeCellRegion(mDragNode.view);
        if (!requestHoverRect(mTmpRect)) {
            return;
        }//from ww  w  .  j  av a2 s  .  com
        canvas.save();
        canvas.clipRect(mTmpRect);
        mHighlightDrawable.setBounds(mTmpRect);

        mTmpRegion.set(mFreeCellsRegion);
        mTmpRegion.op(mTmpRect, Op.INTERSECT);
        mTmpRegion.getBounds(mTmpRect);

        final boolean allowed = mTmpRegion.isRect() && (child.getWidth() <= mTmpRect.width())
                && (child.getHeight() <= mTmpRect.height());
        final int[] stateSet = new int[] { (allowed ? 1 : -1) * R.attr.state_drop_allow };

        mHighlightDrawable.setState(stateSet);
        mHighlightDrawable.draw(canvas);
        canvas.restore();
    }
}