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:com.microhealthllc.Slide.MultiShrinkScroller.java

/**
 * Draw all components on the screen.//from  w  ww .  j  a  va2s .  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);
        }

        // todo: figure out what is wrong with the edge glow with padded layouts
        if (paddedLayout) {
            edgeGlowBottom.setSize(0, 0);
        }

        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() * (1 / 6), 0);
        } else {
            edgeGlowTop.setSize(width, height);
        }

        // todo: figure out what is wrong with the edge glow with padded layouts
        if (paddedLayout) {
            edgeGlowTop.setSize(0, 0);
        }

        if (edgeGlowTop.draw(canvas)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                postInvalidateOnAnimation();
            } else {
                postInvalidate();
            }
        }
        canvas.restoreToCount(restoreCount);
    }
}

From source file:cn.emagsoftware.ui.BugFixedSlidingPaneLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    boolean result;
    final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);

    if (mCanSlide && !lp.slideable && mSlideableView != null) {
        // Clip against the slider; no sense drawing what will immediately be covered.
        canvas.getClipBounds(mTmpRect);/*from   ww  w.ja va 2s  .c o m*/
        mTmpRect.right = Math.min(mTmpRect.right, mSlideableView.getLeft());
        canvas.clipRect(mTmpRect);
    }

    if (Build.VERSION.SDK_INT >= 11) { // HC
        result = super.drawChild(canvas, child, drawingTime);
    } else {
        if (lp.dimWhenOffset && mSlideOffset > 0) {
            if (!child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(true);
            }
            final Bitmap cache = child.getDrawingCache();
            if (cache != null) {
                canvas.drawBitmap(cache, child.getLeft(), child.getTop(), lp.dimPaint);
                result = false;
            } else {
                Log.e(TAG, "drawChild: child view " + child + " returned null drawing cache");
                result = super.drawChild(canvas, child, drawingTime);
            }
        } else {
            if (child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(false);
            }
            result = super.drawChild(canvas, child, drawingTime);
        }
    }

    canvas.restoreToCount(save);

    return result;
}

From source file:com.example.verticaldrawerlayout.VerticalDrawerLayout.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  w  w  . jav a2  s .com

            if (checkDrawerViewAbsoluteGravity(v, Gravity.TOP)) {
                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 (mShadowTop != null && checkDrawerViewAbsoluteGravity(child, Gravity.TOP)) {
        final int shadowWidth = mShadowTop.getIntrinsicWidth();
        final int childRight = child.getRight();
        final int drawerPeekDistance = mTopDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) childRight / drawerPeekDistance, 1.f));
        mShadowTop.setBounds(childRight, child.getTop(), childRight + shadowWidth, child.getBottom());
        mShadowTop.setAlpha((int) (0xff * alpha));
        mShadowTop.draw(canvas);
    } else if (mShadowBottom != null && checkDrawerViewAbsoluteGravity(child, Gravity.BOTTOM)) {
        final int shadowWidth = mShadowBottom.getIntrinsicWidth();
        final int childLeft = child.getLeft();
        final int showing = getWidth() - childLeft;
        final int drawerPeekDistance = mBottomDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) showing / drawerPeekDistance, 1.f));
        mShadowBottom.setBounds(childLeft - shadowWidth, child.getTop(), childLeft, child.getBottom());
        mShadowBottom.setAlpha((int) (0xff * alpha));
        mShadowBottom.draw(canvas);
    }
    return result;
}

