在android中的图像上徒手剪裁

时间:2013-10-17 04:53:13

标签: android canvas

我正在实现android中图像的徒手裁剪。我可以使用触摸在图像上绘制任意形状,并收集数组列表中路径上的所有点。但我无法在任意形状内提取图像部分。

我搜索了很多,但未能找到合适的答案。是否有任何机构都有这方面的实例。

编辑:我可以使用位图上的以下代码创建任意形状。

@Override
protected void onDraw(Canvas canvas) {


    canvas.drawBitmap(scaledBitmap, 0, 0,null);
    path.reset();


    boolean firstTouchPoint = true;

    for (int i = 0; i < lastPath.size(); i += 2) {
        Point point = lastPath.get(i);

        if (firstTouchPoint) {
            firstTouchPoint = false;
            path.moveTo(point.fXPosition, point.fYPosition);
        } else if (i < lastPath.size() - 1) {
            Point next = lastPath.get(i + 1);
            path.quadTo(point.fXPosition, point.fYPosition, next.fXPosition, next.fYPosition);
        } else {
            path.lineTo(point.fXPosition, point.fYPosition);
        }
    }

    canvas.drawPath(path, paint); 


}

但我无法提取此路径中的位图区域。

1 个答案:

答案 0 :(得分:0)

你应该试试这个:

//the image should support transparency.
Bitmap scaledBitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
// fill the area around the path with alpha
Canvas c = new Canvas(scaledBitmap);
c.clipPath(path, Region.Op.DIFFERENCE);
c.drawColor(0x00000000, PorterDuff.Mode.CLEAR);