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:android.car.ui.provider.CarDrawerLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final int height = getHeight();
    final boolean drawingContent = isContentView(child);
    int clipLeft = findContentView().getLeft();
    int clipRight = findContentView().getRight();
    final int baseAlpha = (mScrimColor & 0xff000000) >>> 24;

    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 v 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 (drawingContent) {
        int scrimAlpha = SCRIM_ENABLED
                ? (int) (baseAlpha * Math.max(0, Math.min(1, onScreen())) * MAX_SCRIM_ALPHA)
                : 0;

        if (scrimAlpha > 0) {
            int color = scrimAlpha << 24 | (mScrimColor & 0xffffff);
            mScrimPaint.setColor(color);

            canvas.drawRect(clipLeft, 0, clipRight, getHeight(), mScrimPaint);

            canvas.drawRect(clipLeft - 1, 0, clipLeft, getHeight(), mEdgeHighlightPaint);
        }

        LayoutParams drawerLp = (LayoutParams) findDrawerView().getLayoutParams();
        if (mShadow != null && checkDrawerViewAbsoluteGravity(findDrawerView(), Gravity.LEFT)) {
            final int offScreen = (int) ((1 - drawerLp.onScreen) * findDrawerView().getWidth());
            final int drawerRight = getWidth() - drawerLp.getMarginEnd() - offScreen;
            final int shadowWidth = mShadow.getIntrinsicWidth();
            final float alpha = Math.max(0, Math.min((float) drawerRight / mDragger.getEdgeSize(), 1.f));
            mShadow.setBounds(drawerRight, child.getTop(), drawerRight + shadowWidth, child.getBottom());
            mShadow.setAlpha((int) (255 * alpha * alpha * alpha));
            mShadow.draw(canvas);
        } else if (mShadow != null && checkDrawerViewAbsoluteGravity(findDrawerView(), Gravity.RIGHT)) {
            final int onScreen = (int) (findDrawerView().getWidth() * drawerLp.onScreen);
            final int drawerLeft = drawerLp.getMarginStart() + getWidth() - onScreen;
            final int shadowWidth = mShadow.getIntrinsicWidth();
            final float alpha = Math.max(0, Math.min((float) onScreen / mDragger.getEdgeSize(), 1.f));
            canvas.save();
            canvas.translate(2 * drawerLeft - shadowWidth, 0);
            canvas.scale(-1.0f, 1.0f);
            mShadow.setBounds(drawerLeft - shadowWidth, child.getTop(), drawerLeft, child.getBottom());
            mShadow.setAlpha((int) (255 * alpha * alpha * alpha * alpha));
            mShadow.draw(canvas);
            canvas.restore();
        }
    }
    return result;
}

From source file:com.klinker.android.sliding.MultiShrinkScroller.java

/**
 * Draw all components on the screen./*  w  ww.j  a v a2  s.c  o  m*/
 * @param canvas the canvas to draw on.
 */
@Override
public void draw(Canvas canvas) {
    super.draw(canvas);

    final int width = getWidth() - getPaddingLeft() - getPaddingRight();
    final int height = getHeight();

    if (!edgeGlowBottom.isFinished()) {
        final int restoreCount = canvas.save();

        // Draw the EdgeEffect on the bottom of the Window (Or a little bit below the bottom
        // of the Window if we start to scroll upwards while EdgeEffect is visible). This
        // does not need to consider the case where this MultiShrinkScroller doesn't fill
        // the Window, since the nested ScrollView should be set to fillViewport.
        canvas.translate(-width + getPaddingLeft(), height + getMaximumScrollUpwards() - getScroll());

        canvas.rotate(180, width, 0);
        if (isTwoPanel) {
            // Only show the EdgeEffect on the bottom of the ScrollView.
            edgeGlowBottom.setSize(scrollView.getWidth(), height);
        } else {
            edgeGlowBottom.setSize(width, height);
        }
        if (edgeGlowBottom.draw(canvas)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                postInvalidateOnAnimation();
            } else {
                postInvalidate();
            }
        }
        canvas.restoreToCount(restoreCount);
    }

    if (!edgeGlowTop.isFinished()) {
        final int restoreCount = canvas.save();
        if (isTwoPanel) {
            edgeGlowTop.setSize(scrollView.getWidth(), height);
            canvas.translate(photoViewContainer.getWidth(), 0);
        } else {
            edgeGlowTop.setSize(width, height);
        }
        if (edgeGlowTop.draw(canvas)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                postInvalidateOnAnimation();
            } else {
                postInvalidate();
            }
        }
        canvas.restoreToCount(restoreCount);
    }
}

