Question

I'm developping an app for on touch drawing. I'm impelementig a SurfaceView sublass to draw my canvas.

My drawing are divided in two types:

  • temporary canvas drawing on moving touch ACTION_MOVE.
  • canvas drawn on ACTION_UP that shouldn't be cleared .

I know that I can retrieve my canvas through

mSurfaceHolder.lockCanvas();

but I need to have an other layer whose canvas is independent from SurfaceView.

drawing for ACTION_MOVE works correctly. But I want to draw permanent shape which will be removed each time we got into ACTION_MOVE. Here is my code:

public class DrawSurfaceView  extends SurfaceView implements SurfaceHolder.Callback {

   SurfaceHolder mSurfaceHolder;
    Paint paint = new Paint();
    int startX, startY, endX, endY;
    Canvas canvas = null;
    Thread draw;

    /**
     * Utilisé pour construire la vue depuis XML avec un style
     * @param context le contexte qui héberge la vue
     * @param attrs les attributs définis en XML
     * @param defStyle référence au style associé
     */
     public DrawSurfaceView(Context context, AttributeSet attrs) {
     super(context, attrs);
     mSurfaceHolder = getHolder();
     this.setBackgroundColor(Color.WHITE);                 
     this.setZOrderOnTop(true); //necessary                
     mSurfaceHolder.setFormat(PixelFormat.TRANSLUCENT); 
     mSurfaceHolder.addCallback(this);


     }

     @Override
     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
     }

     @Override
     public void surfaceCreated(SurfaceHolder holder) {

     }

     @Override
     public void surfaceDestroyed(SurfaceHolder holder) {
     }

     @Override
     protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.BLUE);
            paint.setStrokeWidth(3);
            canvas.drawRect(startX, startY, endX, endY, paint);

     }

     @Override
     public boolean onTouchEvent(MotionEvent event) {
         super.onTouchEvent(event);
      // TODO Auto-generated method stub
      int x = (int) event.getX();
      int y = (int) event.getY();
    if(x<0 || y<0){
            return false;
    }else{
      int action = event.getAction();
      switch(action){
      case MotionEvent.ACTION_DOWN:
       startX = x ;
       startY = y;
       endX = x;
       endY = y ;
       break;
      case MotionEvent.ACTION_MOVE:

          endX = x;
           endY = y ;
           draw = new DrawingThread();
           draw.start();
       break;
      case MotionEvent.ACTION_UP:
       endX = x;
       endY = y ;
     //what should Put Here ?????????
       break;
       }
    return true;
        }
     }

     private class DrawingThread extends Thread {
                boolean keepDrawing = true;
                @Override
                public void run() {

                    while (keepDrawing) {
                        Canvas canvas = null;
                        try {
                            canvas = mSurfaceHolder.lockCanvas();
                            synchronized (mSurfaceHolder) {
                                onDraw(canvas);
                            }
                        } finally {
                            if (canvas != null)
                                mSurfaceHolder.unlockCanvasAndPost(canvas);
                        }
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {}
                    }
                }
            }
}
Was it helpful?

Solution

I found a solution by myself.

I just called the the onTouchEvent from a main activity that implements onTouchListener. and I overridden onTouch method like this.

@Override
public boolean onTouch(View v, MotionEvent event) {
    SurfaceViewSubClass1.onTouchEvent(event);       
    SurfaceViewSubClass2.onTouchEvent(event);
    return true;        
}

with setting setOnTouchListener(this); to my foreground SurfaceView subclass SurfaceViewSubClass1 in the onCreate() .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top