Example usage for android.graphics Canvas clipRect

List of usage examples for android.graphics Canvas clipRect

Introduction

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

Prototype

public boolean clipRect(int left, int top, int right, int bottom) 

Source Link

Document

Intersect the current clip with the specified rectangle, which is expressed in local coordinates.

Usage

From source file:ru.mahortov.listviewcustom.ListView.PinnedSectionListView.java

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

    if (mPinnedSection != null) {

        // prepare variables
        int pLeft = getListPaddingLeft();
        int pTop = getListPaddingTop();
        View view = mPinnedSection.view;

        // draw child
        canvas.save();//from  www.  ja v a 2s .c om

        int clipHeight = view.getHeight()
                + (mShadowDrawable == null ? 0 : Math.min(mShadowHeight, mSectionsDistanceY));
        canvas.clipRect(pLeft, pTop, pLeft + view.getWidth(), pTop + clipHeight);

        canvas.translate(pLeft, pTop + mTranslateY);
        drawChild(canvas, mPinnedSection.view, getDrawingTime());

        if (mShadowDrawable != null && mSectionsDistanceY > 0) {
            mShadowDrawable.setBounds(mPinnedSection.view.getLeft(), mPinnedSection.view.getBottom(),
                    mPinnedSection.view.getRight(), mPinnedSection.view.getBottom() + mShadowHeight);
            mShadowDrawable.draw(canvas);
        }

        canvas.restore();
    }
}

From source file:com.hippo.widget.slidingdrawerlayout.SlidingDrawerLayout.java

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

    if (mFitPaddingTop != 0) {
        int saved = canvas.save();
        canvas.clipRect(0, 0, getWidth(), mFitPaddingTop);
        canvas.drawColor(mStatusBarColor);
        canvas.restoreToCount(saved);/*from   w  w  w. ja  va2  s .  com*/
    }

    if (mFitPaddingBottom != 0) {
        int saved = canvas.save();
        int height = getHeight();
        canvas.clipRect(0, height - mFitPaddingBottom, getWidth(), height);
        canvas.drawColor(mNavigationBarColor);
        canvas.restoreToCount(saved);
    }
}

From source file:com.hippo.widget.recyclerview.EasyRecyclerView.java

@Override
protected void dispatchDraw(@NonNull Canvas canvas) {
    boolean clipToPadding = mClipToPadding;
    int saveCount = 0;
    if (clipToPadding) {
        saveCount = canvas.save();//from  www  . j av  a  2s. com
        final int scrollX = getScrollX();
        final int scrollY = getScrollY();
        canvas.clipRect(scrollX + getPaddingLeft(), scrollY + getPaddingTop(),
                scrollX + getRight() - getLeft() - getPaddingRight(),
                scrollY + getBottom() - getTop() - getPaddingBottom());
    }

    // TODO disable selector drawable state change when need not to draw selector
    boolean drawSelector = mOnDrawSelectorListener == null || mSelectorPosition < 0
            || mSelectorPosition >= mAdapter.getItemCount()
            || mOnDrawSelectorListener.beforeDrawSelector(mSelectorPosition);
    final boolean drawSelectorOnTop = mDrawSelectorOnTop;

    if (drawSelector && !drawSelectorOnTop) {
        drawSelector(canvas);
    }

    super.dispatchDraw(canvas);

    if (drawSelector && drawSelectorOnTop) {
        drawSelector(canvas);
    }

    if (clipToPadding) {
        canvas.restoreToCount(saveCount);
    }
}

From source file:org.connectbot.TerminalView.java

@Override
public void onDraw(Canvas canvas) {
    if (bridge.bitmap != null) {
        // draw the bitmap
        bridge.onDraw();//from ww w  .  j  a  va 2 s  .co m

        // draw the bridge bitmap if it exists
        canvas.drawBitmap(bridge.bitmap, 0, 0, paint);

        // also draw cursor if visible
        if (bridge.buffer.isCursorVisible()) {
            int cursorColumn = bridge.buffer.getCursorColumn();
            final int cursorRow = bridge.buffer.getCursorRow();

            final int columns = bridge.buffer.getColumns();

            if (cursorColumn == columns)
                cursorColumn = columns - 1;

            if (cursorColumn < 0 || cursorRow < 0)
                return;

            int currentAttribute = bridge.buffer.getAttributes(cursorColumn, cursorRow);
            boolean onWideCharacter = (currentAttribute & VDUBuffer.FULLWIDTH) != 0;

            int x = cursorColumn * bridge.charWidth;
            int y = (bridge.buffer.getCursorRow() + bridge.buffer.screenBase - bridge.buffer.windowBase)
                    * bridge.charHeight;

            // Save the current clip and translation
            canvas.save();

            canvas.translate(x, y);
            canvas.clipRect(0, 0, bridge.charWidth * (onWideCharacter ? 2 : 1), bridge.charHeight);
            canvas.drawPaint(cursorPaint);

            final int deadKey = bridge.getKeyHandler().getDeadKey();
            if (deadKey != 0) {
                canvas.drawText(new char[] { (char) deadKey }, 0, 1, 0, 0, cursorStrokePaint);
            }

            // Make sure we scale our decorations to the correct size.
            canvas.concat(scaleMatrix);

            int metaState = bridge.getKeyHandler().getMetaState();

            if ((metaState & TerminalKeyListener.OUR_SHIFT_ON) != 0)
                canvas.drawPath(shiftCursor, cursorStrokePaint);
            else if ((metaState & TerminalKeyListener.OUR_SHIFT_LOCK) != 0)
                canvas.drawPath(shiftCursor, cursorPaint);

            if ((metaState & TerminalKeyListener.OUR_ALT_ON) != 0)
                canvas.drawPath(altCursor, cursorStrokePaint);
            else if ((metaState & TerminalKeyListener.OUR_ALT_LOCK) != 0)
                canvas.drawPath(altCursor, cursorPaint);

            if ((metaState & TerminalKeyListener.OUR_CTRL_ON) != 0)
                canvas.drawPath(ctrlCursor, cursorStrokePaint);
            else if ((metaState & TerminalKeyListener.OUR_CTRL_LOCK) != 0)
                canvas.drawPath(ctrlCursor, cursorPaint);

            // Restore previous clip region
            canvas.restore();
        }

        // draw any highlighted area
        if (bridge.isSelectingForCopy()) {
            SelectionArea area = bridge.getSelectionArea();
            canvas.save(Canvas.CLIP_SAVE_FLAG);
            canvas.clipRect(area.getLeft() * bridge.charWidth, area.getTop() * bridge.charHeight,
                    (area.getRight() + 1) * bridge.charWidth, (area.getBottom() + 1) * bridge.charHeight);
            canvas.drawPaint(cursorPaint);
            canvas.restore();
        }
    }
}

