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

/**
 * /*w  ww. j a va  2  s.c  o m*/
 *
 * @param canvas     
 * @param position   ???
 * @param itemWidth  ?
 * @param itemHeight ?
 */
protected void drawItemBackground(Canvas canvas, int position, int itemWidth, int itemHeight) {
    if (!hasItemBackgrounds())
        return;
    Drawable tag = getItemBackground(position);
    if (position == getItemCount() - 1) {
        int restWidth = getLastItemWidth();
        tag.setBounds(0, 0, restWidth, itemHeight);
    } else {
        tag.setBounds(0, 0, itemWidth, itemHeight);
    }
    final float moveX = ViewCompat.getPaddingStart(this) + (itemWidth + getIntervalWidth()) * position;
    final float moveY = getPaddingTop();
    canvas.save();
    canvas.translate(moveX, moveY);
    tag.draw(canvas);
    canvas.restore();
}

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 w w  w . j av  a 2s.  com*/
    }

    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);
}

From source file:com.microhealthllc.Slide.MultiShrinkScroller.java

/**
 * Draw all components on the screen.//from www  .j a v a2s .com
 *
 * @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:com.example.mylibrary.utils.decoration.StickyHeaderDecoration.java

/**
 * {@inheritDoc}//from w  w w.j  a v a  2 s. c o m
 */
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    final int count = parent.getChildCount();
    long previousHeaderId = -1;

    for (int layoutPos = 0; layoutPos < count; layoutPos++) {
        final View child = parent.getChildAt(layoutPos);
        int adapterPos = parent.getChildAdapterPosition(child);

        if (!mIncludeHeader) {
            if (parent.getAdapter() instanceof RecyclerArrayAdapter) {
                int headerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getHeaderCount();
                int footerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getFooterCount();
                int dataCount = ((RecyclerArrayAdapter) parent.getAdapter()).getCount();
                if (adapterPos < headerCount) {
                    continue;
                }
                if (adapterPos >= headerCount + dataCount) {
                    continue;
                }
                if (adapterPos >= headerCount) {
                    adapterPos -= headerCount;
                }

            }
        }

        if (adapterPos != RecyclerView.NO_POSITION && hasHeader(adapterPos)) {
            long headerId = mAdapter.getHeaderId(adapterPos);

            if (headerId != previousHeaderId) {
                previousHeaderId = headerId;
                View header = getHeader(parent, adapterPos).itemView;
                canvas.save();

                final int left = child.getLeft();
                final int top = getHeaderTop(parent, child, header, adapterPos, layoutPos);
                canvas.translate(left, top);

                header.setTranslationX(left);
                header.setTranslationY(top);
                header.draw(canvas);
                canvas.restore();
            }
        }
    }
}

From source file:com.pdftron.pdf.tools.Tool.java

@Override
public boolean onDrawEdgeEffects(Canvas canvas, int width, int verticalOffset) {
    boolean needsInvalidate = false;

    if (!mEdgeEffectLeft.isFinished()) {
        canvas.save();
        try {/*from  ww w  . j av  a 2 s .  c o m*/
            canvas.translate(0, canvas.getHeight() + verticalOffset);
            canvas.rotate(-90, 0, 0);
            mEdgeEffectLeft.setSize(canvas.getHeight(), canvas.getWidth());
            if (mEdgeEffectLeft.draw(canvas)) {
                needsInvalidate = true;
            }
        } finally {
            canvas.restore();
        }
    }

    if (!mEdgeEffectRight.isFinished()) {
        canvas.save();
        try {
            canvas.translate(width, verticalOffset);
            canvas.rotate(90, 0, 0);
            mEdgeEffectRight.setSize(canvas.getHeight(), canvas.getWidth());
            if (mEdgeEffectRight.draw(canvas)) {
                needsInvalidate = true;
            }
        } finally {
            canvas.restore();
        }
    }
    return needsInvalidate;
}