From source file:com.sanron.sunweather.view.v4.DrawerLayout.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;
            }/* ww  w . j a  v  a2 s.  c o 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 (mShadowLeft != null && checkDrawerViewAbsoluteGravity(child, Gravity.LEFT)) {
        final int shadowWidth = mShadowLeft.getIntrinsicWidth();
        final int childRight = child.getRight();
        mShadowLeft.setBounds(childRight, child.getTop(), childRight + shadowWidth, child.getBottom());
        mShadowLeft.draw(canvas);
    } else if (mShadowRight != null && checkDrawerViewAbsoluteGravity(child, Gravity.RIGHT)) {
        final int shadowWidth = mShadowRight.getIntrinsicWidth();
        final int childLeft = child.getLeft();
        mShadowRight.setBounds(childLeft - shadowWidth, child.getTop(), childLeft, child.getBottom());
        mShadowRight.draw(canvas);
    }
    return result;
}

From source file:com.aidy.bottomdrawerlayout.DrawerLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    Log.i(TAG, "drawChild()");
    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) {
                Log.i(TAG, "drawChild() -- 0");
                continue;
            }//  w ww  .  j av  a 2 s .c om

            if (checkDrawerViewAbsoluteGravity(v, Gravity.LEFT)) {
                Log.i(TAG, "drawChild() -- 1");
                final int vright = v.getRight();
                if (vright > clipLeft)
                    clipLeft = vright;
            } else {
                Log.i(TAG, "drawChild() -- 2");
                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) {
        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(clipLeft, 0, clipRight, getHeight(), mScrimPaint);
    } else if (mShadowLeft != null && checkDrawerViewAbsoluteGravity(child, Gravity.LEFT)) {
        Log.i(TAG, "drawChild() -- 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)) {
        Log.i(TAG, "drawChild() -- 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:ca.co.rufus.androidboilerplate.ui.DebugDrawerLayout.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;
            }// ww  w. ja v a  2s  .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 (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.ylw.split.splitview.view.SplitView.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    boolean result;
    final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);

    //        if (mCanSlide && !lp.slideable && mSlideableView != null) {
    //            // Clip against the slider; no sense drawing what will immediately be covered.
    //            canvas.getClipBounds(mTmpRect);
    //            if (isLayoutRtlSupport()) {
    //                mTmpRect.left = Math.max(mTmpRect.left, mSlideableView.getRight());
    //            } else {
    //                mTmpRect.right = Math.min(mTmpRect.right, mSlideableView.getLeft());
    //            }
    //            canvas.clipRect(mTmpRect);
    //        }//from  w  w w . j  a  va2s  . com

    if (Build.VERSION.SDK_INT >= 11) { // HC
        result = super.drawChild(canvas, child, drawingTime);
    } else {
        if (lp.dimWhenOffset && mSlideOffset > 0) {
            if (!child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(true);
            }
            final Bitmap cache = child.getDrawingCache();
            if (cache != null) {
                canvas.drawBitmap(cache, child.getLeft(), child.getTop(), lp.dimPaint);
                result = false;
            } else {
                Log.e(TAG, "drawChild: child view " + child + " returned null drawing cache");
                result = super.drawChild(canvas, child, drawingTime);
            }
        } else {
            if (child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(false);
            }
            result = super.drawChild(canvas, child, drawingTime);
        }
    }

    canvas.restoreToCount(save);

    return result;
}

From source file:com.aigo.kt03airdemo.ui.view.ResideLayout.java

@Override
protected boolean drawChild(@NonNull Canvas canvas, @NonNull View child, long drawingTime) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    boolean result;
    final int save = canvas.save(Canvas.ALL_SAVE_FLAG);

    if (mCanSlide && !lp.slideable && mSlideableView != null) {
        // Clip against the slider; no sense drawing what will immediately be covered.
        canvas.scale(1.2f - 0.2f * mSlideOffset, 1.2f - 0.2f * mSlideOffset, child.getRight(), getHeight() / 2);
    } else {//from w ww . ja  va  2 s  .com
        assert mSlideableView != null;
        canvas.scale(1 - mSlideOffset / 3, 1 - mSlideOffset / 3, mSlideableView.getLeft(), getHeight() / 2);
        ViewCompat.setRotationY(child, -10 * mSlideOffset);
    }

    if (!lp.slideable && mSlideOffset == 0) {
        result = true;
    } else {
        if (Build.VERSION.SDK_INT >= 11) { // HC
            result = super.drawChild(canvas, child, drawingTime);
        } else {
            if (lp.dimWhenOffset && mSlideOffset > 0) {
                if (!child.isDrawingCacheEnabled()) {
                    child.setDrawingCacheEnabled(true);
                }
                final Bitmap cache = child.getDrawingCache();
                if (cache != null) {
                    canvas.drawBitmap(cache, child.getLeft(), child.getTop(), lp.dimPaint);
                    result = false;
                } else {
                    Log.e(TAG, "drawChild: child view " + child + " returned null drawing cache");
                    result = super.drawChild(canvas, child, drawingTime);
                }
            } else {
                if (child.isDrawingCacheEnabled()) {
                    child.setDrawingCacheEnabled(false);
                }
                result = super.drawChild(canvas, child, drawingTime);
            }
        }
    }

    canvas.restoreToCount(save);

    return result;
}

From source file:com.shizhefei.view.largeimage.LargeImageView.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (getMeasuredWidth() == 0 || getMeasuredHeight() == 0) {
        return;//from   ww w  . j  a v a 2s  .com
    }
    if (!hasLoad()) {
        return;
    }
    int drawOffsetX = 0;
    int drawOffsetY = 0;
    int contentWidth = getContentWidth();
    int contentHeight = getContentHeight();
    int layoutWidth = getMeasuredWidth();
    int layoutHeight = getMeasuredHeight();
    if (layoutWidth > contentWidth) {
        drawOffsetX = (layoutWidth - contentWidth) / 2;
    }
    if (layoutHeight > contentHeight) {
        drawOffsetY = (layoutHeight - contentHeight) / 2;
    }
    if (mDrawable != null) {
        mDrawable.setBounds(drawOffsetX, drawOffsetY, drawOffsetX + contentWidth, drawOffsetY + contentHeight);
        mDrawable.draw(canvas);
    } else if (mFactory != null) {
        int mOffsetX = 0;
        int mOffsetY = 0;
        int left = getScrollX();
        int right = left + getMeasuredWidth();
        int top = getScrollY();
        int bottom = top + getMeasuredHeight();
        float width = mScale * getWidth();
        float imgWidth = imageBlockLoader.getWidth();

        float imageScale = imgWidth / width;

        // ?
        imageRect.left = (int) Math.ceil((left - mOffsetX) * imageScale);
        imageRect.top = (int) Math.ceil((top - mOffsetY) * imageScale);
        imageRect.right = (int) Math.ceil((right - mOffsetX) * imageScale);
        imageRect.bottom = (int) Math.ceil((bottom - mOffsetY) * imageScale);

        List<BlockImageLoader.DrawData> drawData = imageBlockLoader.getDrawData(imageScale, imageRect);

        int saveCount = canvas.save();
        for (BlockImageLoader.DrawData data : drawData) {
            Rect drawRect = data.imageRect;
            drawRect.left = (int) (Math.ceil(drawRect.left / imageScale) + mOffsetX) + drawOffsetX;
            drawRect.top = (int) (Math.ceil(drawRect.top / imageScale) + mOffsetY) + drawOffsetY;
            drawRect.right = (int) (Math.ceil(drawRect.right / imageScale) + mOffsetX) + drawOffsetX;
            drawRect.bottom = (int) (Math.ceil(drawRect.bottom / imageScale) + mOffsetY) + drawOffsetY;
            canvas.drawBitmap(data.bitmap, data.srcRect, drawRect, null);
        }
        canvas.restoreToCount(saveCount);
    }
}

From source file:rp3.widget.SlidingPaneLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    boolean result;
    final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);

    if (mCanSlide && !lp.slideable && mSlideableView != null) {
        // Clip against the slider; no sense drawing what will immediately
        // be covered.
        canvas.getClipBounds(mTmpRect);/*from w  w w.jav  a  2  s  . c o m*/
        mTmpRect.right = Math.min(mTmpRect.right, mSlideableView.getLeft());
        canvas.clipRect(mTmpRect);
    }

    if (Build.VERSION.SDK_INT >= 11) { // HC
        result = super.drawChild(canvas, child, drawingTime);
    } else {
        if (lp.dimWhenOffset && mSlideOffset > 0) {
            if (!child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(true);
            }
            final Bitmap cache = child.getDrawingCache();
            if (cache != null) {
                canvas.drawBitmap(cache, child.getLeft(), child.getTop(), lp.dimPaint);
                result = false;
            } else {
                Log.e(TAG, "drawChild: child view " + child + " returned null drawing cache");
                result = super.drawChild(canvas, child, drawingTime);
            }
        } else {
            if (child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(false);
            }
            result = super.drawChild(canvas, child, drawingTime);
        }
    }

    canvas.restoreToCount(save);

    return result;
}