使用许多点在图像上构建区域

时间:2013-08-08 09:07:24

标签: java android geometry

我有图像的imageview。 我点击图像(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中剪切此区域?

1 个答案:

答案 0 :(得分: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);