현재 그림판 기능을 가진 캔버스에서
drawBitmap으로 이미지를 추가하고 터치를 이용하여
비트맵 이미지를 이동 시키는 어플을 구현하고자 합니다.
비트맵 이미지를 출력하고 터치로 이동 시키는 것은 되는데
터치를 따라 무브하면서 이미지는 계속그려지지만 이전 이미지는 지워지지
않아 마치 잔상처럼 보이게 됩니다. 이것을 어떻게 해결하면 좋을까요ㅜㅜ
선배개발자님께 질문드립니다. 아래는 제가 사용한 코드중 해당 문제에 해당하는 부분입니다.
변수설명 드리겠습니다.
locationX : 처음 비트맵이 그려질 때 X좌표
locationY : 처음 비트맵이 그려질 때Y좌표
lastLocationX : 비트맵 이미지 터치시 터치이벤트의X값을 저장
lastLocationY : 비트맵 이미지 터치시 터치이벤트의Y값을 저장
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (w > 0 && h > 0) {
newImage(w, h);
}
}
protected void onDraw(Canvas canvas) {
if (mBitmap != null) {
canvas.drawBitmap(mBitmap, 0, 0, null);
}
}
public void sendTool(Bitmap tempBitmap, int scaleX, int scaleY, int locationX, int locationY){
this.locationX = locationX;
this.locationY = locationY;
this.scaleX = scaleX;
this.scaleY = scaleY;
toolImage = Bitmap.createScaledBitmap(tempBitmap, scaleX, scaleY, true);
mCanvas.drawBitmap(toolImage, this.locationX, this.locationY, null);
}
public void drawTool(){
mCanvas.drawBitmap(toolImage, lastLocationX, lastLocationY, null);
locationX = lastLocationX;
locationY = lastLocationY;
}
private void checkImageMove(int eventX, int eventY){
if ((locationX < eventX) && (eventX < locationX+scaleX)){
if ((locationY < eventY) && (eventX < locationY+scaleY)){
bMove = true;
}
}
}
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
eventX = (int) event.getX();
eventY = (int) event.getY();
switch (action) {
case MotionEvent.ACTION_UP:
bMove = false;
changed = true;
lastX = -1;
lastY = -1;
break;
case MotionEvent.ACTION_DOWN:
this.checkImageMove(eventX, eventY);
if(bMove==false){
saveUndo();
if (lastX != -1) {
if (eventX != lastX || eventY != lastY) {
mCanvas.drawLine(lastX, lastY, eventX, eventY, mPaint);
}
}
lastX = eventX;
lastY = eventY;
}
break;
case MotionEvent.ACTION_MOVE:
if(bMove){
lastLocationX = eventX;
lastLocationY = eventY;
drawTool();
} else{
if (lastX != -1) {
mCanvas.drawLine(lastX, lastY, eventX, eventY, mPaint);
}
lastX = eventX;
lastY = eventY;
}
break;
}
invalidate();
return true;
}