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:am.widget.indicatortabstrip.IndicatorTabStrip.java

/**
 * // w  w  w. ja va2  s . c om
 *
 * @param canvas     
 * @param position   ???
 * @param itemWidth  ?
 * @param itemHeight ?
 */
protected void drawText(Canvas canvas, int position, int itemWidth, int itemHeight) {
    if (getItemText(position) == null)
        return;
    String text = getItemText(position).toString();
    if (text.length() <= 0)
        return;
    mTextPaint.setTextSize(mTextSize);
    if (mTextColor == null) {
        mTextPaint.setColor(DEFAULT_TEXT_COLOR);
    } else {
        final int normalColor = mTextColor.getDefaultColor();
        final int selectedColor = mTextColor.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 centerX = ViewCompat.getPaddingStart(this)
            + (itemWidth + getIntervalWidth()) * ((float) position + 0.5f);
    final float centerY = getPaddingTop() + itemHeight * 0.5f;
    float scale;
    if (position == mNextPager) {
        scale = 1 + (mTextScale - 1) * mOffset;
    } else if (position == mCurrentPager) {
        scale = 1 + (mTextScale - 1) * (1 - mOffset);
    } else {
        scale = 1;
    }
    canvas.save();
    canvas.translate(centerX, centerY + mTextDesc);
    if (scale != 1) {
        canvas.scale(scale, scale, 0, -mTextDesc);
    }
    canvas.drawText(text, 0, 0, mTextPaint);
    canvas.restore();
}

From source file:om.sstvencoder.CropView.java

private void drawBitmap(Canvas canvas) {
    int w = mImageWidth;
    int h = mImageHeight;
    for (int i = 0; i < mOrientation / 90; ++i) {
        int tmp = w;
        w = h;//ww  w  . j a  va 2s.  co  m
        h = tmp;
        //noinspection SuspiciousNameCombination
        mImageDrawRect.set(mImageDrawRect.top, h - mImageDrawRect.left, mImageDrawRect.bottom,
                h - mImageDrawRect.right);
        //noinspection SuspiciousNameCombination
        mCanvasDrawRect.set(mCanvasDrawRect.top, -mCanvasDrawRect.right, mCanvasDrawRect.bottom,
                -mCanvasDrawRect.left);
    }
    mImageDrawRect.sort();
    canvas.save();
    canvas.rotate(mOrientation);
    if (!mSmallImage) {
        int sampleSize = getSampleSize();
        if (sampleSize < mCacheSampleSize || !mCacheRect.contains(mImageDrawRect)) {
            if (mCacheBitmap != null)
                mCacheBitmap.recycle();
            int cacheWidth = mImageDrawRect.width();
            int cacheHeight = mImageDrawRect.height();
            while (cacheWidth * cacheHeight < (sampleSize * 1024 * sampleSize * 1024)) {
                cacheWidth += mImageDrawRect.width();
                cacheHeight += mImageDrawRect.height();
            }
            mCacheRect.set(Math.max(0, ~(sampleSize - 1) & (mImageDrawRect.centerX() - cacheWidth / 2)),
                    Math.max(0, ~(sampleSize - 1) & (mImageDrawRect.centerY() - cacheHeight / 2)),
                    Math.min(mRegionDecoder.getWidth(),
                            ~(sampleSize - 1) & (mImageDrawRect.centerX() + cacheWidth / 2 + sampleSize - 1)),
                    Math.min(mRegionDecoder.getHeight(),
                            ~(sampleSize - 1) & (mImageDrawRect.centerY() + cacheHeight / 2 + sampleSize - 1)));
            mBitmapOptions.inSampleSize = mCacheSampleSize = sampleSize;
            mCacheBitmap = mRegionDecoder.decodeRegion(mCacheRect, mBitmapOptions);
        }
        mImageDrawRect.offset(-mCacheRect.left, -mCacheRect.top);
        mImageDrawRect.left /= mCacheSampleSize;
        mImageDrawRect.top /= mCacheSampleSize;
        mImageDrawRect.right /= mCacheSampleSize;
        mImageDrawRect.bottom /= mCacheSampleSize;
    }
    canvas.drawBitmap(mCacheBitmap, mImageDrawRect, mCanvasDrawRect, mPaint);
    canvas.restore();
}

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

@Override
public void draw(Canvas canvas) {
    // If capturing a texture of the toolbar, ensure the alpha is set prior to draw(...) being
    // called.  The alpha is being used prior to getting to draw(...), so updating the value
    // after this point was having no affect.
    if (mTextureCaptureMode)
        assert getAlpha() == 1f;

    // mClipRect can change in the draw call, so cache this value to ensure the canvas is
    // restored correctly.
    boolean shouldClip = !mTextureCaptureMode && mClipRect != null;
    if (shouldClip) {
        canvas.save();/*from  ww w  .j  av  a 2s.c o m*/
        canvas.clipRect(mClipRect);
    }
    super.draw(canvas);
    if (shouldClip) {
        canvas.restore();

        // Post an invalidate when the clip rect becomes null to ensure another draw pass occurs
        // and the full toolbar is drawn again.
        if (mClipRect == null)
            postInvalidate();
    }
}

From source file:org.akop.ararat.view.CrosswordView.java

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

    constrainTranslation();// ww  w .j  a v a  2  s. co  m

    canvas.save();
    canvas.clipRect(mContentRect);
    canvas.translate(mBitmapOffset.x, mBitmapOffset.y);
    canvas.scale(mBitmapScale, mBitmapScale);

    if (mPuzzleBitmap != null) {
        canvas.drawBitmap(mPuzzleBitmap, 0, 0, mBitmapPaint);
    } else {
        // Perform a fast, barebones render so that the screen doesn't
        // look completely empty

        mInPlaceRenderer.renderPuzzle(canvas, mRenderScale, true);
    }

    canvas.restore();
}

