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:pl.motyczko.scrollheader.PagerSlidingTabStrip.java

private void drawIcon(Canvas canvas) {
    if (mIcon == null)
        return;/* ww w.java2 s  . c  o  m*/
    canvas.save();
    float translationX = calculateIconTranslationX();
    float translationY = calculateIconTranslationY();
    canvas.translate(translationX, translationY);
    float scale = calculateIconScale();
    canvas.scale(scale, scale);
    mIcon.draw(canvas);
    canvas.restore();
}

From source file:com.mikelau.croperino.HighlightView.java

protected void draw(Canvas canvas) {

    if (mHidden) {
        return;//from   w  w  w  . j  a  v  a  2 s.  c o  m
    }

    Path path = new Path();
    if (!hasFocus()) {
        mOutlinePaint.setColor(0xFF000000);
        canvas.drawRect(mDrawRect, mOutlinePaint);
    } else {
        Rect viewDrawingRect = new Rect();
        mContext.getDrawingRect(viewDrawingRect);
        if (mCircle) {

            canvas.save();

            float width = mDrawRect.width();
            float height = mDrawRect.height();
            path.addCircle(mDrawRect.left + (width / 2), mDrawRect.top + (height / 2), width / 2,
                    Path.Direction.CW);
            mOutlinePaint.setColor(0xFFEF04D6);

            canvas.clipPath(path, Region.Op.DIFFERENCE);
            canvas.drawRect(viewDrawingRect, hasFocus() ? mFocusPaint : mNoFocusPaint);

            canvas.restore();

        } else if (mOvalo) {
            canvas.save();

            RectF r = new RectF(mDrawRect.left, mDrawRect.top, mDrawRect.right, mDrawRect.bottom);

            path.addOval(r, Path.Direction.CW);
            mOutlinePaint.setColor(0xFFEF04D6);

            canvas.clipPath(path, Region.Op.DIFFERENCE);
            canvas.drawRect(viewDrawingRect, hasFocus() ? mFocusPaint : mNoFocusPaint);

            canvas.restore();
        } else {

            Rect topRect = new Rect(viewDrawingRect.left, viewDrawingRect.top, viewDrawingRect.right,
                    mDrawRect.top);
            if (topRect.width() > 0 && topRect.height() > 0) {
                canvas.drawRect(topRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect bottomRect = new Rect(viewDrawingRect.left, mDrawRect.bottom, viewDrawingRect.right,
                    viewDrawingRect.bottom);
            if (bottomRect.width() > 0 && bottomRect.height() > 0) {
                canvas.drawRect(bottomRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect leftRect = new Rect(viewDrawingRect.left, topRect.bottom, mDrawRect.left, bottomRect.top);
            if (leftRect.width() > 0 && leftRect.height() > 0) {
                canvas.drawRect(leftRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect rightRect = new Rect(mDrawRect.right, topRect.bottom, viewDrawingRect.right, bottomRect.top);
            if (rightRect.width() > 0 && rightRect.height() > 0) {
                canvas.drawRect(rightRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }

            path.addRect(new RectF(mDrawRect), Path.Direction.CW);

            mOutlinePaint.setColor(0xFFFF8A00);

        }

        canvas.drawPath(path, mOutlinePaint);

        if (mMode == ModifyMode.Grow) {
            if (mCircle) {
                int width = mResizeDrawableDiagonal.getIntrinsicWidth();
                int height = mResizeDrawableDiagonal.getIntrinsicHeight();

                int d = (int) Math.round(Math.cos(/*45deg*/Math.PI / 4D) * (mDrawRect.width() / 2D));
                int x = mDrawRect.left + (mDrawRect.width() / 2) + d - width / 2;
                int y = mDrawRect.top + (mDrawRect.height() / 2) - d - height / 2;
                mResizeDrawableDiagonal.setBounds(x, y, x + mResizeDrawableDiagonal.getIntrinsicWidth(),
                        y + mResizeDrawableDiagonal.getIntrinsicHeight());
                mResizeDrawableDiagonal.draw(canvas);
            } else {
                int left = mDrawRect.left + 1;
                int right = mDrawRect.right + 1;
                int top = mDrawRect.top + 4;
                int bottom = mDrawRect.bottom + 3;

                int widthWidth = mResizeDrawableWidth.getIntrinsicWidth() / 2;
                int widthHeight = mResizeDrawableWidth.getIntrinsicHeight() / 2;
                int heightHeight = mResizeDrawableHeight.getIntrinsicHeight() / 2;
                int heightWidth = mResizeDrawableHeight.getIntrinsicWidth() / 2;

                int xMiddle = mDrawRect.left + ((mDrawRect.right - mDrawRect.left) / 2);
                int yMiddle = mDrawRect.top + ((mDrawRect.bottom - mDrawRect.top) / 2);

                mResizeDrawableWidth.setBounds(left - widthWidth, yMiddle - widthHeight, left + widthWidth,
                        yMiddle + widthHeight);
                mResizeDrawableWidth.draw(canvas);

                mResizeDrawableWidth.setBounds(right - widthWidth, yMiddle - widthHeight, right + widthWidth,
                        yMiddle + widthHeight);
                mResizeDrawableWidth.draw(canvas);

                mResizeDrawableHeight.setBounds(xMiddle - heightWidth, top - heightHeight,
                        xMiddle + heightWidth, top + heightHeight);
                mResizeDrawableHeight.draw(canvas);

                mResizeDrawableHeight.setBounds(xMiddle - heightWidth, bottom - heightHeight,
                        xMiddle + heightWidth, bottom + heightHeight);
                mResizeDrawableHeight.draw(canvas);
            }
        }
    }
}

From source file:com.hgdendi.contactslist.common.FloatingBarItemDecoration.java

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(c, parent, state);
    final int position = ((LinearLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
    if (position == RecyclerView.NO_POSITION) {
        return;//from   w w  w .j a v a 2 s  . c  o  m
    }
    View child = parent.findViewHolderForAdapterPosition(position).itemView;
    String initial = getTag(position);
    if (initial == null) {
        return;
    }

    boolean flag = false;
    if (getTag(position + 1) != null && !initial.equals(getTag(position + 1))) {
        if (child.getHeight() + child.getTop() < mTitleHeight) {
            c.save();
            flag = true;
            c.translate(0, child.getHeight() + child.getTop() - mTitleHeight);
        }
    }

    c.drawRect(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getRight() - parent.getPaddingRight(),
            parent.getPaddingTop() + mTitleHeight, mBackgroundPaint);
    c.drawText(initial, child.getPaddingLeft() + mTextStartMargin,
            parent.getPaddingTop() + mTitleHeight - (mTitleHeight - mTextHeight) / 2 - mTextBaselineOffset,
            mTextPaint);

    if (flag) {
        c.restore();
    }
}

From source file:de.telekom.pde.codelibrary.ui.layout.PDESwipeRefreshLayout.java

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

    int width = mBounds.width();
    int height = mBounds.height();
    int cx = width / 2;
    int cy = height / 2;

    int restoreCount = canvas.save();
    canvas.clipRect(mBounds);//w w w. j a  v a2s  . co m

    if (mRunning) {
        long now = AnimationUtils.currentAnimationTimeMillis();
        long elapsed = (now - mStartTime) % mAnimationDuration;
        float currentAngle = (elapsed / (mAnimationDuration / 100f)) * 3.6f;

        canvas.drawCircle(cx, cy, PDEBuildingUnits.BU(), mPaint);

        canvas.save();
        canvas.rotate(currentAngle, cx, cy);
        canvas.drawPath(mCirclePath, mWhitePaint);
        canvas.restore();

        ViewCompat.postInvalidateOnAnimation(this);
    } else {
        // Otherwise if we're in the middle of a trigger, draw that.
        if (mTriggerPercentage > 0 && mTriggerPercentage <= 1.0) {
            drawTrigger(canvas, cx, cy);
        }
    }
    canvas.restoreToCount(restoreCount);
}

From source file:com.dongdong.wheel.WheelView.java

/**
 * Draws value and mLabel layout// ww  w  .j a  v a2  s  .  c om
 *
 * @param canvas the canvas for drawing
 */
private void drawValue(Canvas canvas) {
    mValuePaint.setColor(VALUE_TEXT_COLOR);
    mValuePaint.drawableState = getDrawableState();

    Rect bounds = new Rect();
    mItemsLayout.getLineBounds(mVisibleItems / 2, bounds);

    // draw mLabel
    if (mLabelLayout != null) {
        canvas.save();
        canvas.translate(mItemsLayout.getWidth() + LABEL_OFFSET, bounds.top);
        mLabelLayout.draw(canvas);
        canvas.restore();
    }

    // draw current value
    if (mValueLayout != null) {
        canvas.save();
        canvas.translate(0, bounds.top + mScrollingOffset);
        mValueLayout.draw(canvas);
        canvas.restore();
    }
}

From source file:com.android.camera.HighlightView.java

void draw(Canvas canvas) {
    if (mHidden) {
        return;//from  w  w w.ja v a 2 s.  co  m
    }
    canvas.save();
    Path path = new Path();
    if (!hasFocus()) {
        mOutlinePaint.setColor(0xFF000000);
        canvas.drawRect(mDrawRect, mOutlinePaint);
    } else {
        Rect viewDrawingRect = new Rect();
        mContext.getDrawingRect(viewDrawingRect);
        if (mCircle) {
            float width = mDrawRect.width();
            float height = mDrawRect.height();
            path.addCircle(mDrawRect.left + (width / 2), mDrawRect.top + (height / 2), width / 2,
                    Path.Direction.CW);
            mOutlinePaint.setColor(mOutlineCircleColor);
        } else {
            path.addRect(new RectF(mDrawRect), Path.Direction.CW);
            mOutlinePaint.setColor(mOutlineColor);
        }
        canvas.clipPath(path, Region.Op.DIFFERENCE);
        canvas.drawRect(viewDrawingRect, hasFocus() ? mFocusPaint : mNoFocusPaint);

        canvas.restore();
        canvas.drawPath(path, mOutlinePaint);

        if (mMode == ModifyMode.Grow) {
            if (mCircle) {
                int width = mResizeDrawableDiagonal.getIntrinsicWidth();
                int height = mResizeDrawableDiagonal.getIntrinsicHeight();

                int d = (int) Math.round(Math.cos(/*45deg*/Math.PI / 4D) * (mDrawRect.width() / 2D));
                int x = mDrawRect.left + (mDrawRect.width() / 2) + d - width / 2;
                int y = mDrawRect.top + (mDrawRect.height() / 2) - d - height / 2;
                mResizeDrawableDiagonal.setBounds(x, y, x + mResizeDrawableDiagonal.getIntrinsicWidth(),
                        y + mResizeDrawableDiagonal.getIntrinsicHeight());
                mResizeDrawableDiagonal.draw(canvas);
            } else {
                int left = mDrawRect.left + 1;
                int right = mDrawRect.right + 1;
                int top = mDrawRect.top + 4;
                int bottom = mDrawRect.bottom + 3;

                int widthWidth = mResizeDrawableWidth.getIntrinsicWidth() / 2;
                int widthHeight = mResizeDrawableWidth.getIntrinsicHeight() / 2;
                int heightHeight = mResizeDrawableHeight.getIntrinsicHeight() / 2;
                int heightWidth = mResizeDrawableHeight.getIntrinsicWidth() / 2;

                int xMiddle = mDrawRect.left + ((mDrawRect.right - mDrawRect.left) / 2);
                int yMiddle = mDrawRect.top + ((mDrawRect.bottom - mDrawRect.top) / 2);

                mResizeDrawableWidth.setBounds(left - widthWidth, yMiddle - widthHeight, left + widthWidth,
                        yMiddle + widthHeight);
                mResizeDrawableWidth.draw(canvas);

                mResizeDrawableWidth.setBounds(right - widthWidth, yMiddle - widthHeight, right + widthWidth,
                        yMiddle + widthHeight);
                mResizeDrawableWidth.draw(canvas);

                mResizeDrawableHeight.setBounds(xMiddle - heightWidth, top - heightHeight,
                        xMiddle + heightWidth, top + heightHeight);
                mResizeDrawableHeight.draw(canvas);

                mResizeDrawableHeight.setBounds(xMiddle - heightWidth, bottom - heightHeight,
                        xMiddle + heightWidth, bottom + heightHeight);
                mResizeDrawableHeight.draw(canvas);
            }
        }
    }
}

From source file:kr.wdream.storyshop.AndroidUtilities.java

private static Intent createShortcutIntent(long did, boolean forDelete) {
    Intent shortcutIntent = new Intent(ApplicationLoader.applicationContext, OpenChatReceiver.class);

    int lower_id = (int) did;
    int high_id = (int) (did >> 32);

    TLRPC.User user = null;//from  w w w .ja v a 2 s .  c om
    TLRPC.Chat chat = null;
    if (lower_id == 0) {
        shortcutIntent.putExtra("encId", high_id);
        TLRPC.EncryptedChat encryptedChat = MessagesController.getInstance().getEncryptedChat(high_id);
        if (encryptedChat == null) {
            return null;
        }
        user = MessagesController.getInstance().getUser(encryptedChat.user_id);
    } else if (lower_id > 0) {
        shortcutIntent.putExtra("userId", lower_id);
        user = MessagesController.getInstance().getUser(lower_id);
    } else if (lower_id < 0) {
        chat = MessagesController.getInstance().getChat(-lower_id);
        shortcutIntent.putExtra("chatId", -lower_id);
    } else {
        return null;
    }
    if (user == null && chat == null) {
        return null;
    }

    String name;
    TLRPC.FileLocation photo = null;

    if (user != null) {
        name = ContactsController.formatName(user.first_name, user.last_name);
        if (user.photo != null) {
            photo = user.photo.photo_small;
        }
    } else {
        name = chat.title;
        if (chat.photo != null) {
            photo = chat.photo.photo_small;
        }
    }

    shortcutIntent.setAction("com.tmessages.openchat" + did);
    shortcutIntent.addFlags(0x4000000);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    addIntent.putExtra("duplicate", false);
    if (!forDelete) {
        Bitmap bitmap = null;
        if (photo != null) {
            try {
                File path = FileLoader.getPathToAttach(photo, true);
                bitmap = BitmapFactory.decodeFile(path.toString());
                if (bitmap != null) {
                    int size = AndroidUtilities.dp(58);
                    Bitmap result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
                    result.eraseColor(Color.TRANSPARENT);
                    Canvas canvas = new Canvas(result);
                    BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
                            Shader.TileMode.CLAMP);
                    if (roundPaint == null) {
                        roundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
                        bitmapRect = new RectF();
                    }
                    float scale = size / (float) bitmap.getWidth();
                    canvas.save();
                    canvas.scale(scale, scale);
                    roundPaint.setShader(shader);
                    bitmapRect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
                    canvas.drawRoundRect(bitmapRect, bitmap.getWidth(), bitmap.getHeight(), roundPaint);
                    canvas.restore();
                    Drawable drawable = ApplicationLoader.applicationContext.getResources()
                            .getDrawable(R.drawable.book_logo);
                    int w = AndroidUtilities.dp(15);
                    int left = size - w - AndroidUtilities.dp(2);
                    int top = size - w - AndroidUtilities.dp(2);
                    drawable.setBounds(left, top, left + w, top + w);
                    drawable.draw(canvas);
                    try {
                        canvas.setBitmap(null);
                    } catch (Exception e) {
                        //don't promt, this will crash on 2.x
                    }
                    bitmap = result;
                }
            } catch (Throwable e) {
                FileLog.e("tmessages", e);
            }
        }
        if (bitmap != null) {
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
        } else {
            if (user != null) {
                if (user.bot) {
                    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                            .fromContext(ApplicationLoader.applicationContext, R.drawable.book_bot));
                } else {
                    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                            .fromContext(ApplicationLoader.applicationContext, R.drawable.book_user));
                }
            } else if (chat != null) {
                if (ChatObject.isChannel(chat) && !chat.megagroup) {
                    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                            .fromContext(ApplicationLoader.applicationContext, R.drawable.book_channel));
                } else {
                    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                            .fromContext(ApplicationLoader.applicationContext, R.drawable.book_group));
                }
            }
        }
    }
    return addIntent;
}

From source file:ca.barrenechea.widget.recyclerview.decoration.DoubleHeaderDecoration.java

/**
 * {@inheritDoc}//www .j  av  a 2s  .c  o  m
 */
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    final int count = parent.getChildCount();

    boolean headerDrawn = false;
    for (int layoutPos = 0; layoutPos < count; layoutPos++) {
        final View child = parent.getChildAt(layoutPos);
        boolean visible = getAnimatedTop(child) > -child.getHeight()/* && child.getTop() < parent.getHeight()*/;
        final int adapterPos = parent.getChildAdapterPosition(child);
        if (visible && adapterPos != RecyclerView.NO_POSITION && (!headerDrawn || hasSubHeader(adapterPos))) {
            int left, top;
            final View header = getHeader(parent, adapterPos).itemView;
            final View subHeader = getSubHeader(parent, adapterPos).itemView;

            c.save();
            left = child.getLeft();
            top = getSubHeaderTop(parent, child, header, subHeader, adapterPos, layoutPos);
            c.translate(left, top);
            subHeader.setTranslationX(left);
            subHeader.setTranslationY(top);
            subHeader.draw(c);
            c.restore();

            if (!headerDrawn || hasHeader(adapterPos)) {
                c.save();
                left = child.getLeft();
                top = getHeaderTop(parent, child, header, subHeader, adapterPos, layoutPos);
                c.translate(left, top);
                header.setTranslationX(left);
                header.setTranslationY(top);
                header.draw(c);
                c.restore();
            }

            headerDrawn = true;
        }
    }
}

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

/**
 * Constructs a new omnibox suggestion view.
 *
 * @param context The context used to construct the suggestion view.
 * @param locationBar The location bar showing these suggestions.
 */// ww w .  ja  v a 2  s.c o  m
public SuggestionView(Context context, LocationBar locationBar) {
    super(context);
    mLocationBar = locationBar;

    mSuggestionHeight = context.getResources().getDimensionPixelOffset(R.dimen.omnibox_suggestion_height);
    mSuggestionAnswerHeight = context.getResources()
            .getDimensionPixelOffset(R.dimen.omnibox_suggestion_answer_height);

    TypedArray a = getContext().obtainStyledAttributes(new int[] { R.attr.selectableItemBackground });
    Drawable itemBackground = a.getDrawable(0);
    a.recycle();

    mContentsView = new SuggestionContentsContainer(context, itemBackground);
    addView(mContentsView);

    mRefineView = new View(context) {
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            if (mRefineIcon == null)
                return;
            canvas.save();
            canvas.translate((getMeasuredWidth() - mRefineIcon.getIntrinsicWidth()) / 2f,
                    (getMeasuredHeight() - mRefineIcon.getIntrinsicHeight()) / 2f);
            mRefineIcon.draw(canvas);
            canvas.restore();
        }

        @Override
        public void setVisibility(int visibility) {
            super.setVisibility(visibility);

            if (visibility == VISIBLE) {
                setClickable(true);
                setFocusable(true);
            } else {
                setClickable(false);
                setFocusable(false);
            }
        }

        @Override
        protected void drawableStateChanged() {
            super.drawableStateChanged();

            if (mRefineIcon != null && mRefineIcon.isStateful()) {
                mRefineIcon.setState(getDrawableState());
            }
        }
    };
    mRefineView.setContentDescription(getContext().getString(R.string.accessibility_omnibox_btn_refine));

    // Although this has the same background as the suggestion view, it can not be shared as
    // it will result in the state of the drawable being shared and always showing up in the
    // refine view.
    mRefineView.setBackground(itemBackground.getConstantState().newDrawable());
    mRefineView.setId(R.id.refine_view_id);
    mRefineView.setClickable(true);
    mRefineView.setFocusable(true);
    mRefineView.setLayoutParams(new LayoutParams(0, 0));
    addView(mRefineView);

    mRefineWidth = getResources().getDimensionPixelSize(R.dimen.omnibox_suggestion_refine_width);

    mUrlBar = (UrlBar) locationBar.getContainerView().findViewById(R.id.url_bar);

    mPhoneUrlBarLeftOffsetPx = getResources()
            .getDimensionPixelOffset(R.dimen.omnibox_suggestion_phone_url_bar_left_offset);
    mPhoneUrlBarLeftOffsetRtlPx = getResources()
            .getDimensionPixelOffset(R.dimen.omnibox_suggestion_phone_url_bar_left_offset_rtl);
}

From source file:com.acbelter.scheduleview.ScheduleView.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    changeClipRect(canvas);//  w w  w.j  a  va  2 s  .  co  m

    canvas.save();
    canvas.translate(getPaddingLeft(), mListY + mInternalPaddingTop + getPaddingTop());
    ViewHolder holder = (ViewHolder) mTimeMark.getTag();
    for (int i = 0; i < mTimeMarks.size(); i++) {
        holder.time.setText(mTimeMarks.get(i));
        mTimeMark.layout(0, 0, mTimeMark.getMeasuredWidth(), mTimeMark.getMeasuredHeight());
        mTimeMark.draw(canvas);

        if (i != mTimeMarks.size() - 1) {
            canvas.translate(0, mTimeMarkHeight + mTimeMarksDistance);
        }
    }
    canvas.restore();

    drawEdgeEffects(canvas);
}