From source file:com.iangclifton.auid.horizontaliconview.HorizontalIconView.java

@Override
protected void onDraw(Canvas canvas) {
    if (mDrawables == null || mDrawables.isEmpty()) {
        return;//w  w w.  j  a  va 2  s  .  c o  m
    }

    final int width = getWidth();
    final int paddingBottom = getPaddingBottom();
    final int paddingLeft = getPaddingLeft();
    final int paddingTop = getPaddingTop();

    // Determine edges of visible content
    final int leftEdge = getScrollX();
    final int rightEdge = leftEdge + width;

    int left = paddingLeft;
    int top = paddingTop;
    mSkippedIconCount = 0;

    final int iconCount = mDrawables.size();
    for (int i = 0; i < iconCount; i++) {
        if (left + mIconSize < leftEdge) {
            // Icon is too far left to be seen
            left = left + mIconSize + mIconSpacing;
            mSkippedIconCount++;
            continue;
        }

        if (left > rightEdge) {
            // All remaining icons are right of the view
            break;
        }

        // Get a reference to the icon to be drawn
        final Drawable icon = mDrawables.get(i);
        icon.setBounds(left, top, left + mIconSize, top + mIconSize);
        icon.draw(canvas);

        // Icon was drawn, so track position
        final int drawnPosition = i - mSkippedIconCount;
        if (drawnPosition + 1 > mIconPositions.size()) {
            final Rect rect = icon.copyBounds();
            mIconPositions.add(rect);
        } else {
            final Rect rect = mIconPositions.get(drawnPosition);
            icon.copyBounds(rect);
        }

        // Update left position
        left = left + mIconSize + mIconSpacing;
    }

    if (mEdgeEffectLeft != null) {
        if (!mEdgeEffectLeft.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - paddingTop - paddingBottom;

            canvas.rotate(270);
            canvas.translate(-height + paddingTop, Math.min(0, leftEdge));
            mEdgeEffectLeft.setSize(height, getWidth());
            if (mEdgeEffectLeft.draw(canvas)) {
                postInvalidateOnAnimation();
            }
            canvas.restoreToCount(restoreCount);
        }
        if (!mEdgeEffectRight.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - paddingTop - paddingBottom;

            canvas.rotate(90);
            canvas.translate(-paddingTop, -(Math.max(mScrollRange, leftEdge) + width));
            mEdgeEffectRight.setSize(height, width);
            if (mEdgeEffectRight.draw(canvas)) {
                postInvalidateOnAnimation();
            }
            canvas.restoreToCount(restoreCount);
        }
    }
}

From source file:com.hippo.widget.BothScrollView.java

@SuppressWarnings("SuspiciousNameCombination")
@Override//from ww  w .j  av  a  2 s . co m
public void draw(Canvas canvas) {
    super.draw(canvas);
    if (mEdgeGlowLeft != null) {
        final int scrollX = getScrollX();
        final int scrollY = getScrollY();
        final int width = getWidth();
        final int height = getHeight();
        if (!mEdgeGlowLeft.isFinished()) {
            final int restoreCount = canvas.save();
            canvas.rotate(270);
            canvas.translate(-height - scrollY, Math.min(0, scrollX));
            mEdgeGlowLeft.setSize(height, width);
            if (mEdgeGlowLeft.draw(canvas)) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
            canvas.restoreToCount(restoreCount);
        }
        if (!mEdgeGlowRight.isFinished()) {
            final int restoreCount = canvas.save();
            canvas.rotate(90);
            canvas.translate(scrollY, -(Math.max(getHorizontalScrollRange(), scrollX) + width));
            mEdgeGlowRight.setSize(height, width);
            if (mEdgeGlowRight.draw(canvas)) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
            canvas.restoreToCount(restoreCount);
        }
        if (!mEdgeGlowTop.isFinished()) {
            final int restoreCount = canvas.save();
            canvas.translate(scrollX, Math.min(0, scrollY));
            mEdgeGlowTop.setSize(width, height);
            if (mEdgeGlowTop.draw(canvas)) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
            canvas.restoreToCount(restoreCount);
        }
        if (!mEdgeGlowBottom.isFinished()) {
            final int restoreCount = canvas.save();
            canvas.translate(-width + scrollX, Math.max(getVerticalScrollRange(), scrollY) + height);
            canvas.rotate(180, width, 0);
            mEdgeGlowBottom.setSize(width, height);
            if (mEdgeGlowBottom.draw(canvas)) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
            canvas.restoreToCount(restoreCount);
        }
    }
}

