마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

캔버스위에 drawBitmap으로 이미지를 그리는데 잔상이 생깁니다.

0 추천
현재 그림판 기능을 가진 캔버스에서

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;
 }
a12243 (560 포인트) 님이 2016년 1월 3일 질문

1개의 답변

0 추천
canvas에 그리는 것은 onDraw안에서만 해야 합니다.
익명사용자 님이 2016년 1월 4일 답변
...