android在surfaceview子类上放置一个图层进行绘制

时间:2014-02-24 17:02:14

标签: android multithreading android-canvas surfaceview

我正在开发一个触摸绘图应用程序。我想要一个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) {}
                    }
                }
            }
}

1 个答案:

答案 0 :(得分:0)

我自己找到了解决方案。

我刚从一个实现onTouchListener的主活动调用了onTouchEvent。 我像这样覆盖了onTouch方法。

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

setOnTouchListener(this);设置为SurfaceViewSubClass1中前景SurfaceView子类onCreate()