无法在surfaceview onDraw上绘制

时间:2012-03-09 10:23:29

标签: android canvas surfaceview

您好我正在使用SurfaceView在画布上绘图。当我像这样使用时

canvas.drawCircle(100, 100, 5, paint); //it is drawing fine

虽然我这样使用:

canvas.drawCircle(x, y, 5, paint); //it's not drawing.x,y being some points on screen and they are floats.

任何帮助

3 个答案:

答案 0 :(得分:0)

检查您的x和y坐标是否为正且位于视图范围内:

x >= 0 && x < canvas.getWidth() && y >= 0 && y < canvas.getHeight()

答案 1 :(得分:0)

         @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.restoreToCount(1);
            canvas.clipRect(new Rect(0, 0, getWidth(), getHeight()));
            canvas.drawCircle(x, y, 10, paint);
            canvas.save();      
        }
        float x,y;
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            x=event.getX();
            y=event.getY();
            invalidate();
            return true;
        }

答案 2 :(得分:0)

默认情况下,某些类型的视图不会重绘。如果您希望这样做,请在视图中实施SurfaceHolder.Callback并添加:

@Override
public void surfaceCreated(SurfaceHolder holder) {
    setWillNotDraw(false);
}