0

我有带图像的图像视图。我单击图像(onTouchEvent)并获得坐标(x,y)的点并在点之间一一画线:

@Override
public boolean onTouchEvent(MotionEvent event) {
    int positionX = (int) event.getRawX();
    int positionY = (int) event.getRawY() - 80;
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                // Add current touch position to the list of points
                pointsList.add(new Point(positionX, positionY));
                Log.d(TAG, " positionX: " + positionX + " positionY: " + positionY);

                bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

                Canvas canvas = new Canvas(bitmap);
                Paint paint = new Paint();
                paint.setColor(Color.RED);
                paint.setStrokeWidth(3);

                // Iterate on the list
                for (int i = 0; i < pointsList.size(); i++) {
                    Point current = pointsList.get(i);

                    // Draw points
                    canvas.drawCircle(current.x, current.y, 10, paint);
                    Log.d(TAG, " startCurrentX: " + current.x + " startCurrentY: " + current.y);

                    // Draw line with next point (if it exists)
                    if (i + 1 < pointsList.size()) {
                        Point next = pointsList.get(i + 1);
                        canvas.drawLine(current.x, current.y, next.x, next.y, paint);
                        Log.d(TAG, " currentX: " + current.x + " currentY: " + current.y + " nextX: " + next.x + " nextY: " + next.y);
                    }
                }
            }

            setImageBitmap(bitmap);
            break;
    }
    return true;
}

所以我得到了很多点,我必须在所有连接点的区域中切割图像区域。我需要指定区域(连接点区域)中的部分图像。

我如何获得点区域并在imageview中剪切该图像区域?

4

1 回答 1

1

这可能会帮助您:

从列表中的点创建一个路径,并计算由它们表示的多边形的下限和上限。

Path polyPath = new Path();
int minX,minY,maxX,maxY; 
for( Point current : pointsList ){
polypath.lineTo(current.x, current.y); 
//TO DO : also add calculation for minX,minY,maxX,maxY here // 
}
polyPath.lineTo(pointsList.get(0).x, pointsList.get(0).y);
polyPath.close();

然后按如下方式创建裁剪的位图:

Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));

polyPath.setFillType(Path.FillType.INVERSE_WINDING);

Bitmap newBitmap = Bitmap.createBitmap(bitmap, minX, minY, maxX-minX, maxY-minY);
Canvas canvas = new Canvas(newBitmap);

canvas.drawPath(polyPath, paint);   
canvas.drawBitmap(newBitmap, 0, 0, paint);
于 2013-08-08T10:53:58.057 回答