Example usage for android.graphics Canvas save

List of usage examples for android.graphics Canvas save

Introduction

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

Prototype

public int save() 

Source Link

Document

Saves the current matrix and clip onto a private stack.

Usage

From source file:com.ruesga.timelinechart.TimelineChartView.java

private void drawEdgeEffects(Canvas c) {
    boolean needsInvalidate = false;

    if (mEdgeEffectLeft != null && !mEdgeEffectLeft.isFinished()) {
        final int restoreCount = c.save();
        c.rotate(270);/*from  ww w. j a  va2 s .  c o m*/
        c.translate(-mGraphArea.height() - mGraphArea.top, mGraphArea.left);
        mEdgeEffectLeft.setSize((int) mGraphArea.height(), (int) mGraphArea.width());
        needsInvalidate = mEdgeEffectLeft.draw(c);
        c.restoreToCount(restoreCount);
    }

    if (mEdgeEffectRight != null && !mEdgeEffectRight.isFinished()) {
        final int restoreCount = c.save();
        c.rotate(90);
        c.translate(mGraphArea.top, -getWidth() + (getWidth() - mGraphArea.right));
        mEdgeEffectRight.setSize((int) mGraphArea.height(), (int) mGraphArea.width());
        needsInvalidate |= mEdgeEffectRight.draw(c);
        c.restoreToCount(restoreCount);
    }

    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }
}

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   www  .j  a va  2  s  . c om*/
    }
    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:com.hippo.widget.BothScrollView.java

@SuppressWarnings("SuspiciousNameCombination")
@Override//from  w  w  w  . java 2  s .c  o 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: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);//w w w . j  av a 2 s.c  o m
            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:de.uni_weimar.mheinz.androidtouchscope.display.HandleView.java

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

    if (mIsOn) {//w w w. j av  a  2s.  c om
        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.huangj.huangjlibrary.widget.drawerlayout.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;
            }//from   www .ja v  a2  s  .com

            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 (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);
    }
    return result;
}

From source file:am.widget.indicatortabstrip.IndicatorTabStrip.java

/**
 * /*from   w  w  w  . j  av a  2  s .com*/
 *
 * @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:org.chromium.chrome.browser.toolbar.ToolbarPhone.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    if (child == mLocationBar)
        return drawLocationBar(canvas, drawingTime);
    boolean clipped = false;

    if (mLocationBarBackground != null
            && ((mTabSwitcherState == STATIC_TAB && !mTabSwitcherModeViews.contains(child))
                    || (mTabSwitcherState != STATIC_TAB && mBrowsingModeViews.contains(child)))) {
        canvas.save();

        int translationY = (int) mLocationBar.getTranslationY();
        int clipTop = mLocationBarBackgroundBounds.top - mLocationBarBackgroundPadding.top + translationY;
        if (mUrlExpansionPercent != 0f && clipTop < child.getBottom()) {
            // For other child views, use the inverse clipping of the URL viewport.
            // Only necessary during animations.
            // Hardware mode does not support unioned clip regions, so clip using the
            // appropriate bounds based on whether the child is to the left or right of the
            // location bar.
            boolean isLeft = (child == mNewTabButton || child == mHomeButton) ^ LocalizationUtils.isLayoutRtl();

            int clipBottom = mLocationBarBackgroundBounds.bottom + mLocationBarBackgroundPadding.bottom
                    + translationY;//from   w ww.j  a  v a2s .  c o  m
            boolean verticalClip = false;
            if (translationY > 0f) {
                clipTop = child.getTop();
                clipBottom = clipTop;
                verticalClip = true;
            }

            if (isLeft) {
                int clipRight = verticalClip ? child.getMeasuredWidth()
                        : mLocationBarBackgroundBounds.left - mLocationBarBackgroundPadding.left;
                canvas.clipRect(0, clipTop, clipRight, clipBottom);
            } else {
                int clipLeft = verticalClip ? 0
                        : mLocationBarBackgroundBounds.right + mLocationBarBackgroundPadding.right;
                canvas.clipRect(clipLeft, clipTop, getMeasuredWidth(), clipBottom);
            }
        }
        clipped = true;
    }
    boolean retVal = super.drawChild(canvas, child, drawingTime);
    if (clipped)
        canvas.restore();
    return retVal;
}

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);//from  w  ww .j  a v a 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.huewu.pla.lib.internal.PLAListView.java

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

    canvas.save();
    canvas.clipRect(bounds);/* w  w  w .j  ava2  s .c  o m*/

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

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

    canvas.restore();
}