一个简单的绘图示例

时间:2012-09-28 10:06:49

标签: android

这段代码应该是绘制的东西,但是当我移动手指时它不会绘制任何东西。请帮我调试一下。

  @Override

  protected void onDraw(Canvas canvas) {
    canvas.drawPath(path, paint);
  }
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        path.moveTo(eventX, eventY);
        return true;
      case MotionEvent.ACTION_MOVE:
        path.lineTo(eventX, eventY);
        break;
      case MotionEvent.ACTION_UP:
        // nothing to do 
        break;
      default:
        return false;
    }

    // Schedules a repaint.
    invalidate();
    return true;
  }
} 

2 个答案:

答案 0 :(得分:3)

试试这个

@Override

  protected void onDraw(Canvas canvas)
  {
    canvas.drawPath(path, paint);
  }
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        path.moveTo(eventX, eventY);
        path.lineTo(eventX, eventY);
        return true;
      case MotionEvent.ACTION_MOVE:
        path.quadTo(eventX, eventY, (x + eventX)/2, (y + eventY)/2);
        break;
      case MotionEvent.ACTION_UP:
        canvas.drawPath(path, paint);
        // kill this so we don't double draw
     path.reset();
        break;
      default:
        return false;
    }

    // Schedules a repaint.
    invalidate();
    return true;
  }
} 

有关详细信息,请查看

http://code.google.com/p/strokesformartians/source/browse/trunk/src/se/combitech/strokesformartians/drawing/FingerPaint.java?r=120

这可能会对你有帮助。

答案 1 :(得分:1)

这是我的工作示例,你可以自由地画画。

public class CanvasDrawingActivity extends  Activity   
{
    private ArrayList<Path> _graphics = new ArrayList<Path>();
    private Paint mPaint;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(new DrawingPanel(this));
        mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setColor(0xFFFFFF00);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(3);

    }

    class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback 
    {
        private DrawingThread _thread;
        private Path path;

        public DrawingPanel(Context context) 
        {
            super(context);
            getHolder().addCallback(this);
            _thread = new DrawingThread(getHolder(), this);
        }


        public boolean onTouchEvent(MotionEvent event) 
        {
            synchronized (_thread.getSurfaceHolder()) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    path = new Path();
                    path.moveTo(event.getX(), event.getY());
                    path.lineTo(event.getX(), event.getY());
                }else if(event.getAction() == MotionEvent.ACTION_MOVE){
                    path.lineTo(event.getX(), event.getY());
                }else if(event.getAction() == MotionEvent.ACTION_UP){
                    path.lineTo(event.getX(), event.getY());
                    _graphics.add(path);
                }

                return true;
            }
        }

        @Override
        public void onDraw(Canvas canvas) {
            for (Path path : _graphics) {
                //canvas.drawPoint(graphic.x, graphic.y, mPaint);
                canvas.drawPath(path, mPaint);
            }
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                                   int height) {
            // TODO Auto-generated method stub

        }

        public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            _thread.setRunning(true);
            _thread.start();
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            boolean retry = true;
            _thread.setRunning(false);
            while (retry) {
                try {
                    _thread.join();
                    retry = false;
                } catch (InterruptedException e) {
                    // we will try it again and again...
                }
            }
        }
    }

    class DrawingThread extends Thread {
        private SurfaceHolder _surfaceHolder;
        private DrawingPanel _panel;
        private boolean _run = false;

        public DrawingThread(SurfaceHolder surfaceHolder, DrawingPanel panel) {
            _surfaceHolder = surfaceHolder;
            _panel = panel;
        }

        public void setRunning(boolean run) {
            _run = run;
        }

        public SurfaceHolder getSurfaceHolder() {
            return _surfaceHolder;
        }

        @Override
        public void run() {
            Canvas c;
            while (_run) {
                c = null;
                try {
                    c = _surfaceHolder.lockCanvas(null);
                    synchronized (_surfaceHolder) {
                        _panel.onDraw(c);
                    }
                } finally {
                    // do this in a finally so that if an exception is thrown
                    // during the above, we don't leave the Surface in an
                    // inconsistent state
                    if (c != null) {
                        _surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }
    }
}

输出如下,

  

enter image description here