1
캔버스에 ontouch 이벤트의 포인트로 직사각형을 그려야합니다. 직사각형 모양이 작동하지만 다른 사각형을 그릴 때 이전 사각형이 사라집니다. 나는 어떤 배열에 직사각형 점을 저장할 필요가 있다고 생각한다. 나에게 몇 가지 아이디어를 제안 해주세요.Android ontouch를 사용하여 캔버스에 사각형을 그립니다.
지금까지 제 코드는 다음과 같습니다. 뷰 클래스 ontouch 방법 제
는
private void onTouchEvent(MotionEvent event) {
xTouchCoordinate = event.getX();
yTouchCoordinate = event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isActionUp = false;
pushState();
startX = event.getX();
startY = event.getY();
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
isActionUp = true;
}
else if (event.getAction() == MotionEvent.ACTION_MOVE){
isActionUp = false;
if(mTouchMode == TouchModes.RECTANGLE)
{
updateRectPath(startX,startY , xTouchCoordinate , yTouchCoordinate);
}
}
}
과의 onDraw 방법은
protected void onDraw(Canvas canvas) {
ImageObject.setInteractiveMode(true);
int sc = canvas.save();
canvas.drawColor(Color.BLACK);
canvas.scale(mCanvasScale, mCanvasScale);
canvas.translate(mCanvasOffset.x, mCanvasOffset.y);
canvas.clipRect(mCanvasLimits);
canvas.drawColor(Color.WHITE);
if (currentState.drawGrid)
drawGridLines (canvas);
drawImages (canvas, true);
drawLines (canvas);
drawRectangles(canvas);
drawCircles(canvas);
drawOvals(canvas);
drawImages (canvas, false);
}
DrawRectangles 방법이다
private void drawRectangles(Canvas canvas) {
try
{
if(path!=null)
{
if(mTouchMode == TouchModes.RECTANGLE)
{
finishPath(canvas);
}
}
}catch(Exception e)
{
}
}
finishpath 방법
void finishPath(Canvas canvas) {
if(mTouchMode == TouchModes.RECTANGLE)
{
paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(currentState.currentColor);
paint.setFlags(Paint.DITHER_FLAG | Paint.ANTI_ALIAS_FLAG);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(currentStrokeWidth);
canvas.drawPath(path, paint);
}
}
void updateRectPath(float x1, float y1, float x2, float y2) {
path.rewind();
x1 = x1 - mCanvasOffset.x;
y1 = y1 - mCanvasOffset.y;
x2 = x2 - mCanvasOffset.x;
y2 = y2 - mCanvasOffset.y;
translate(x1, y1);
Log.d ("x1 is "+x1, "y1 is "+y1);
if (x1 < x2 && y1 > y2) {
path.addRect(x1, y2, x2, y1, Path.Direction.CW);
}
if (x1 < x2 && y1 < y2) {
path.addRect(x1, y1, x2, y2, Path.Direction.CW);
}
if (x1 > x2 && y1 > y2) {
path.addRect(x2, y2, x1, y1, Path.Direction.CW);
}
if (x1 > x2 && y1 < y2) {
path.addRect(x2, y1, x1, y2, Path.Direction.CW);
}
}
,
안녕하세요.이 답변이 귀하의 목적에 부합하는 경우 다른 사람들이 옳은 답변을 알고 사용하도록 수락하시기 바랍니다 .. 감사합니다. – Sushil