2014-11-29 3 views
0

캔버스를 사용하도록보기를 확장했습니다. onDraw() 메서드에서 기본 드로잉을 그립니다. 사용자가 캔버스에서 터치 할 때 캔버스를 onTouchEvent() 메서드 내부에서 사용 했으므로 거기에 이미지를 그려야합니다. 드로잉을 수행하지 않습니다. 코드는 아래에 나와 있습니다. 문제는 무엇이며 어떻게이캔버스 다른 방법으로 이미지를 그릴 수 없습니다.

public class ScreenView extends View(){ 

    static Canvas canvas; 
    Bitmap bm; 

    protected void onDraw(final Canvas canvas) { 
     super.onDraw(canvas); 
     this.canvas = canvas; 
     bm = BitmapFactory.decodeResource(getResources(), 
       R.drawable.ic_launcher); 

     canvas.draw....... 
     ...... 
     ........... 


    } 
    public boolean onTouchEvent(final MotionEvent event) { 

     handleTouches(event.getX(), event.getY()); 

     return false; 
    } 
    public void handleTouches(float x, float y) { 
     xLocTouch = (int) x; 
     yLocTouched = (int) y; 
     Paint paint = new Paint(); 
     paint.setColor(Color.BLACK); 
     canvas.drawBitmap(bm, xLocTouch ,yLocTouched , paint); 
    } 
} 
+0

Canvas onDraw 메서드에서만 (관련 : dispatchDraw(), draw()) – pskink

답변

0

당신은 onTouchEvent 내부 invalidate() 메소드를 호출해야 다음 onDraw() 메서드가 호출됩니다 해결할 수 있습니다, 당신은 당신의 xy 좌표를 저장하고이에 비트 맵을 그린다 좌표는 다음과 같습니다.

public class ScreenView extends View { 
    int xLocTouched; 
    int yLocTouched; 
    Bitmap bm; 

    protected void onDraw(final Canvas canvas) { 
     super.onDraw(canvas); 
     bm = BitmapFactory.decodeResource(getResources(), 
       R.drawable.ic_launcher); 

     //your basic drawings also should depends on xLocTouched and yLocTouched. 

     Paint paint = new Paint(); 
     paint.setColor(Color.BLACK); 
     canvas.drawBitmap(bm, xLocTouched ,yLocTouched , paint); 

    } 

    public boolean onTouchEvent(final MotionEvent event) { 
     xLocTouched = (int) event.getX(); 
     yLocTouched = (int) event.getY(); 

     invalidate(); 

     return false; 
    } 
} 
+0

하지만 이전 터치 데이터가 두 번 터치 된 경우 캔버스에서 손실됩니다. 이전 데이터를 유지하고 다시 다시 그릴 수 있습니까? – programr

관련 문제