From source file:org.telegram.ui.ActionBar.ActionBarLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    int width = getWidth() - getPaddingLeft() - getPaddingRight();
    int translationX = (int) innerTranslationX + getPaddingRight();
    int clipLeft = getPaddingLeft();
    int clipRight = width + getPaddingLeft();

    if (child == containerViewBack) {
        clipRight = translationX;//from  w ww . j ava 2 s  .  co m
    } else if (child == containerView) {
        clipLeft = translationX;
    }

    final int restoreCount = canvas.save();
    if (!transitionAnimationInProgress) {
        canvas.clipRect(clipLeft, 0, clipRight, getHeight());
    }
    final boolean result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(restoreCount);

    if (translationX != 0) {
        if (child == containerView) {
            final float alpha = Math.max(0,
                    Math.min((width - translationX) / (float) AndroidUtilities.dp(20), 1.0f));
            layerShadowDrawable.setBounds(translationX - layerShadowDrawable.getIntrinsicWidth(),
                    child.getTop(), translationX, child.getBottom());
            layerShadowDrawable.setAlpha((int) (0xff * alpha));
            layerShadowDrawable.draw(canvas);
        } else if (child == containerViewBack) {
            float opacity = Math.min(0.8f, (width - translationX) / (float) width);
            if (opacity < 0) {
                opacity = 0;
            }
            scrimPaint.setColor((int) (((0x99000000 & 0xff000000) >>> 24) * opacity) << 24);
            canvas.drawRect(clipLeft, 0, clipRight, getHeight(), scrimPaint);
        }
    }

    return result;
}

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  ww.  j  av a2  s.c  o  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:com.guodong.sun.guodong.widget.ZoomImageView.java

@Override
protected void onDraw(Canvas canvas) {
    if (getDrawable() == null) {
        return; // couldn't resolve the URI
    }/*ww  w  .j  av  a 2s  .c o m*/

    if (mState == STATE_TRANSFORM_IN || mState == STATE_TRANSFORM_OUT) {
        if (mTransformStart) {
            initTransform();
        }
        if (mTransfrom == null) {
            super.onDraw(canvas);
            return;
        }

        if (mTransformStart) {
            if (mState == STATE_TRANSFORM_IN) {
                mTransfrom.initStartIn();
            } else {
                mTransfrom.initStartOut();
            }
        }

        if (mTransformStart) {
            Log.d("Dean", "mTransfrom.startScale:" + mTransfrom.startScale);
            Log.d("Dean", "mTransfrom.startScale:" + mTransfrom.endScale);
            Log.d("Dean", "mTransfrom.scale:" + mTransfrom.scale);
            Log.d("Dean", "mTransfrom.startRect:" + mTransfrom.startRect.toString());
            Log.d("Dean", "mTransfrom.endRect:" + mTransfrom.endRect.toString());
            Log.d("Dean", "mTransfrom.rect:" + mTransfrom.rect.toString());
        }

        mPaint.setAlpha(mBgAlpha);
        canvas.drawPaint(mPaint);

        int saveCount = canvas.getSaveCount();
        canvas.save();
        // ?Matrix
        getBmpMatrix();
        canvas.translate(mTransfrom.rect.left, mTransfrom.rect.top);
        canvas.clipRect(0, 0, mTransfrom.rect.width, mTransfrom.rect.height);
        canvas.concat(mSmoothMatrix);
        getDrawable().draw(canvas);
        canvas.restoreToCount(saveCount);
        if (mTransformStart) {
            mTransformStart = false;
            startTransform(mState);
        }
    } else {
        //Transform In???Activity??
        mPaint.setAlpha(255);
        canvas.drawPaint(mPaint);
        super.onDraw(canvas);
    }
}

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;
            }/*from  w  w w .j ava2 s . c o  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.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   ww  w.j a va  2  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  ww w .  j a va 2s  . co 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;
}