Example usage for android.graphics Canvas restoreToCount

List of usage examples for android.graphics Canvas restoreToCount

Introduction

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

Prototype

public void restoreToCount(int saveCount) 

Source Link

Document

Efficient way to pop any calls to save() that happened after the save count reached saveCount.

Usage

From source file:au.com.glassechidna.velocityviewpager.VelocityViewPager.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    boolean needsInvalidate = false;

    final int overScrollMode = ViewCompat.getOverScrollMode(this);
    if (overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS
            || (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mLeftEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int width = getWidth();

            canvas.rotate(270);/* w  w w  . j  a  va2s  .  co  m*/
            canvas.translate(-height + getPaddingTop(), mFirstOffset * width);
            mLeftEdge.setSize(height, width);
            needsInvalidate |= mLeftEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!mRightEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();

            canvas.rotate(90);
            canvas.translate(-getPaddingTop(), -(mLastOffset + 1) * width);
            mRightEdge.setSize(height, width);
            needsInvalidate |= mRightEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    } else {
        mLeftEdge.finish();
        mRightEdge.finish();
    }

    if (needsInvalidate) {
        // Keep animating
        ViewCompat.postInvalidateOnAnimation(this);
    }
}

From source file:com.bettervectordrawable.lib.graphics.drawable.VectorDrawable.java

@Override
public void draw(Canvas canvas) {
    // We will offset the bounds for drawBitmap, so copyBounds() here instead
    // of getBounds().
    copyBounds(mTmpBounds);//from  w  w  w  .j av a2  s  .  c  o m
    if (mTmpBounds.width() <= 0 || mTmpBounds.height() <= 0) {
        // Nothing to draw
        return;
    }
    // Color filters always override tint filters.
    final ColorFilter colorFilter = (mColorFilter == null ? mTintFilter : mColorFilter);
    // The imageView can scale the canvas in different ways, in order to
    // avoid blurry scaling, we have to draw into a bitmap with exact pixel
    // size first. This bitmap size is determined by the bounds and the
    // canvas scale.
    canvas.getMatrix(mTmpMatrix);
    mTmpMatrix.getValues(mTmpFloats);
    float canvasScaleX = Math.abs(mTmpFloats[Matrix.MSCALE_X]);
    float canvasScaleY = Math.abs(mTmpFloats[Matrix.MSCALE_Y]);
    int scaledWidth = (int) (mTmpBounds.width() * canvasScaleX);
    int scaledHeight = (int) (mTmpBounds.height() * canvasScaleY);
    scaledWidth = Math.min(MAX_CACHED_BITMAP_SIZE, scaledWidth);
    scaledHeight = Math.min(MAX_CACHED_BITMAP_SIZE, scaledHeight);
    if (scaledWidth <= 0 || scaledHeight <= 0) {
        return;
    }
    final int saveCount = canvas.save();
    canvas.translate(mTmpBounds.left, mTmpBounds.top);
    // Handle RTL mirroring.
    final boolean needMirroring = needMirroring();
    if (needMirroring) {
        canvas.translate(mTmpBounds.width(), 0);
        canvas.scale(-1.0f, 1.0f);
    }
    // At this point, canvas has been translated to the right position.
    // And we use this bound for the destination rect for the drawBitmap, so
    // we offset to (0, 0);
    mTmpBounds.offsetTo(0, 0);
    mVectorState.createCachedBitmapIfNeeded(scaledWidth, scaledHeight);
    if (!mAllowCaching) {
        mVectorState.updateCachedBitmap(scaledWidth, scaledHeight);
    } else {
        if (!mVectorState.canReuseCache()) {
            mVectorState.updateCachedBitmap(scaledWidth, scaledHeight);
            mVectorState.updateCacheStates();
        }
    }
    mVectorState.drawCachedBitmapWithRootAlpha(canvas, colorFilter, mTmpBounds);
    canvas.restoreToCount(saveCount);
}

