Example usage for android.graphics Canvas clipPath

List of usage examples for android.graphics Canvas clipPath

Introduction

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

Prototype

@Deprecated
public boolean clipPath(@NonNull Path path, @NonNull Region.Op op) 

Source Link

Document

Modify the current clip with the specified path.

Usage

From source file:com.android.tabletcustomui.views.LeftCircleContainer.java

private void addCircle(int position, final int value, float diff, double ratio) {
    final int radius = value / 2;
    final RectF mOval = new RectF();
    mOval.set(0 + diff, 0 + diff, value - diff, value - diff);

    final Paint mPaintClip = new Paint();
    mPaintClip.setStyle(Paint.Style.FILL);
    mPaintClip.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.OVERLAY));
    mPaintClip.setAntiAlias(true);/*from   w  w  w  . j av  a2  s.c o m*/

    final PointF pointF = new PointF(mOval.centerX(), mOval.centerY());
    final Path clipPath = new Path();
    clipPath.addCircle(pointF.x, pointF.y, (float) (radius * ratio), Path.Direction.CW);

    View circleView;
    if (position == 0) {
        mPaintClip.setColor(getContext().getResources().getColor(R.color.circle_light_blue));
        circleView = new View(getContext()) {

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

                canvas.clipPath(clipPath, Region.Op.DIFFERENCE);

                canvas.drawArc(mOval, 330, 10, true, mPaintClip);
                canvas.drawArc(mOval, 342, 20, true, mPaintClip);
                canvas.drawArc(mOval, 4, 10, true, mPaintClip);

                canvas.drawArc(mOval, 240, 10, true, mPaintClip);
                canvas.drawArc(mOval, 252, 20, true, mPaintClip);
                canvas.drawArc(mOval, 275, 10, true, mPaintClip);

                canvas.drawArc(mOval, 150, 10, true, mPaintClip);
                canvas.drawArc(mOval, 162, 20, true, mPaintClip);
                canvas.drawArc(mOval, 185, 10, true, mPaintClip);

                canvas.drawArc(mOval, 60, 10, true, mPaintClip);
                canvas.drawArc(mOval, 72, 20, true, mPaintClip);
                canvas.drawArc(mOval, 95, 10, true, mPaintClip);
            }
        };

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(value, value);
        layoutParams.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;
        circleView.setLayoutParams(layoutParams);

        this.addView(circleView);

        animateClockWise(circleView);

    } else if (position == 1) {

        mPaintClip.setColor(getContext().getResources().getColor(R.color.circle_dark_blue));
        circleView = new View(getContext()) {

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

                canvas.clipPath(clipPath, Region.Op.DIFFERENCE);

                canvas.drawCircle(pointF.x, pointF.y, radius, mPaintClip);
            }
        };

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(value, value);
        layoutParams.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;
        circleView.setLayoutParams(layoutParams);

        this.addView(circleView);
        animateClockWise(circleView);

    } else if (position == 2) {

        mPaintClip.setColor(getContext().getResources().getColor(R.color.circle_light_blue));
        circleView = new View(getContext()) {

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

                canvas.clipPath(clipPath, Region.Op.DIFFERENCE);

                canvas.drawArc(mOval, 55, 190, true, mPaintClip);

            }
        };

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(value, value);
        layoutParams.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;
        circleView.setLayoutParams(layoutParams);

        this.addView(circleView);
        animateClockWise(circleView);

    } else if (position == 3) {
        mPaintClip.setColor(getContext().getResources().getColor(R.color.circle_light_blue));
        circleView = new View(getContext()) {

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

                canvas.clipPath(clipPath, Region.Op.DIFFERENCE);

                canvas.drawArc(mOval, 240, 190, true, mPaintClip);
                canvas.drawArc(mOval, 80, 150, true, mPaintClip);
            }
        };

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(value, value);
        layoutParams.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;
        circleView.setLayoutParams(layoutParams);

        this.addView(circleView);
        animateClockWise(circleView);

    } else if (position == 4) {
        mPaintClip.setColor(getContext().getResources().getColor(R.color.circle_light_blue));
        circleView = new View(getContext()) {

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

                canvas.clipPath(clipPath, Region.Op.DIFFERENCE);

                canvas.drawArc(mOval, 335, 150, true, mPaintClip);
                canvas.drawArc(mOval, 140, 180, true, mPaintClip);
            }
        };

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(value, value);
        layoutParams.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;
        circleView.setLayoutParams(layoutParams);

        this.addView(circleView);
        animateClockWise(circleView);

    } else if (position == 5) {
        mPaintClip.setColor(getContext().getResources().getColor(R.color.circle_light_grey));
        circleView = new View(getContext()) {

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

                canvas.clipPath(clipPath, Region.Op.DIFFERENCE);

                canvas.drawArc(mOval, 330, 150, true, mPaintClip);
                canvas.drawArc(mOval, 140, 150, true, mPaintClip);
            }
        };

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(value, value);
        layoutParams.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;
        circleView.setLayoutParams(layoutParams);

        this.addView(circleView);
        animateClockWise(circleView);

    } else if (position == 6) {
        mPaintClip.setColor(getContext().getResources().getColor(R.color.circle_purple_color));
        circleView = new View(getContext()) {

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

                canvas.clipPath(clipPath, Region.Op.DIFFERENCE);

                canvas.drawArc(mOval, 290, 5, true, mPaintClip);
                canvas.drawArc(mOval, 297, 20, true, mPaintClip);
                canvas.drawArc(mOval, 319, 20, true, mPaintClip);

                canvas.drawArc(mOval, 60, 15, true, mPaintClip);
                canvas.drawArc(mOval, 77, 8, true, mPaintClip);

            }
        };

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(value, value);
        layoutParams.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;
        circleView.setLayoutParams(layoutParams);

        this.addView(circleView);

        animateClockWise(circleView);

    } else if (position == 7) {
        mPaintClip.setColor(getContext().getResources().getColor(R.color.circle_dark_blue));
        circleView = new View(getContext()) {

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

                canvas.clipPath(clipPath, Region.Op.DIFFERENCE);

                canvas.drawArc(mOval, 160, 10, true, mPaintClip);
                canvas.drawArc(mOval, 172, 30, true, mPaintClip);
                canvas.drawArc(mOval, 204, 30, true, mPaintClip);
                canvas.drawArc(mOval, 236, 10, true, mPaintClip);

                canvas.drawArc(mOval, 20, 20, true, mPaintClip);
                canvas.drawArc(mOval, 42, 8, true, mPaintClip);

            }
        };

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(value, value);
        layoutParams.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;
        circleView.setLayoutParams(layoutParams);

        this.addView(circleView);
        animateAntiClockWise(circleView);
    }
}

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

