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

@Deprecated
public boolean clipRect(@NonNull Rect rect, @NonNull Region.Op op) 

Source Link

Document

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

Usage

From source file:com.yanzhenjie.durban.view.OverlayView.java

/**
 * This method draws dimmed area around the crop bounds.
 *
 * @param canvas - valid canvas object/*  ww  w  . j a va 2  s.  com*/
 */
protected void drawDimmedLayer(@NonNull Canvas canvas) {
    canvas.save();
    if (mCircleDimmedLayer)
        canvas.clipPath(mCircularPath, Region.Op.DIFFERENCE);
    else
        canvas.clipRect(mCropViewRect, Region.Op.DIFFERENCE);
    canvas.drawColor(mDimmedColor);
    canvas.restore();

    if (mCircleDimmedLayer) { // Draw 1px stroke to fix antialias
        canvas.drawCircle(mCropViewRect.centerX(), mCropViewRect.centerY(),
                Math.min(mCropViewRect.width(), mCropViewRect.height()) / 2.f, mDimmedStrokePaint);
    }
}

From source file:org.telegram.ui.Components.Switch.java

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

    final Rect padding = mTempRect;
    final Drawable trackDrawable = mTrackDrawable;
    if (trackDrawable != null) {
        trackDrawable.getPadding(padding);
    } else {//from   w  ww.j  av a 2 s .  c o m
        padding.setEmpty();
    }

    final int switchTop = mSwitchTop;
    final int switchBottom = mSwitchBottom;

    final Drawable thumbDrawable = mThumbDrawable;
    if (trackDrawable != null) {
        if (mSplitTrack && thumbDrawable != null) {
            final Insets insets = Insets.NONE;
            thumbDrawable.copyBounds(padding);
            padding.left += insets.left;
            padding.right -= insets.right;

            final int saveCount = canvas.save();
            canvas.clipRect(padding, Region.Op.DIFFERENCE);
            trackDrawable.draw(canvas);
            canvas.restoreToCount(saveCount);
        } else {
            trackDrawable.draw(canvas);
        }
    }

    final int saveCount = canvas.save();

    if (thumbDrawable != null) {
        thumbDrawable.draw(canvas);
    }

    canvas.restoreToCount(saveCount);
}

From source file:org.mariotaku.multivalueswitch.library.MultiValueSwitch.java

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

    final Rect padding = mTempRect;
    final Drawable trackDrawable = mTrackDrawable;
    if (trackDrawable != null) {
        trackDrawable.getPadding(padding);
    } else {//from ww  w.ja  v  a 2 s . c  om
        padding.setEmpty();
    }

    final Drawable thumbDrawable = mThumbDrawable;
    if (trackDrawable != null) {
        if (mSplitTrack && thumbDrawable != null) {
            final Rect insets = MVS_DrawableUtilsAccessor.getOpticalBounds(thumbDrawable);
            thumbDrawable.copyBounds(padding);
            padding.left += insets.left;
            padding.right -= insets.right;

            final int saveCount = canvas.save();
            canvas.clipRect(padding, Region.Op.DIFFERENCE);
            trackDrawable.draw(canvas);
            canvas.restoreToCount(saveCount);
        } else {
            trackDrawable.draw(canvas);
        }

        final int thumbScrollStart = mSwitchLeft + mThumbWidth - mThumbWidth / 2;
        final int thumbScrollRange = mSwitchRight - mSwitchLeft - mThumbWidth;
        final int y = trackDrawable.getBounds().centerY();
        for (int i = 0, j = getMax(); i < j; i++) {
            if (i == 0 || i == j - 1)
                continue;

            canvas.drawCircle(thumbScrollStart + i * (thumbScrollRange / (j - 1)), y, mPointRadius,
                    mPointPaint);
        }
    }

    final int saveCount = canvas.save();

    if (thumbDrawable != null) {
        thumbDrawable.draw(canvas);
    }

    canvas.restoreToCount(saveCount);

}