From source file:de.andacaydin.bidirectionalviewpagerlibrary.BiDirectionalViewPager.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);/*from w w  w . j  av  a  2s  .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.aidy.bottomdrawerlayout.BottomDrawerLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    Log.i(TAG, "drawChild()");
    final int width = getWidth();
    final boolean drawingContent = isContentView(child);
    int clipTop = 0;
    int clipBottom = getHeight();

    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.getWidth() < width) {
                Log.i(TAG, "drawChild() -- 0");
                continue;
            }//from   ww  w .  j a v  a  2  s.  co  m
            if (checkDrawerViewAbsoluteGravity(v, Gravity.TOP)) {
                final int vbottom = v.getBottom();
                if (vbottom > clipTop)
                    clipTop = vbottom;
            } else {
                final int vtop = v.getTop();
                if (vtop < clipBottom) {
                    clipBottom = vtop;
                }
            }
        }
        canvas.clipRect(0, clipTop, getWidth(), clipBottom);
    }
    final boolean result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(restoreCount);

    if (mScrimOpacity > 0 && drawingContent) {
        Log.i(TAG, "drawChild() -- 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(0, clipTop, getWidth(), clipBottom, mScrimPaint);
    } else if (mShadowBottom != null && checkDrawerViewAbsoluteGravity(child, Gravity.BOTTOM)) {
        Log.i(TAG, "drawChild() -- Gravity.BOTTOM");
        final int shadowHeight = mShadowBottom.getIntrinsicWidth();
        final int childTop = child.getTop();
        final int showing = getHeight() - childTop;
        final int drawerPeekDistance = mBottomDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) showing / drawerPeekDistance, 1.f));
        mShadowBottom.setBounds(child.getLeft(), childTop - shadowHeight, child.getRight(), childTop);
        mShadowBottom.setAlpha((int) (0xff * alpha));
        mShadowBottom.draw(canvas);
    }
    return result;
}

From source file:org.immopoly.android.widget.ViewPager.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();

            canvas.rotate(270);/*from   w w w  . jav  a2 s  .com*/
            canvas.translate(-height + getPaddingTop(), 0);
            mLeftEdge.setSize(height, getWidth());
            needsInvalidate |= mLeftEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!mRightEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = forcedChildWidth > 0 ? forcedChildWidth : getWidth();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int itemCount = mAdapter != null ? mAdapter.getCount() : 1;

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

    if (needsInvalidate) {
        // Keep animating
        invalidate();
    }
}

From source file:beichen.douban.ui.view.LazyViewPager.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();

            canvas.rotate(270);/*w  ww . j a va 2 s  .c  om*/
            canvas.translate(-height + getPaddingTop(), 0);
            mLeftEdge.setSize(height, getWidth());
            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();
            final int itemCount = mAdapter != null ? mAdapter.getCount() : 1;

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

    if (needsInvalidate) {
        // Keep animating
        invalidate();
    }
}

From source file:com.klinker.android.launcher.addons.view.LauncherDrawerLayout.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;
            }/*from   w  ww  . j a  v a2s.  co  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 (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;
}

From source file:com.grottworkshop.gwsvectorsandboxlib.VectorDrawable.java

@Override
public void draw(Canvas canvas) {
    final Rect bounds = getBounds();
    if (bounds.width() == 0 || bounds.height() == 0) {
        // too small to draw
        return;//from   ww w  . j  av  a2 s. co  m
    }

    final int saveCount = canvas.save();
    final boolean needMirroring = needMirroring();

    canvas.translate(bounds.left, bounds.top);
    if (needMirroring) {
        canvas.translate(bounds.width(), 0);
        canvas.scale(-1.0f, 1.0f);
    }

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

    if (!mAllowCaching) {
        // AnimatedVectorDrawable
        if (!mVectorState.hasTranslucentRoot()) {
            mVectorState.mVPathRenderer.draw(canvas, bounds.width(), bounds.height(), colorFilter);
        } else {
            mVectorState.createCachedBitmapIfNeeded(bounds);
            mVectorState.updateCachedBitmap(bounds);
            mVectorState.drawCachedBitmapWithRootAlpha(canvas, colorFilter);
        }
    } else {
        // Static Vector Drawable case.
        mVectorState.createCachedBitmapIfNeeded(bounds);
        if (!mVectorState.canReuseCache()) {
            mVectorState.updateCachedBitmap(bounds);
            mVectorState.updateCacheStates();
        }
        mVectorState.drawCachedBitmapWithRootAlpha(canvas, colorFilter);
    }

    canvas.restoreToCount(saveCount);
}