/**
 * This method draws dimmed area around the crop bounds.
 *
 * @param canvas - valid canvas object//from ww  w  .  ja  v  a  2s.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:eu.janmuller.android.simplecropimage.CropImage.java

private void onSaveClicked() throws Exception {
    // TODO this code needs to change to use the decode/crop/encode single
    // step api so that we don't require that the whole (possibly large)
    // bitmap doesn't have to be read into memory
    if (mSaving)/*from w  w w.  j  a  v  a  2s. c  om*/
        return;

    if (mCrop == null) {

        return;
    }

    mSaving = true;

    Rect r = mCrop.getCropRect();

    int width = r.width();
    int height = r.height();

    // If we are circle cropping, we want alpha channel, which is the
    // third param here.
    Bitmap croppedImage;
    try {

        croppedImage = Bitmap.createBitmap(width, height,
                mCircleCrop ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    } catch (Exception e) {
        throw e;
    }
    if (croppedImage == null) {

        return;
    }

    {
        Canvas canvas = new Canvas(croppedImage);
        Rect dstRect = new Rect(0, 0, width, height);
        canvas.drawBitmap(mBitmap, r, dstRect, null);
    }

    if (mCircleCrop) {

        // OK, so what's all this about?
        // Bitmaps are inherently rectangular but we want to return
        // something that's basically a circle.  So we fill in the
        // area around the circle with alpha.  Note the all important
        // PortDuff.Mode.CLEAR.
        Canvas c = new Canvas(croppedImage);
        Path p = new Path();
        p.addCircle(width / 2F, height / 2F, width / 2F, Path.Direction.CW);
        c.clipPath(p, Region.Op.DIFFERENCE);
        c.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
    }

    /* If the output is required to a specific size then scale or fill */
    if (mOutputX != 0 && mOutputY != 0) {

        if (mScale) {

            /* Scale the image to the required dimensions */
            Bitmap old = croppedImage;
            croppedImage = Util.transform(new Matrix(), croppedImage, mOutputX, mOutputY, mScaleUp);
            if (old != croppedImage) {

                old.recycle();
            }
        } else {

            /* Don't scale the image crop it to the size requested.
                 * Create an new image with the cropped image in the center and
             * the extra space filled.
             */

            // Don't scale the image but instead fill it so it's the
            // required dimension
            Bitmap b = Bitmap.createBitmap(mOutputX, mOutputY, Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(b);

            Rect srcRect = mCrop.getCropRect();
            Rect dstRect = new Rect(0, 0, mOutputX, mOutputY);

            int dx = (srcRect.width() - dstRect.width()) / 2;
            int dy = (srcRect.height() - dstRect.height()) / 2;

            /* If the srcRect is too big, use the center part of it. */
            srcRect.inset(Math.max(0, dx), Math.max(0, dy));

            /* If the dstRect is too big, use the center part of it. */
            dstRect.inset(Math.max(0, -dx), Math.max(0, -dy));

            /* Draw the cropped bitmap in the center */
            canvas.drawBitmap(mBitmap, srcRect, dstRect, null);

            /* Set the cropped bitmap as the new bitmap */
            croppedImage.recycle();
            croppedImage = b;
        }
    }

    // Return the cropped image directly or save it to the specified URI.
    Bundle myExtras = getIntent().getExtras();
    if (myExtras != null && (myExtras.getParcelable("data") != null || myExtras.getBoolean(RETURN_DATA))) {

        Bundle extras = new Bundle();
        extras.putParcelable(RETURN_DATA_AS_BITMAP, croppedImage);
        setResult(RESULT_OK, (new Intent()).setAction(ACTION_INLINE_DATA).putExtras(extras));
        finish();
    } else {
        final Bitmap b = croppedImage;
        Util.startBackgroundJob(this, null, getString(R.string.saving_image), new Runnable() {
            public void run() {

                saveOutput(b);
            }
        }, mHandler);
    }
}

From source file:com.example.gatsu.theevent.HighlightView.java

protected void draw(Canvas canvas) {

    if (mHidden) {
        return;//  w  w  w  .  j  a v a 2s . c om
    }

    Path path = new Path();
    if (!hasFocus()) {
        mOutlinePaint.setColor(0xFF000000);
        canvas.drawRect(mDrawRect, mOutlinePaint);
    } else {
        Rect viewDrawingRect = new Rect();
        mContext.getDrawingRect(viewDrawingRect);
        if (mCircle) {

            canvas.save();

            float width = mDrawRect.width();
            float height = mDrawRect.height();
            path.addCircle(mDrawRect.left + (width / 2), mDrawRect.top + (height / 2), width / 2,
                    Path.Direction.CW);
            mOutlinePaint.setColor(0xFFEF04D6);

            canvas.clipPath(path, Region.Op.DIFFERENCE);
            canvas.drawRect(viewDrawingRect, hasFocus() ? mFocusPaint : mNoFocusPaint);

            canvas.restore();

        } else {

            Rect topRect = new Rect(viewDrawingRect.left, viewDrawingRect.top, viewDrawingRect.right,
                    mDrawRect.top);
            if (topRect.width() > 0 && topRect.height() > 0) {
                canvas.drawRect(topRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect bottomRect = new Rect(viewDrawingRect.left, mDrawRect.bottom, viewDrawingRect.right,
                    viewDrawingRect.bottom);
            if (bottomRect.width() > 0 && bottomRect.height() > 0) {
                canvas.drawRect(bottomRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect leftRect = new Rect(viewDrawingRect.left, topRect.bottom, mDrawRect.left, bottomRect.top);
            if (leftRect.width() > 0 && leftRect.height() > 0) {
                canvas.drawRect(leftRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect rightRect = new Rect(mDrawRect.right, topRect.bottom, viewDrawingRect.right, bottomRect.top);
            if (rightRect.width() > 0 && rightRect.height() > 0) {
                canvas.drawRect(rightRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }

            path.addRect(new RectF(mDrawRect), Path.Direction.CW);

            mOutlinePaint.setColor(0xFFFF8A00);

        }

        canvas.drawPath(path, mOutlinePaint);

        if (mMode == ModifyMode.Grow) {
            if (mCircle) {
                int width = mResizeDrawableDiagonal.getIntrinsicWidth();
                int height = mResizeDrawableDiagonal.getIntrinsicHeight();

                int d = (int) Math.round(Math.cos(/*45deg*/Math.PI / 4D) * (mDrawRect.width() / 2D));
                int x = mDrawRect.left + (mDrawRect.width() / 2) + d - width / 2;
                int y = mDrawRect.top + (mDrawRect.height() / 2) - d - height / 2;
                mResizeDrawableDiagonal.setBounds(x, y, x + mResizeDrawableDiagonal.getIntrinsicWidth(),
                        y + mResizeDrawableDiagonal.getIntrinsicHeight());
                mResizeDrawableDiagonal.draw(canvas);
            } else {
                int left = mDrawRect.left + 1;
                int right = mDrawRect.right + 1;
                int top = mDrawRect.top + 4;
                int bottom = mDrawRect.bottom + 3;

                int widthWidth = mResizeDrawableWidth.getIntrinsicWidth() / 2;
                int widthHeight = mResizeDrawableWidth.getIntrinsicHeight() / 2;
                int heightHeight = mResizeDrawableHeight.getIntrinsicHeight() / 2;
                int heightWidth = mResizeDrawableHeight.getIntrinsicWidth() / 2;

                int xMiddle = mDrawRect.left + ((mDrawRect.right - mDrawRect.left) / 2);
                int yMiddle = mDrawRect.top + ((mDrawRect.bottom - mDrawRect.top) / 2);

                mResizeDrawableWidth.setBounds(left - widthWidth, yMiddle - widthHeight, left + widthWidth,
                        yMiddle + widthHeight);
                mResizeDrawableWidth.draw(canvas);

                mResizeDrawableWidth.setBounds(right - widthWidth, yMiddle - widthHeight, right + widthWidth,
                        yMiddle + widthHeight);
                mResizeDrawableWidth.draw(canvas);

                mResizeDrawableHeight.setBounds(xMiddle - heightWidth, top - heightHeight,
                        xMiddle + heightWidth, top + heightHeight);
                mResizeDrawableHeight.draw(canvas);

                mResizeDrawableHeight.setBounds(xMiddle - heightWidth, bottom - heightHeight,
                        xMiddle + heightWidth, bottom + heightHeight);
                mResizeDrawableHeight.draw(canvas);
            }
        }
    }
}

From source file:org.caojun.library.cropimage.HighlightView.java

protected void draw(Canvas canvas) {

    if (mHidden) {
        return;/*from ww w  . j  av a 2s .com*/
    }

    Path path = new Path();
    if (!hasFocus()) {
        mOutlinePaint.setColor(0xFF000000);
        canvas.drawRect(mDrawRect, mOutlinePaint);
    } else {
        Rect viewDrawingRect = new Rect();
        mView.getDrawingRect(viewDrawingRect);
        if (mCircle) {

            canvas.save();

            float width = mDrawRect.width();
            float height = mDrawRect.height();
            path.addCircle(mDrawRect.left + (width / 2), mDrawRect.top + (height / 2), width / 2,
                    Path.Direction.CW);
            mOutlinePaint.setColor(0xFFEF04D6);

            canvas.clipPath(path, Region.Op.DIFFERENCE);
            canvas.drawRect(viewDrawingRect, hasFocus() ? mFocusPaint : mNoFocusPaint);

            canvas.restore();

        } else {

            Rect topRect = new Rect(viewDrawingRect.left, viewDrawingRect.top, viewDrawingRect.right,
                    mDrawRect.top);
            if (topRect.width() > 0 && topRect.height() > 0) {
                canvas.drawRect(topRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect bottomRect = new Rect(viewDrawingRect.left, mDrawRect.bottom, viewDrawingRect.right,
                    viewDrawingRect.bottom);
            if (bottomRect.width() > 0 && bottomRect.height() > 0) {
                canvas.drawRect(bottomRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect leftRect = new Rect(viewDrawingRect.left, topRect.bottom, mDrawRect.left, bottomRect.top);
            if (leftRect.width() > 0 && leftRect.height() > 0) {
                canvas.drawRect(leftRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect rightRect = new Rect(mDrawRect.right, topRect.bottom, viewDrawingRect.right, bottomRect.top);
            if (rightRect.width() > 0 && rightRect.height() > 0) {
                canvas.drawRect(rightRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }

            path.addRect(new RectF(mDrawRect), Path.Direction.CW);

            mOutlinePaint.setColor(0xFFFF8A00);

        }

        canvas.drawPath(path, mOutlinePaint);

        if (mMode == ModifyMode.Grow) {
            if (mCircle) {
                int width = mResizeDrawableDiagonal.getIntrinsicWidth();
                int height = mResizeDrawableDiagonal.getIntrinsicHeight();

                int d = (int) Math.round(Math.cos(/*45deg*/Math.PI / 4D) * (mDrawRect.width() / 2D));
                int x = mDrawRect.left + (mDrawRect.width() / 2) + d - width / 2;
                int y = mDrawRect.top + (mDrawRect.height() / 2) - d - height / 2;
                mResizeDrawableDiagonal.setBounds(x, y, x + mResizeDrawableDiagonal.getIntrinsicWidth(),
                        y + mResizeDrawableDiagonal.getIntrinsicHeight());
                mResizeDrawableDiagonal.draw(canvas);
            } else {
                int left = mDrawRect.left + 1;
                int right = mDrawRect.right + 1;
                int top = mDrawRect.top + 4;
                int bottom = mDrawRect.bottom + 3;

                int widthWidth = mResizeDrawableWidth.getIntrinsicWidth() / 2;
                int widthHeight = mResizeDrawableWidth.getIntrinsicHeight() / 2;
                int heightHeight = mResizeDrawableHeight.getIntrinsicHeight() / 2;
                int heightWidth = mResizeDrawableHeight.getIntrinsicWidth() / 2;

                int xMiddle = mDrawRect.left + ((mDrawRect.right - mDrawRect.left) / 2);
                int yMiddle = mDrawRect.top + ((mDrawRect.bottom - mDrawRect.top) / 2);

                mResizeDrawableWidth.setBounds(left - widthWidth, yMiddle - widthHeight, left + widthWidth,
                        yMiddle + widthHeight);
                mResizeDrawableWidth.draw(canvas);

                mResizeDrawableWidth.setBounds(right - widthWidth, yMiddle - widthHeight, right + widthWidth,
                        yMiddle + widthHeight);
                mResizeDrawableWidth.draw(canvas);

                mResizeDrawableHeight.setBounds(xMiddle - heightWidth, top - heightHeight,
                        xMiddle + heightWidth, top + heightHeight);
                mResizeDrawableHeight.draw(canvas);

                mResizeDrawableHeight.setBounds(xMiddle - heightWidth, bottom - heightHeight,
                        xMiddle + heightWidth, bottom + heightHeight);
                mResizeDrawableHeight.draw(canvas);
            }
        }
    }
}

From source file:com.mikelau.croperino.HighlightView.java

protected void draw(Canvas canvas) {

    if (mHidden) {
        return;//ww  w. java  2  s. c o m
    }

    Path path = new Path();
    if (!hasFocus()) {
        mOutlinePaint.setColor(0xFF000000);
        canvas.drawRect(mDrawRect, mOutlinePaint);
    } else {
        Rect viewDrawingRect = new Rect();
        mContext.getDrawingRect(viewDrawingRect);
        if (mCircle) {

            canvas.save();

            float width = mDrawRect.width();
            float height = mDrawRect.height();
            path.addCircle(mDrawRect.left + (width / 2), mDrawRect.top + (height / 2), width / 2,
                    Path.Direction.CW);
            mOutlinePaint.setColor(0xFFEF04D6);

            canvas.clipPath(path, Region.Op.DIFFERENCE);
            canvas.drawRect(viewDrawingRect, hasFocus() ? mFocusPaint : mNoFocusPaint);

            canvas.restore();

        } else if (mOvalo) {
            canvas.save();

            RectF r = new RectF(mDrawRect.left, mDrawRect.top, mDrawRect.right, mDrawRect.bottom);

            path.addOval(r, Path.Direction.CW);
            mOutlinePaint.setColor(0xFFEF04D6);

            canvas.clipPath(path, Region.Op.DIFFERENCE);
            canvas.drawRect(viewDrawingRect, hasFocus() ? mFocusPaint : mNoFocusPaint);

            canvas.restore();
        } else {

            Rect topRect = new Rect(viewDrawingRect.left, viewDrawingRect.top, viewDrawingRect.right,
                    mDrawRect.top);
            if (topRect.width() > 0 && topRect.height() > 0) {
                canvas.drawRect(topRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect bottomRect = new Rect(viewDrawingRect.left, mDrawRect.bottom, viewDrawingRect.right,
                    viewDrawingRect.bottom);
            if (bottomRect.width() > 0 && bottomRect.height() > 0) {
                canvas.drawRect(bottomRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect leftRect = new Rect(viewDrawingRect.left, topRect.bottom, mDrawRect.left, bottomRect.top);
            if (leftRect.width() > 0 && leftRect.height() > 0) {
                canvas.drawRect(leftRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }
            Rect rightRect = new Rect(mDrawRect.right, topRect.bottom, viewDrawingRect.right, bottomRect.top);
            if (rightRect.width() > 0 && rightRect.height() > 0) {
                canvas.drawRect(rightRect, hasFocus() ? mFocusPaint : mNoFocusPaint);
            }

            path.addRect(new RectF(mDrawRect), Path.Direction.CW);

            mOutlinePaint.setColor(0xFFFF8A00);

        }

        canvas.drawPath(path, mOutlinePaint);

        if (mMode == ModifyMode.Grow) {
            if (mCircle) {
                int width = mResizeDrawableDiagonal.getIntrinsicWidth();
                int height = mResizeDrawableDiagonal.getIntrinsicHeight();

                int d = (int) Math.round(Math.cos(/*45deg*/Math.PI / 4D) * (mDrawRect.width() / 2D));
                int x = mDrawRect.left + (mDrawRect.width() / 2) + d - width / 2;
                int y = mDrawRect.top + (mDrawRect.height() / 2) - d - height / 2;
                mResizeDrawableDiagonal.setBounds(x, y, x + mResizeDrawableDiagonal.getIntrinsicWidth(),
                        y + mResizeDrawableDiagonal.getIntrinsicHeight());
                mResizeDrawableDiagonal.draw(canvas);
            } else {
                int left = mDrawRect.left + 1;
                int right = mDrawRect.right + 1;
                int top = mDrawRect.top + 4;
                int bottom = mDrawRect.bottom + 3;

                int widthWidth = mResizeDrawableWidth.getIntrinsicWidth() / 2;
                int widthHeight = mResizeDrawableWidth.getIntrinsicHeight() / 2;
                int heightHeight = mResizeDrawableHeight.getIntrinsicHeight() / 2;
                int heightWidth = mResizeDrawableHeight.getIntrinsicWidth() / 2;

                int xMiddle = mDrawRect.left + ((mDrawRect.right - mDrawRect.left) / 2);
                int yMiddle = mDrawRect.top + ((mDrawRect.bottom - mDrawRect.top) / 2);

                mResizeDrawableWidth.setBounds(left - widthWidth, yMiddle - widthHeight, left + widthWidth,
                        yMiddle + widthHeight);
                mResizeDrawableWidth.draw(canvas);

                mResizeDrawableWidth.setBounds(right - widthWidth, yMiddle - widthHeight, right + widthWidth,
                        yMiddle + widthHeight);
                mResizeDrawableWidth.draw(canvas);

                mResizeDrawableHeight.setBounds(xMiddle - heightWidth, top - heightHeight,
                        xMiddle + heightWidth, top + heightHeight);
                mResizeDrawableHeight.draw(canvas);

                mResizeDrawableHeight.setBounds(xMiddle - heightWidth, bottom - heightHeight,
                        xMiddle + heightWidth, bottom + heightHeight);
                mResizeDrawableHeight.draw(canvas);
            }
        }
    }
}

From source file:com.android.camera.HighlightView.java

void draw(Canvas canvas) {
    if (mHidden) {
        return;//w w  w  .j ava  2 s .  c om
    }
    canvas.save();
    Path path = new Path();
    if (!hasFocus()) {
        mOutlinePaint.setColor(0xFF000000);
        canvas.drawRect(mDrawRect, mOutlinePaint);
    } else {
        Rect viewDrawingRect = new Rect();
        mContext.getDrawingRect(viewDrawingRect);
        if (mCircle) {
            float width = mDrawRect.width();
            float height = mDrawRect.height();
            path.addCircle(mDrawRect.left + (width / 2), mDrawRect.top + (height / 2), width / 2,
                    Path.Direction.CW);
            mOutlinePaint.setColor(mOutlineCircleColor);
        } else {
            path.addRect(new RectF(mDrawRect), Path.Direction.CW);
            mOutlinePaint.setColor(mOutlineColor);
        }
        canvas.clipPath(path, Region.Op.DIFFERENCE);
        canvas.drawRect(viewDrawingRect, hasFocus() ? mFocusPaint : mNoFocusPaint);

        canvas.restore();
        canvas.drawPath(path, mOutlinePaint);

        if (mMode == ModifyMode.Grow) {
            if (mCircle) {
                int width = mResizeDrawableDiagonal.getIntrinsicWidth();
                int height = mResizeDrawableDiagonal.getIntrinsicHeight();

                int d = (int) Math.round(Math.cos(/*45deg*/Math.PI / 4D) * (mDrawRect.width() / 2D));
                int x = mDrawRect.left + (mDrawRect.width() / 2) + d - width / 2;
                int y = mDrawRect.top + (mDrawRect.height() / 2) - d - height / 2;
                mResizeDrawableDiagonal.setBounds(x, y, x + mResizeDrawableDiagonal.getIntrinsicWidth(),
                        y + mResizeDrawableDiagonal.getIntrinsicHeight());
                mResizeDrawableDiagonal.draw(canvas);
            } else {
                int left = mDrawRect.left + 1;
                int right = mDrawRect.right + 1;
                int top = mDrawRect.top + 4;
                int bottom = mDrawRect.bottom + 3;

                int widthWidth = mResizeDrawableWidth.getIntrinsicWidth() / 2;
                int widthHeight = mResizeDrawableWidth.getIntrinsicHeight() / 2;
                int heightHeight = mResizeDrawableHeight.getIntrinsicHeight() / 2;
                int heightWidth = mResizeDrawableHeight.getIntrinsicWidth() / 2;

                int xMiddle = mDrawRect.left + ((mDrawRect.right - mDrawRect.left) / 2);
                int yMiddle = mDrawRect.top + ((mDrawRect.bottom - mDrawRect.top) / 2);

                mResizeDrawableWidth.setBounds(left - widthWidth, yMiddle - widthHeight, left + widthWidth,
                        yMiddle + widthHeight);
                mResizeDrawableWidth.draw(canvas);

                mResizeDrawableWidth.setBounds(right - widthWidth, yMiddle - widthHeight, right + widthWidth,
                        yMiddle + widthHeight);
                mResizeDrawableWidth.draw(canvas);

                mResizeDrawableHeight.setBounds(xMiddle - heightWidth, top - heightHeight,
                        xMiddle + heightWidth, top + heightHeight);
                mResizeDrawableHeight.draw(canvas);

                mResizeDrawableHeight.setBounds(xMiddle - heightWidth, bottom - heightHeight,
                        xMiddle + heightWidth, bottom + heightHeight);
                mResizeDrawableHeight.draw(canvas);
            }
        }
    }
}

From source file:com.appeaser.sublimepickerlibrary.timepicker.RadialTimePickerView.java

private void drawMinutes(Canvas canvas, float alphaMod) {
    final int minutesAlpha = (int) (mAlpha[MINUTES].getValue() * alphaMod + 0.5f);
    if (minutesAlpha > 0) {
        // Draw the minute selector under the elements.
        drawSelector(canvas, MINUTES, mSelectorPath, alphaMod);

        // Exclude the selector region, then draw minutes with no
        // activated states.
        canvas.save(Canvas.CLIP_SAVE_FLAG);
        canvas.clipPath(mSelectorPath, Region.Op.DIFFERENCE);
        drawTextElements(canvas, mTextSize[MINUTES], mTypeface, mTextColor[MINUTES], mMinutesText,
                mOuterTextX[MINUTES], mOuterTextY[MINUTES], mPaint[MINUTES], minutesAlpha, false, 0, false);
        canvas.restore();//from w  w  w .j a v a  2 s.c o  m

        // Intersect the selector region, then draw minutes with only
        // activated states.
        canvas.save(Canvas.CLIP_SAVE_FLAG);
        canvas.clipPath(mSelectorPath, Region.Op.INTERSECT);
        drawTextElements(canvas, mTextSize[MINUTES], mTypeface, mTextColor[MINUTES], mMinutesText,
                mOuterTextX[MINUTES], mOuterTextY[MINUTES], mPaint[MINUTES], minutesAlpha, true,
                mSelectionDegrees[MINUTES], true);
        canvas.restore();
    }
}