From source file:com.jecelyin.editor.v2.widget.AnyDrawerLayout.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();
    int clipTop = 0;

    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;
            }/*from  w ww. j a v a2  s.com*/

            if (checkDrawerViewAbsoluteGravity(v, Gravity.TOP)) {
                final int vbottom = v.getBottom();
                if (vbottom > clipTop)
                    clipTop = vbottom;
            } else 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, clipTop, 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 (mShadowLeftResolved != null && checkDrawerViewAbsoluteGravity(child, Gravity.LEFT)) {
        final int shadowWidth = mShadowLeftResolved.getIntrinsicWidth();
        final int childRight = child.getRight();
        final int drawerPeekDistance = mLeftDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) childRight / drawerPeekDistance, 1.f));
        mShadowLeftResolved.setBounds(childRight, child.getTop(), childRight + shadowWidth, child.getBottom());
        mShadowLeftResolved.setAlpha((int) (0xff * alpha));
        mShadowLeftResolved.draw(canvas);
    } else if (mShadowRightResolved != null && checkDrawerViewAbsoluteGravity(child, Gravity.RIGHT)) {
        final int shadowWidth = mShadowRightResolved.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));
        mShadowRightResolved.setBounds(childLeft - shadowWidth, child.getTop(), childLeft, child.getBottom());
        mShadowRightResolved.setAlpha((int) (0xff * alpha));
        mShadowRightResolved.draw(canvas);
    } else if (mShadowBottomResolved != null && checkDrawerViewAbsoluteGravity(child, Gravity.BOTTOM)) {
        final int shadowHeight = mShadowBottomResolved.getIntrinsicHeight();
        final int showing = getHeight() - child.getTop();
        final int drawerPeekDistance = mRightDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) showing / drawerPeekDistance, 1.f));
        mShadowBottomResolved.setBounds(child.getLeft(), child.getTop() - shadowHeight, child.getRight(),
                child.getBottom());
        mShadowBottomResolved.setAlpha((int) (0xff * alpha));
        mShadowBottomResolved.draw(canvas);
    }
    return result;
}

From source file:com.hainva.feedlynavigation.FeedlyViewPager.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    boolean needsInvalidate = false;

    final int overScrollMode = ViewCompat.getOverScrollMode(this);
    if (overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS
            || (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mTopEdge.isFinished()) {
            final int height = getHeight();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();

            mTopEdge.setSize(width, height);

            needsInvalidate |= mTopEdge.draw(canvas);
        }/*w  ww  .j  a v a 2  s  . com*/
        if (!mBottomEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();
            canvas.rotate(180);
            canvas.translate(-width, -(mLastOffset + 1) * height);

            mBottomEdge.setSize(width, height);
            needsInvalidate |= mBottomEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    } else {
        mTopEdge.finish();
        mBottomEdge.finish();
    }

    if (needsInvalidate) {
        // Keep animating
        ViewCompat.postInvalidateOnAnimation(this);
    }
}

From source file:com.example.sky.test.view.ViewPager.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    boolean needsInvalidate = false;

    final int overScrollMode = getOverScrollMode();
    if (overScrollMode == View.OVER_SCROLL_ALWAYS || (overScrollMode == View.OVER_SCROLL_IF_CONTENT_SCROLLS
            && mAdapter != null && mAdapter.getCount() > 1)) {
        if (!mLeftEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int width = getWidth();

            canvas.rotate(270);//from w w w .  j a  v  a2  s .  c  o  m
            canvas.translate(-height + getPaddingTop(), mFirstOffset * width);
            mLeftEdge.setSize(height, width);
            needsInvalidate |= mLeftEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!mRightEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();

            canvas.rotate(90);
            canvas.translate(-getPaddingTop(), -(mLastOffset + 1) * width);
            mRightEdge.setSize(height, width);
            needsInvalidate |= mRightEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    } else {
        mLeftEdge.finish();
        mRightEdge.finish();
    }

    if (needsInvalidate) {
        // Keep animating
        ViewCompat.postInvalidateOnAnimation(this);
    }
}