From source file:com.actionbarsherlock.custom.widget.VerticalDrawerLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final int width = getWidth();
    final boolean drawingContent = isContentView(child);
    int clipTop = 0, 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) {
                continue;
            }/* w  w w .  jav  a2s  .co m*/

            if (checkDrawerViewGravity(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) {
        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 (mShadowTop != null && checkDrawerViewGravity(child, Gravity.TOP)) {
        final int shadowHeight = mShadowTop.getIntrinsicHeight();
        final int childBottom = child.getBottom();
        final int drawerPeekDistance = mTopDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) childBottom / drawerPeekDistance, 1.f));
        mShadowTop.setBounds(child.getLeft(), childBottom, child.getRight(), childBottom + shadowHeight);
        mShadowTop.setAlpha((int) (0xff * alpha));
        mShadowTop.draw(canvas);
    } else if (mShadowBottom != null && checkDrawerViewGravity(child, Gravity.BOTTOM)) {
        final int shadowHeight = mShadowBottom.getIntrinsicHeight();
        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));
        mShadowTop.setBounds(child.getLeft(), childTop - shadowHeight, child.getRight(), childTop);
        mShadowBottom.setAlpha((int) (0xff * alpha));
        mShadowBottom.draw(canvas);
    }
    return result;
}

From source file:com.aviary.android.feather.sdk.widget.AviaryWorkspace.java

private void drawEdges(Canvas canvas) {

    if (mEdgeGlowLeft != null) {

        final int width = getWidth();
        final int height = getHeight() - getPaddingTop() - getPaddingBottom();

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

            canvas.rotate(270);/*from  w w  w  .  j  a va2  s .com*/
            canvas.translate(-height + getPaddingTop(), 0);
            mEdgeGlowLeft.setSize(height, width);
            if (mEdgeGlowLeft.draw(canvas)) {
                postInvalidate();
            }
            canvas.restoreToCount(restoreCount);
        }

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

            canvas.rotate(90);
            canvas.translate(-getPaddingTop(), -(mTouchX + width));
            mEdgeGlowRight.setSize(height, width);

            if (mEdgeGlowRight.draw(canvas)) {
                postInvalidate();
            }
            canvas.restoreToCount(restoreCount);
        }
    }
}

From source file:com.blestep.sportsbracelet.view.TimelineChartView.java

private void drawTickLabels(Canvas c, SparseArray<Object[]> data) {
    final int size = data.size() - 1;
    final float graphCenterX = mGraphArea.left + (mGraphArea.width() / 2);
    final float halfItemBarWidth = mBarItemWidth / 2;
    for (int i = mItemsOnScreen[1]; i >= mItemsOnScreen[0] && i <= data.size(); i--) {
        final float barCenterX = graphCenterX + mCurrentOffset - (mBarWidth * (size - i));
        float barLeft = barCenterX - halfItemBarWidth;
        float barRight = barCenterX + halfItemBarWidth;
        // Update the dynamic layout
        String label = (String) data.valueAt(i)[0];
        // //from   w ww  .ja  v a 2  s .co  m
        // Calculate the x position and draw the layout
        final float x = graphCenterX + mCurrentOffset - (mBarWidth * (size - i))
                - (mLabelFgPaint.measureText(label) / 2);
        final int restoreCount = c.save();
        c.translate(x, mFooterArea.top + (mFooterArea.height() / 2 - mTickLabelMinHeight / 2));
        final Paint paint;
        // ?
        paint = barLeft < graphCenterX && barRight > graphCenterX
                && (mLastPosition == mCurrentPosition || (mState != STATE_SCROLLING)) ? mHighlightLabelFgPaint
                        : mLabelFgPaint;
        c.drawText(label, 0, 0, paint);
        c.restoreToCount(restoreCount);
    }
}

From source file:com.abewy.android.apps.klyph.widget.KlyphDrawerLayout.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  a2  s.c om*/

            if (checkDrawerViewGravity(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 && checkDrawerViewGravity(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 && checkDrawerViewGravity(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.lansun.qmyo.view.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  w w w . ja v  a 2 s  .c  o m*/

            if (checkDrawerViewGravity(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 && checkDrawerViewGravity(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 && checkDrawerViewGravity(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;
}