以另一种形状绘制一个形状

时间:2018-02-13 07:03:13

标签: android drawing android-canvas rectangles

首先,我将解释我想要实现的目标。我正在尝试实现裁剪功能。为此,我在画布上绘制一个矩形,然后尝试在矩形内绘制心脏。因此,如果用户最小化/最大化矩形心脏也将最小化/最大化。

这是我的代码。

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

    if (mIsInitialized) {
        setMatrix();
        Matrix localMatrix1 = new Matrix();
        localMatrix1.postConcat(this.mMatrix);
        Bitmap bm = getBitmap();
        if (bm != null) {
            canvas.drawBitmap(bm, localMatrix1, mPaintBitmap);
            // draw edit frame
            drawEditFrame(canvas);
        }
    }
}

private void drawEditFrame(Canvas canvas) {

        mPaintTransparent.setFilterBitmap(true);
        mPaintTransparent.setColor(mOverlayColor);
        mPaintTransparent.setStyle(Paint.Style.FILL);

        Path path = new Path();
        path.addRect(mImageRect.left, mImageRect.top, mImageRect.right, mImageRect.bottom,
                Path.Direction.CW);

        drawHeart(mFrameRect.centerX(), mFrameRect.centerY(), path);
        canvas.drawPath(path, mPaintTransparent);
    }

private Path drawHeart(float width, float height, Path path) {

    // Starting point
    path.moveTo(width / 2, height / 5);

    // Upper left path
    path.cubicTo(5 * width / 14, 0,
            0, height / 15,
            width / 28, 2 * height / 5);

    // Lower left path
    path.cubicTo(width / 14, 2 * height / 3,
            3 * width / 7, 5 * height / 6,
            width / 2, height);

    // Lower right path
    path.cubicTo(4 * width / 7, 5 * height / 6,
            13 * width / 14, 2 * height / 3,
            27 * width / 28, 2 * height / 5);

    // Upper right path
    path.cubicTo(width, height / 15,
            9 * width / 14, 0,
            width / 2, height / 5);

    return path;
}

private void setMatrix() {
    mMatrix.reset();
    mMatrix.setTranslate(mCenter.x - mImgWidth * 0.5f, mCenter.y - mImgHeight * 0.5f);
    mMatrix.postScale(mScale, mScale, mCenter.x, mCenter.y);
    mMatrix.postRotate(mAngle, mCenter.x, mCenter.y);
}

但是,心脏正在左上角,而不是按矩形移动或最小化/最大化。

这是当前图片供参考。

enter image description here

请提供我的提示或任何参考。

编辑我可以在矩形内绘制圆圈,根据矩形大小最小化/最大化,并按矩形移动移动。但我无法对定制的心做同样的事情。这是圆圈的代码。

path.addCircle((mFrameRect.left + mFrameRect.right) / 2,
                (mFrameRect.top + mFrameRect.bottom) / 2,
                (mFrameRect.right - mFrameRect.left) / 2, Path.Direction.CCW);

这里是用矩形绘制圆形的图像,也想用心实现。

enter image description here

1 个答案:

答案 0 :(得分:0)

实现就像你应该在调整大小之前重新获得整个位图大小(高度,宽度),然后最大/最小化该位图的大小。

相关问题