2014-02-24 46 views
0

我正在開發一個觸摸繪圖應用程序。我正在執行一個SurfaceView子類來繪製我的畫布。android爲繪圖添加了一層Surfaceview子類

我的圖紙有兩種類型分爲:

  • 臨時畫布繪製上移動觸摸ACTION_MOVE。
  • 在ACTION_UP上繪製的畫布不應該被清除。

我知道,我可以通過

mSurfaceHolder.lockCanvas(); 

找回我的畫布,但我需要有一個其他層,其畫布是獨立於SurfaceView。

對於ACTION_MOVE的繪圖正常工作。但我想繪製永久形狀,每次進入ACTION_MOVE時都會將其移除。 這是我的代碼:

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) {} 
        } 
       } 
      } 
} 

回答

0

我自己找到了解決方案。

我只是從實現onTouchListener的主要活動中調用onTouchEvent。 和我重寫onTouch方法是這樣的。

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

與在onCreate()設置setOnTouchListener(this);我的前景SurfaceView子SurfaceViewSubClass1

相關問題