From source file:de.uni_weimar.mheinz.androidtouchscope.display.HandleView.java

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

    if (mIsOn) {/* w  w w  .  ja  v  a 2  s  .  c o  m*/
        mShapeDrawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
        mShapeDrawable.getPaint().setColor(mColor);
        mShapeDrawable.getPaint().setShadowLayer(2, 2, 2, Color.GRAY);
        mShapeDrawable.draw(canvas);
    } else {
        /*mShapeDrawable.getPaint().setStyle(Paint.Style.FILL);
        mShapeDrawable.getPaint().setColor(Color.WHITE);
        mShapeDrawable.getPaint().setShadowLayer(2,2,2,Color.GRAY);
        mShapeDrawable.draw(canvas);
        mShapeDrawable.getPaint().setStyle(Paint.Style.STROKE);
        mShapeDrawable.getPaint().setColor(Color.BLACK);
        mShapeDrawable.draw(canvas);*/
        mShapeDrawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
        mShapeDrawable.getPaint().setColor(Color.LTGRAY);
        mShapeDrawable.getPaint().setShadowLayer(2, 2, 2, Color.GRAY);
        mShapeDrawable.draw(canvas);
    }

    PointF center = getCircleCenter();
    mMainTextPaint.getTextBounds(mMainText, 0, mMainText.length(), mTextBounds);
    if (mOrientation == HandleDirection.RIGHT)
        canvas.drawText(mMainText, center.x + 5, center.y + mTextBounds.height() / 2 - 1, mMainTextPaint);
    else if (mOrientation == HandleDirection.LEFT)
        canvas.drawText(mMainText, center.x - 5, center.y + mTextBounds.height() / 2 - 2, mMainTextPaint);
    else
        canvas.drawText(mMainText, center.x, center.y + mTextBounds.height() / 2, mMainTextPaint);

    if (mPressDrawable != null) {
        if (mOrientation == HandleDirection.RIGHT) {
            mPressDrawable.setBounds(2, (int) (center.y - HANDLE_BREADTH / 2), HANDLE_BREADTH - 3,
                    (int) center.y + HANDLE_BREADTH / 2);
            mPressDrawable.draw(canvas);
        } else if (mOrientation == HandleDirection.LEFT) {
            canvas.save();
            canvas.rotate(180, HANDLE_LENGTH / 2, mHandlePos);
            mPressDrawable.setBounds(1, (int) (center.y - HANDLE_BREADTH / 2), HANDLE_BREADTH - 5,
                    (int) center.y + HANDLE_BREADTH / 2);
            mPressDrawable.draw(canvas);
            canvas.restore();
        }
    }
}

