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: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;
            }//  www  .java  2 s. 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 (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.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  w  w w . j av  a  2 s .  c  o 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:com.jude.easyrecyclerview.decoration.StickyHeaderDecoration.java

/**
 * {@inheritDoc}/*w  w  w  .j  a  v  a  2 s.  com*/
 */
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    if (parent.getAdapter() == null) {
        return;
    }

    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.youth.xframe.adapter.decoration.StickyHeaderDecoration.java

/**
 * {@inheritDoc}// ww  w.  j a v a 2 s. c  o m
 */
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    if (parent.getAdapter() == null) {
        return;
    }

    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 XRecyclerViewAdapter) {
                int headerCount = ((XRecyclerViewAdapter) parent.getAdapter()).getHeaderCount();
                int footerCount = ((XRecyclerViewAdapter) parent.getAdapter()).getFooterCount();
                int dataCount = ((XRecyclerViewAdapter) parent.getAdapter()).getDataCount();
                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.sanjie.zy.adpter.decoration.StickyHeaderDecoration.java

/**
 * {@inheritDoc}/*from   ww w .  j  a v  a 2  s  .co  m*/
 */
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    if (parent.getAdapter() == null) {
        return;
    }

    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 ZYRecyclerViewAdapter) {
                int headerCount = ((ZYRecyclerViewAdapter) parent.getAdapter()).getHeaderCount();
                int footerCount = ((ZYRecyclerViewAdapter) parent.getAdapter()).getFooterCount();
                int dataCount = ((ZYRecyclerViewAdapter) parent.getAdapter()).getDataCount();
                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.xiaosu.lib.base.widget.drawerLayout.DrawerLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final boolean drawingContent = child == mContentView;
    int clipBottom = mDrawerView.getTop();

    final int restoreCount = canvas.save();
    if (drawingContent) {
        canvas.clipRect(0, 0, getWidth(), clipBottom);
    }//from w  w w  .  ja va  2s  .  co  m
    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, 0, getWidth(), clipBottom, mScrimPaint);
    }
    return result;
}

From source file:org.androfarsh.widget.DragGridLayout.java

private void drawHighlight(final View child, final Canvas canvas) {
    if ((mHighlightDrawable != null)) {
        requestFreeCellRegion(mDragNode.view);
        if (!requestHoverRect(mTmpRect)) {
            return;
        }//from ww  w  .  j a  v a 2s  .c o m
        canvas.save();
        canvas.clipRect(mTmpRect);
        mHighlightDrawable.setBounds(mTmpRect);

        mTmpRegion.set(mFreeCellsRegion);
        mTmpRegion.op(mTmpRect, Op.INTERSECT);
        mTmpRegion.getBounds(mTmpRect);

        final boolean allowed = mTmpRegion.isRect() && (child.getWidth() <= mTmpRect.width())
                && (child.getHeight() <= mTmpRect.height());
        final int[] stateSet = new int[] { (allowed ? 1 : -1) * R.attr.state_drop_allow };

        mHighlightDrawable.setState(stateSet);
        mHighlightDrawable.draw(canvas);
        canvas.restore();
    }
}

From source file:com.jjoe64.graphview.GridLabelRenderer.java

/**
 * draws the vertical axis title if//from www  .  ja v a2 s . co  m
 * it is set
 * @param canvas canvas
 */
protected void drawVerticalAxisTitle(Canvas canvas) {
    if (mVerticalAxisTitle != null && mVerticalAxisTitle.length() > 0) {
        mPaintAxisTitle.setColor(getVerticalAxisTitleColor());
        mPaintAxisTitle.setTextSize(getVerticalAxisTitleTextSize());
        float x = getVerticalAxisTitleWidth();
        float y = canvas.getHeight() / 2;
        canvas.save();
        canvas.rotate(-90, x, y);
        canvas.drawText(mVerticalAxisTitle, x, y, mPaintAxisTitle);
        canvas.restore();
    }
}

From source file:com.mukesh.OtpView.java

private void drawOtpView(Canvas canvas) {
    int nextItemToFill;
    if (rtlTextDirection) {
        nextItemToFill = otpViewItemCount - 1;
    } else {//from   ww  w . ja  v a 2  s. com
        if (getText() != null) {
            nextItemToFill = getText().length();
        } else {
            nextItemToFill = 0;
        }
    }
    for (int i = 0; i < otpViewItemCount; i++) {
        boolean itemSelected = isFocused() && nextItemToFill == i;
        boolean itemFilled = i < nextItemToFill;
        int[] itemState = null;
        if (itemFilled) {
            itemState = FILLED_STATE;
        } else if (itemSelected) {
            itemState = SELECTED_STATE;
        }
        paint.setColor(itemState != null ? getLineColorForState(itemState) : cursorLineColor);
        updateItemRectF(i);
        updateCenterPoint();
        canvas.save();
        if (viewType == VIEW_TYPE_RECTANGLE) {
            updateOtpViewBoxPath(i);
            canvas.clipPath(path);
        }
        drawItemBackground(canvas, itemState);
        canvas.restore();
        if (itemSelected) {
            drawCursor(canvas);
        }
        if (viewType == VIEW_TYPE_RECTANGLE) {
            drawOtpBox(canvas, i);
        } else if (viewType == VIEW_TYPE_LINE) {
            drawOtpLine(canvas, i);
        }
        if (DBG) {
            drawAnchorLine(canvas);
        }
        if (rtlTextDirection) {
            int reversedPosition = otpViewItemCount - i;
            if (getText().length() >= reversedPosition) {
                drawInput(canvas, i);
            } else if (!TextUtils.isEmpty(getHint()) && getHint().length() == otpViewItemCount) {
                drawHint(canvas, i);
            }
        } else {
            if (getText().length() > i) {
                drawInput(canvas, i);
            } else if (!TextUtils.isEmpty(getHint()) && getHint().length() == otpViewItemCount) {
                drawHint(canvas, i);
            }
        }
    }
    if (isFocused() && getText() != null && getText().length() != otpViewItemCount
            && viewType == VIEW_TYPE_RECTANGLE) {
        int index = getText().length();
        updateItemRectF(index);
        updateCenterPoint();
        updateOtpViewBoxPath(index);
        paint.setColor(getLineColorForState(SELECTED_STATE));
        drawOtpBox(canvas, index);
    }
}

From source file:org.akop.crosswords.view.CrosswordView.java

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

    if (mPuzzleBitmap != null) {
        canvas.save();
        canvas.clipRect(mContentRect);/*from   w  ww  .  j a v  a2s. co m*/
        canvas.translate(mBitmapOffset.x, mBitmapOffset.y);
        canvas.scale(mBitmapScale, mBitmapScale);
        canvas.drawBitmap(mPuzzleBitmap, 0, 0, mBitmapPaint);

        canvas.restore();
    }
}