我正在开发一个绘图应用程序,并允许用户导入图像以进一步绘制它。大于绘图区域的图像将被缩小,以满足最大屏幕宽度或屏幕高度。
导入的图像将放置在绘图视图的中心,使用 canvas.drawBitmap(bitmap, x_adjustment, y_adjustment, paintScreen);
这样,导入图像的左、右或上、下就会有空格。调整将从 (0,0) 开始计算
x_adjustment
和y_adjustment
编码:
绘制
@Override
protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(bitmap, x_adjustment, y_adjustment, paintScreen);
for (Integer key : pathMap.keySet())
canvas.drawPath(pathMap.get(key), paintLine); // draw line
}
触摸开始:
private void touchStarted(float x, float y, int lineID)
{
Path path; // used to store the path for the given touch id
Point point; // used to store the last point in path
path = new Path(); // create a new Path
pathMap.put(lineID, path); // add the Path to Map
point = new Point();
previousPointMap.put(lineID, point);
path.moveTo(x, y);
point.x = (int) x;
point.y = (int) y;
}
触摸移动:
// called when the user drags along the screen
private void touchMoved(MotionEvent event)
{
// for each of the pointers in the given MotionEvent
for (int i = 0; i < event.getPointerCount(); i++)
{
// get the pointer ID and pointer index
int pointerID = event.getPointerId(i);
int pointerIndex = event.findPointerIndex(pointerID);
if (pathMap.containsKey(pointerID))
{
// get the new coordinates for the pointer
float newX = event.getX(pointerIndex);
float newY = event.getY(pointerIndex);
// get the Path and previous Point associated with this pointer
Path path = pathMap.get(pointerID);
Point point = previousPointMap.get(pointerID);
float deltaX = Math.abs(newX - point.x);
float deltaY = Math.abs(newY - point.y);
if (deltaX >= TOUCH_TOLERANCE || deltaY >= TOUCH_TOLERANCE)
{
path.quadTo(point.x, point.y, ((newX + point.x)/2),((newY + point.y)/2));
// store the new coordinates
point.x = (int) newX ;
point.y = (int) newY ;
}
}
}
}
触摸结束:
private void touchEnded(int lineID)
{
Path path = pathMap.get(lineID);
bitmapCanvas.drawPath(path, paintLine);
path.reset();
}
问题:
由于导入的图像放置在中心而不是(0,0),因此对于绘制的每条线,虽然在绘制和触摸屏幕时正确显示,当用户移开手指时,即触摸结束,最终线将被 x_adjustment 和 y_adjustment 移动。
例如,如果缩放后的图像宽度<屏幕宽度,左右有空白,画线时显示正确,但当手指移开时,线会立即错误地向右移动x_adjustment
;
我知道这是因为导致错误的调整。我知道这是通过 ax,y shift 来保存路径的坐标。但我不知道如何修改代码,我试图添加对路径的调整但仍然失败。有人可以帮忙给我一些指导吗?非常感谢!