From source file:com.ftinc.kit.attributr.ui.widget.StickyRecyclerHeadersElevationDecoration.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override/*from   w  w w .  j  ava2  s.  c  o m*/
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(canvas, parent, state);
    int orientation = getOrientation(parent);
    mHeaderRects.clear();

    if (parent.getChildCount() > 0 && mAdapter.getItemCount() > 0) {

        // draw the first visible child's header at the top of the view
        View firstView = parent.getChildAt(0);
        int firstPosition = parent.getChildPosition(firstView);

        if (mAdapter.getHeaderId(firstPosition) > -1) {

            View firstHeader = getHeaderView(parent, firstPosition);
            View nextView = getNextView(parent);

            int translationX = Math.max(parent.getChildAt(0).getLeft() - firstHeader.getWidth(), 0);
            int translationY = Math.max(parent.getChildAt(0).getTop() - firstHeader.getHeight(), 0);
            int nextPosition = parent.getChildPosition(nextView);

            if (nextPosition > 0 && hasNewHeader(nextPosition)) {

                View secondHeader = getHeaderView(parent, nextPosition);

                //Translate the topmost header so the next header takes its place, if applicable
                if (orientation == LinearLayoutManager.VERTICAL
                        && nextView.getTop() - secondHeader.getHeight() - firstHeader.getHeight() < 0) {

                    translationY += nextView.getTop() - secondHeader.getHeight() - firstHeader.getHeight();

                } else if (orientation == LinearLayoutManager.HORIZONTAL
                        && nextView.getLeft() - secondHeader.getWidth() - firstHeader.getWidth() < 0) {

                    translationX += nextView.getLeft() - secondHeader.getWidth() - firstHeader.getWidth();

                }
            }

            boolean shouldDrawShadow = true;
            if (firstPosition == 0 && (firstView.getTop() == firstHeader.getHeight())) {
                shouldDrawShadow = false;
            }

            if (translationY == 0 && shouldDrawShadow) {

                ImageView shadow = ButterKnife.findById(firstHeader, R.id.shadow);
                shadow.setVisibility(View.VISIBLE);

            } else {

                ImageView shadow = ButterKnife.findById(firstHeader, R.id.shadow);
                shadow.setVisibility(View.GONE);

            }

            canvas.save();
            canvas.translate(translationX, translationY);
            firstHeader.draw(canvas);
            canvas.restore();
            mHeaderRects.put(firstPosition, new Rect(translationX, translationY,
                    translationX + firstHeader.getWidth(), translationY + firstHeader.getHeight()));

        }

        if (parent.getChildCount() > 1 && mAdapter.getItemCount() > 1) {
            for (int i = 1; i < parent.getChildCount(); i++) {

                int position = parent.getChildPosition(parent.getChildAt(i));
                if (hasNewHeader(position)) {

                    // this header is different than the previous, it must be drawn in the correct place
                    int translationX = 0;
                    int translationY = 0;
                    View header = getHeaderView(parent, position);

                    if (orientation == LinearLayoutManager.VERTICAL) {

                        translationY = parent.getChildAt(i).getTop() - header.getHeight();

                    } else {

                        translationX = parent.getChildAt(i).getLeft() - header.getWidth();

                    }

                    // don't render shadow
                    ImageView shadow = ButterKnife.findById(header, R.id.shadow);
                    shadow.setVisibility(View.GONE);

                    canvas.save();
                    canvas.translate(translationX, translationY);
                    header.draw(canvas);
                    canvas.restore();
                    mHeaderRects.put(position, new Rect(translationX, translationY,
                            translationX + header.getWidth(), translationY + header.getHeight()));
                }
            }
        }
    }
}

From source file:com.huewu.pla.lib.internal.PLAListView.java

void drawOverscrollFooter(final Canvas canvas, final Drawable drawable, final Rect bounds) {
    final int height = drawable.getMinimumHeight();

    canvas.save();//  w w w.j a  va2s .c om
    canvas.clipRect(bounds);

    final int span = bounds.bottom - bounds.top;
    if (span < height) {
        bounds.bottom = bounds.top + height;
    }

    drawable.setBounds(bounds);
    drawable.draw(canvas);

    canvas.restore();
}

From source file:com.huewu.pla.lib.internal.PLAListView.java

void drawOverscrollHeader(final Canvas canvas, final Drawable drawable, final Rect bounds) {
    final int height = drawable.getMinimumHeight();

    canvas.save();/*from  w w  w  .  j a v a2s  .  com*/
    canvas.clipRect(bounds);

    final int span = bounds.bottom - bounds.top;
    if (span < height) {
        bounds.top = bounds.bottom - height;
    }

    drawable.setBounds(bounds);
    drawable.draw(canvas);

    canvas.restore();
}

From source file:com.huewu.pla.lib.internal.PLAListView.java

/**
 * Draws a divider for the given child in the given bounds.
 * /*from   w  ww  .  j a va2s . c o m*/
 * @param canvas The canvas to draw to.
 * @param bounds The bounds of the divider.
 * @param childIndex The index of child (of the View) above the divider.
 *            This will be -1 if there is no child above the divider to be
 *            drawn.
 */
void drawDivider(final Canvas canvas, final Rect bounds, final int childIndex) {
    // This widget draws the same divider for all children
    final Drawable divider = mDivider;
    final boolean clipDivider = mClipDivider;

    if (!clipDivider) {
        divider.setBounds(bounds);
    } else {
        canvas.save();
        canvas.clipRect(bounds);
    }

    divider.draw(canvas);

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

From source file:com.huewu.pla.lib.internal.PLA_ListView.java

/**
 * Draws a divider for the given child in the given bounds.
 * //w w w.  ja  v a2s .c  o  m
 * @param canvas
 *            The canvas to draw to.
 * @param bounds
 *            The bounds of the divider.
 * @param childIndex
 *            The index of child (of the View) above the divider. This will
 *            be -1 if there is no child above the divider to be drawn.
 */
void drawDivider(Canvas canvas, Rect bounds, int childIndex) {
    // This widget draws the same divider for all children
    final Drawable divider = mDivider;
    final boolean clipDivider = mClipDivider;

    if (!clipDivider) {
        divider.setBounds(bounds);
    } else {
        canvas.save();
        canvas.clipRect(bounds);
    }

    divider.draw(canvas);

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