From source file:android.support.v7.widget.SwitchCompat.java

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

    final Rect padding = mTempRect;
    final Drawable trackDrawable = mTrackDrawable;
    if (trackDrawable != null) {
        trackDrawable.getPadding(padding);
    } else {//w  w w .j  a  v a 2 s  . c  o  m
        padding.setEmpty();
    }

    final int switchTop = mSwitchTop;
    final int switchBottom = mSwitchBottom;
    final int switchInnerTop = switchTop + padding.top;
    final int switchInnerBottom = switchBottom - padding.bottom;

    final Drawable thumbDrawable = mThumbDrawable;
    if (trackDrawable != null) {
        if (mSplitTrack && thumbDrawable != null) {
            final Rect insets = DrawableUtils.getOpticalBounds(thumbDrawable);
            thumbDrawable.copyBounds(padding);
            padding.left += insets.left;
            padding.right -= insets.right;

            final int saveCount = canvas.save();
            canvas.clipRect(padding, Region.Op.DIFFERENCE);
            trackDrawable.draw(canvas);
            canvas.restoreToCount(saveCount);
        } else {
            trackDrawable.draw(canvas);
        }
    }

    final int saveCount = canvas.save();

    if (thumbDrawable != null) {
        thumbDrawable.draw(canvas);
    }

    final Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
    if (switchText != null) {
        final int drawableState[] = getDrawableState();
        if (mTextColors != null) {
            mTextPaint.setColor(mTextColors.getColorForState(drawableState, 0));
        }
        mTextPaint.drawableState = drawableState;

        final int cX;
        if (thumbDrawable != null) {
            final Rect bounds = thumbDrawable.getBounds();
            cX = bounds.left + bounds.right;
        } else {
            cX = getWidth();
        }

        final int left = cX / 2 - switchText.getWidth() / 2;
        final int top = (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2;
        canvas.translate(left, top);
        switchText.draw(canvas);
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.android.leanlauncher.Workspace.java

/**
 * Draw the View v into the given Canvas.
 *
 * @param v the view to draw// ww  w. j  a va2  s.  co m
 * @param destCanvas the canvas to draw on
 * @param padding the horizontal and vertical padding to use when drawing
 */
private static void drawDragView(View v, Canvas destCanvas, int padding) {
    final Rect clipRect = sTempRect;
    v.getDrawingRect(clipRect);

    destCanvas.save();
    if (v instanceof TextView) {
        Drawable d = ((TextView) v).getCompoundDrawables()[1];
        Rect bounds = getDrawableBounds(d);
        clipRect.set(0, 0, bounds.width() + padding, bounds.height() + padding);
        destCanvas.translate(padding / 2 - bounds.left, padding / 2 - bounds.top);
        d.draw(destCanvas);
    } else {
        destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
        destCanvas.clipRect(clipRect, Op.REPLACE);
        v.draw(destCanvas);
    }
    destCanvas.restore();
}

From source file:rikka.akashitoolkit.ui.widget.IconSwitchCompat.java

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

    final Rect padding = mTempRect;
    final Drawable trackDrawable = mTrackDrawable;
    if (trackDrawable != null) {
        trackDrawable.getPadding(padding);
    } else {//from w  ww.ja v a 2  s  .com
        padding.setEmpty();
    }

    final int switchTop = mSwitchTop;
    final int switchBottom = mSwitchBottom;
    final int switchInnerTop = switchTop + padding.top;
    final int switchInnerBottom = switchBottom - padding.bottom;

    final Drawable thumbDrawable = mThumbDrawable;
    if (trackDrawable != null) {
        if (mSplitTrack && thumbDrawable != null) {
            final Rect insets = DrawableUtils.getOpticalBounds(thumbDrawable);
            thumbDrawable.copyBounds(padding);
            padding.left += insets.left;
            padding.right -= insets.right;

            final int saveCount = canvas.save();
            canvas.clipRect(padding, Region.Op.DIFFERENCE);
            trackDrawable.draw(canvas);
            canvas.restoreToCount(saveCount);
        } else {
            trackDrawable.draw(canvas);
        }
    }

    final int saveCount = canvas.save();

    if (thumbDrawable != null) {
        thumbDrawable.draw(canvas);
    }

    final Drawable iconDrawable = mIconDrawable;
    if (iconDrawable != null) {
        if (mSplitTrack) {
            final Rect insets = DrawableUtils.getOpticalBounds(iconDrawable);
            iconDrawable.copyBounds(padding);
            padding.left += insets.left;
            padding.right -= insets.right;

            //final int saveCount = canvas.save();
            canvas.clipRect(padding, Region.Op.DIFFERENCE);
            iconDrawable.draw(canvas);
            canvas.restoreToCount(saveCount);
        } else {
            iconDrawable.draw(canvas);
        }
    }

    final Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
    if (switchText != null) {
        final int drawableState[] = getDrawableState();
        if (mTextColors != null) {
            mTextPaint.setColor(mTextColors.getColorForState(drawableState, 0));
        }
        mTextPaint.drawableState = drawableState;

        final int cX;
        if (thumbDrawable != null) {
            final Rect bounds = thumbDrawable.getBounds();
            cX = bounds.left + bounds.right;
        } else {
            cX = getWidth();
        }

        final int left = cX / 2 - switchText.getWidth() / 2;
        final int top = (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2;
        canvas.translate(left, top);
        switchText.draw(canvas);
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.yanzhenjie.durban.view.OverlayView.java

/**
 * This method draws crop bounds (empty rectangle)
 * and crop guidelines (vertical and horizontal lines inside the crop bounds) if needed.
 *
 * @param canvas - valid canvas object/*from  w w  w  .  j  a  v a 2 s .  c o  m*/
 */
protected void drawCropGrid(@NonNull Canvas canvas) {
    if (mShowCropGrid) {
        if (mGridPoints == null && !mCropViewRect.isEmpty()) {

            mGridPoints = new float[(mCropGridRowCount) * 4 + (mCropGridColumnCount) * 4];

            int index = 0;
            for (int i = 0; i < mCropGridRowCount; i++) {
                mGridPoints[index++] = mCropViewRect.left;
                mGridPoints[index++] = (mCropViewRect.height()
                        * (((float) i + 1.0f) / (float) (mCropGridRowCount + 1))) + mCropViewRect.top;
                mGridPoints[index++] = mCropViewRect.right;
                mGridPoints[index++] = (mCropViewRect.height()
                        * (((float) i + 1.0f) / (float) (mCropGridRowCount + 1))) + mCropViewRect.top;
            }

            for (int i = 0; i < mCropGridColumnCount; i++) {
                mGridPoints[index++] = (mCropViewRect.width()
                        * (((float) i + 1.0f) / (float) (mCropGridColumnCount + 1))) + mCropViewRect.left;
                mGridPoints[index++] = mCropViewRect.top;
                mGridPoints[index++] = (mCropViewRect.width()
                        * (((float) i + 1.0f) / (float) (mCropGridColumnCount + 1))) + mCropViewRect.left;
                mGridPoints[index++] = mCropViewRect.bottom;
            }
        }

        if (mGridPoints != null)
            canvas.drawLines(mGridPoints, mCropGridPaint);
    }

    if (mShowCropFrame)
        canvas.drawRect(mCropViewRect, mCropFramePaint);

    if (mFreestyleCropMode != FREESTYLE_CROP_MODE_DISABLE) {
        canvas.save();

        mTempRect.set(mCropViewRect);
        mTempRect.inset(mCropRectCornerTouchAreaLineLength, -mCropRectCornerTouchAreaLineLength);
        canvas.clipRect(mTempRect, Region.Op.DIFFERENCE);

        mTempRect.set(mCropViewRect);
        mTempRect.inset(-mCropRectCornerTouchAreaLineLength, mCropRectCornerTouchAreaLineLength);
        canvas.clipRect(mTempRect, Region.Op.DIFFERENCE);

        canvas.drawRect(mCropViewRect, mCropFrameCornersPaint);

        canvas.restore();
    }
}

From source file:com.klinker.android.launcher.launcher3.Workspace.java

/**
 * Draw the View v into the given Canvas.
 *
 * @param v the view to draw/*ww  w.  j  a v  a2 s. c o  m*/
 * @param destCanvas the canvas to draw on
 * @param padding the horizontal and vertical padding to use when drawing
 */
private static void drawDragView(View v, Canvas destCanvas, int padding) {
    final Rect clipRect = sTempRect;
    v.getDrawingRect(clipRect);

    boolean textVisible = false;

    destCanvas.save();
    if (v instanceof TextView) {
        Drawable d = getTextViewIcon((TextView) v);
        Rect bounds = getDrawableBounds(d);
        clipRect.set(0, 0, bounds.width() + padding, bounds.height() + padding);
        destCanvas.translate(padding / 2 - bounds.left, padding / 2 - bounds.top);
        d.draw(destCanvas);
    } else {
        if (v instanceof FolderIcon) {
            // For FolderIcons the text can bleed into the icon area, and so we need to
            // hide the text completely (which can't be achieved by clipping).
            if (((FolderIcon) v).getTextVisible()) {
                ((FolderIcon) v).setTextVisible(false);
                textVisible = true;
            }
        }
        destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
        destCanvas.clipRect(clipRect, Op.REPLACE);
        v.draw(destCanvas);

        // Restore text visibility of FolderIcon if necessary
        if (textVisible) {
            ((FolderIcon) v).setTextVisible(true);
        }
    }
    destCanvas.restore();
}

From source file:com.android.launcher3.Workspace.java

/**
 * Draw the View v into the given Canvas.
 *
 * @param v the view to draw//from w ww. ja  v  a 2  s  .co  m
 * @param destCanvas the canvas to draw on
 * @param padding the horizontal and vertical padding to use when drawing
 */
private static void drawDragView(View v, Canvas destCanvas, int padding) {
    final Rect clipRect = sTempRect;
    v.getDrawingRect(clipRect);

    boolean textVisible = false;

    destCanvas.save();
    if (v instanceof TextView) {
        Drawable d = ((TextView) v).getCompoundDrawables()[1];
        Rect bounds = getDrawableBounds(d);
        clipRect.set(0, 0, bounds.width() + padding, bounds.height() + padding);
        destCanvas.translate(padding / 2 - bounds.left, padding / 2 - bounds.top);
        d.draw(destCanvas);
    } else {
        if (v instanceof FolderIcon) {
            // For FolderIcons the text can bleed into the icon area, and so we need to
            // hide the text completely (which can't be achieved by clipping).
            if (((FolderIcon) v).getTextVisible()) {
                ((FolderIcon) v).setTextVisible(false);
                textVisible = true;
            }
        }
        destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
        destCanvas.clipRect(clipRect, Op.REPLACE);
        v.draw(destCanvas);

        // Restore text visibility of FolderIcon if necessary
        if (textVisible) {
            ((FolderIcon) v).setTextVisible(true);
        }
    }
    destCanvas.restore();
}

From source file:cc.flydev.launcher.Workspace.java

/**
 * Draw the View v into the given Canvas.
 *
 * @param v the view to draw//from  w  ww  .ja  v a2  s  .com
 * @param destCanvas the canvas to draw on
 * @param padding the horizontal and vertical padding to use when drawing
 */
private void drawDragView(View v, Canvas destCanvas, int padding, boolean pruneToDrawable) {
    final Rect clipRect = mTempRect;
    v.getDrawingRect(clipRect);

    boolean textVisible = false;

    destCanvas.save();
    if (v instanceof TextView && pruneToDrawable) {
        Drawable d = ((TextView) v).getCompoundDrawables()[1];
        clipRect.set(0, 0, d.getIntrinsicWidth() + padding, d.getIntrinsicHeight() + padding);
        destCanvas.translate(padding / 2, padding / 2);
        d.draw(destCanvas);
    } else {
        if (v instanceof FolderIcon) {
            // For FolderIcons the text can bleed into the icon area, and so we need to
            // hide the text completely (which can't be achieved by clipping).
            if (((FolderIcon) v).getTextVisible()) {
                ((FolderIcon) v).setTextVisible(false);
                textVisible = true;
            }
        } else if (v instanceof BubbleTextView) {
            final BubbleTextView tv = (BubbleTextView) v;
            clipRect.bottom = tv.getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V
                    + tv.getLayout().getLineTop(0);
        } else if (v instanceof TextView) {
            final TextView tv = (TextView) v;
            clipRect.bottom = tv.getExtendedPaddingTop() - tv.getCompoundDrawablePadding()
                    + tv.getLayout().getLineTop(0);
        }
        destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
        destCanvas.clipRect(clipRect, Op.REPLACE);
        v.draw(destCanvas);

        // Restore text visibility of FolderIcon if necessary
        if (textVisible) {
            ((FolderIcon) v).setTextVisible(true);
        }
    }
    destCanvas.restore();
}