From source file:com.av.remusic.widget.RoundViewPager.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    boolean needsInvalidate = false;
    boolean isScroll = false;
    final int overScrollMode = ViewCompat.getOverScrollMode(this);
    if (overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS
            || (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mLeftEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int width = getWidth();

            canvas.rotate(270);//  w  ww  . j  a  v a 2  s . c  o m
            canvas.translate(-height + getPaddingTop(), mFirstOffset * width);
            mLeftEdge.setSize(height, width);
            needsInvalidate |= mLeftEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!mRightEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();

            canvas.rotate(90);
            canvas.translate(-getPaddingTop(), -(mLastOffset + 1) * width);
            mRightEdge.setSize(height, width);
            needsInvalidate |= mRightEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    } else {
        mLeftEdge.finish();
        mRightEdge.finish();
    }

    if (needsInvalidate) {
        // Keep animating
        ViewCompat.postInvalidateOnAnimation(this);
    }
}

From source file:t3.giftbook.util.FlipViewPager.java

@Override
public void draw(Canvas canvas) {
    if (mOffset == 0) {
        super.draw(canvas);
    }/*  ww  w .ja  v a  2s  . c  o m*/

    boolean needsInvalidate = false;

    final int overScrollMode = ViewCompat.getOverScrollMode(this);
    if (overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS
            || (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mLeftEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int width = getWidth();

            canvas.rotate(270);
            canvas.translate(-height + getPaddingTop(), mFirstOffset * width);
            mLeftEdge.setSize(height, width);
            needsInvalidate |= mLeftEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!mRightEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();

            canvas.rotate(90);
            canvas.translate(-getPaddingTop(), -(mLastOffset + 1) * width);
            mRightEdge.setSize(height, width);
            needsInvalidate |= mRightEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    } else {
        mLeftEdge.finish();
        mRightEdge.finish();
    }

    if (needsInvalidate) {
        // Keep animating
        ViewCompat.postInvalidateOnAnimation(this);
    }

    drawFlippingPage(canvas);

}

From source file:example.luojing.androidsourceanalysis.ViewPager.java

/**
 * ?????/*  w w w  .  j a v a  2 s . co  m*/
 */
@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    boolean needsInvalidate = false;

    final int overScrollMode = getOverScrollMode();
    if (overScrollMode == View.OVER_SCROLL_ALWAYS || (overScrollMode == View.OVER_SCROLL_IF_CONTENT_SCROLLS
            && mAdapter != null && mAdapter.getCount() > 1)) {
        if (!mLeftEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int width = getWidth();

            canvas.rotate(270);
            canvas.translate(-height + getPaddingTop(), mFirstOffset * width);
            mLeftEdge.setSize(height, width);
            needsInvalidate |= mLeftEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!mRightEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();

            canvas.rotate(90);
            canvas.translate(-getPaddingTop(), -(mLastOffset + 1) * width);
            mRightEdge.setSize(height, width);
            needsInvalidate |= mRightEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    } else {
        mLeftEdge.finish();
        mRightEdge.finish();
    }

    if (needsInvalidate) {
        // Keep animating
        ViewCompat.postInvalidateOnAnimation(this);
    }
}

From source file:com.hippo.vector.VectorDrawable.java

@Override
@SuppressWarnings("deprecation")
public void draw(Canvas canvas) {
    // We will offset the bounds for drawBitmap, so copyBounds() here instead
    // of getBounds().
    copyBounds(mTmpBounds);/*from   ww  w.jav  a  2s .  c o m*/
    if (mTmpBounds.width() <= 0 || mTmpBounds.height() <= 0) {
        // Nothing to draw
        return;
    }

    // Color filters always override tint filters.
    final ColorFilter colorFilter = (mColorFilter == null ? mTintFilter : mColorFilter);

    // The imageView can scale the canvas in different ways, in order to
    // avoid blurry scaling, we have to draw into a bitmap with exact pixel
    // size first. This bitmap size is determined by the bounds and the
    // canvas scale.
    canvas.getMatrix(mTmpMatrix);
    mTmpMatrix.getValues(mTmpFloats);
    float canvasScaleX = Math.abs(mTmpFloats[Matrix.MSCALE_X]);
    float canvasScaleY = Math.abs(mTmpFloats[Matrix.MSCALE_Y]);
    int scaledWidth = (int) (mTmpBounds.width() * canvasScaleX);
    int scaledHeight = (int) (mTmpBounds.height() * canvasScaleY);
    scaledWidth = Math.min(MAX_CACHED_BITMAP_SIZE, scaledWidth);
    scaledHeight = Math.min(MAX_CACHED_BITMAP_SIZE, scaledHeight);

    if (scaledWidth <= 0 || scaledHeight <= 0) {
        return;
    }

    final int saveCount = canvas.save();
    canvas.translate(mTmpBounds.left, mTmpBounds.top);

    // Handle RTL mirroring.
    final boolean needMirroring = needMirroring();
    if (needMirroring) {
        canvas.translate(mTmpBounds.width(), 0);
        canvas.scale(-1.0f, 1.0f);
    }

    // At this point, canvas has been translated to the right position.
    // And we use this bound for the destination rect for the drawBitmap, so
    // we offset to (0, 0);
    mTmpBounds.offsetTo(0, 0);

    mVectorState.createCachedBitmapIfNeeded(scaledWidth, scaledHeight);
    if (!mAllowCaching) {
        mVectorState.updateCachedBitmap(scaledWidth, scaledHeight);
    } else {
        if (!mVectorState.canReuseCache()) {
            mVectorState.updateCachedBitmap(scaledWidth, scaledHeight);
            mVectorState.updateCacheStates();
        }
    }
    mVectorState.drawCachedBitmapWithRootAlpha(canvas, colorFilter, mTmpBounds);
    canvas.restoreToCount(saveCount);
}

From source file:com.lab47billion.appchooser.HorizontalPicker.java

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

    int saveCount = canvas.getSaveCount();
    canvas.save();//from w w w . j a va2  s  .  c o m

    int selectedItem = mSelectedItem;

    float itemWithPadding = mItemWidth + mDividerSize;

    // translate horizontal to center
    canvas.translate(itemWithPadding * mSideItems, 0);

    if (mValues != null) {
        for (int i = 0; i < mValues.length; i++) {

            mTextPaint.setColor(getTextColor(i));

            // get text layout
            View layout = mLayouts[i][0];
            //                layout.draw(canvas);
            //NEW
            View layoutSub = mLayouts[i][1];
            int saveCountHeight = canvas.getSaveCount();
            canvas.save();

            float x = 0;
            float lineWidth = 40f;
            if (lineWidth > mItemWidth) {
                if (isRtl(mValues[i])) {
                    x += (lineWidth - mItemWidth) / 2;
                } else {
                    x -= (lineWidth - mItemWidth) / 2;
                }
            }

            /*set select item top padding */
            int yTranslate;
            if (!isScrollingStart && i == selectedItem) {
                mTextPaint.setTextSize(mSelectedTextSize);
            } else {
                mTextPaint.setTextSize(mNormalTextSize);
            }

            // translate vertically to center
            if (layoutSub != null) {
                yTranslate = (canvas.getHeight() - (layout.getHeight() + layoutSub.getHeight())) / 2;
            } else {
                yTranslate = (canvas.getHeight() - layout.getHeight()) / 2;
            }

            if (i == selectedItem && topPadding != -1) {
                yTranslate = topPadding;
            }

            canvas.translate(-x, yTranslate);

            RectF clipBounds;
            if (x == 0) {
                clipBounds = mItemClipBounds;
            } else {
                clipBounds = mItemClipBoundsOffser;
                clipBounds.set(mItemClipBounds);
                clipBounds.offset(x, 0);
            }
            canvas.clipRect(clipBounds);
            layout.draw(canvas);

            if (layoutSub != null) {
                Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
                int heightText = (int) (Math.abs(fontMetrics.ascent) + Math.abs(fontMetrics.descent));
                canvas.translate(-x, (layout.getHeight() + heightText - getTopPadding()) / 2);
                canvas.clipRect(clipBounds);
                layoutSub.draw(canvas);
            }

            // restore vertical translation
            canvas.restoreToCount(saveCountHeight);

            // translate horizontal for 1 item
            canvas.translate(itemWithPadding, 0);
        }
    }

    // restore horizontal translation
    canvas.restoreToCount(saveCount);

    drawEdgeEffect(canvas, mLeftEdgeEffect, 270);
    drawEdgeEffect(canvas, mRightEdgeEffect, 90);
}