使用SurfaceView时,锁定画布失败,画布为空

时间:2012-03-01 04:52:23

标签: java android android-canvas

Android SurfaceView的问题。 我想在SurfaceView中绘制一个圆,但在SimpleDraw方法中canvas是null。为什么? 否1,canvas为null。 No 2,SimpleDraw方法在ClickListener中运行,是对的,canvas不是null,为什么?

1.public class SurfaceTestActivity extends Activity {
SurfaceView sfv;
SurfaceHolder sfh;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    sfv=(SurfaceView)findViewById(R.id.surface);
    sfh=sfv.getHolder();
    SimpleDraw();

}

void SimpleDraw(){
    Canvas canvas = sfh.lockCanvas(new Rect(0, 0, 300,
            getWindowManager().getDefaultDisplay().getHeight()));
    Paint mPaint = new Paint();
    mPaint.setColor(Color.GREEN);
    mPaint.setStrokeWidth(2);
    canvas.drawCircle(150, 150, 80, mPaint);
    sfh.unlockCanvasAndPost(canvas);
    }

}

2.public class SurfaceTestActivity extends Activity {
Button simpleButton;
SurfaceView sfv;
SurfaceHolder sfh;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    simpleButton=(Button)findViewById(R.id.simple);
    sfv=(SurfaceView)findViewById(R.id.surface);
    sfh=sfv.getHolder();
    simpleButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //SimpleDraw();
        }
    });
}

void SimpleDraw(){
    Canvas canvas = sfh.lockCanvas(new Rect(0, 0, 300,
            getWindowManager().getDefaultDisplay().getHeight()));
    Paint mPaint = new Paint();
    mPaint.setColor(Color.GREEN);
    mPaint.setStrokeWidth(2);
    canvas.drawCircle(150, 150, 80, mPaint);
    sfh.unlockCanvasAndPost(canvas);
    }

}

1 个答案:

答案 0 :(得分:2)

区别在于时机。

没有。 1尝试在创建之前绘制到画布并失败。对sfh.lockCanvas(...)的调用失败并返回null,而不是Canvas

没有。 2在创建画布后尝试绘制,因此它可以正常工作。

这样做:

public class SurfaceTestActivity extends Activity implements SurfaceHolder.Callback {
SurfaceView sfv;
SurfaceHolder sfh;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    sfv=(SurfaceView)findViewById(R.id.surface);
    sfh=sfv.getHolder();
    sfh.addCallback(this);

}

void SimpleDraw(){
    Canvas canvas = sfh.lockCanvas(new Rect(0, 0, 300,
            getWindowManager().getDefaultDisplay().getHeight()));
    Paint mPaint = new Paint();
    mPaint.setColor(Color.GREEN);
    mPaint.setStrokeWidth(2);
    canvas.drawCircle(150, 150, 80, mPaint);
    sfh.unlockCanvasAndPost(canvas);
}



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

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // draw AFTER surface created!
    simpleDraw();

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

变化:

添加SurfaceHolder.Callback

添加sfh.addCallback(this);

在创建画布后添加3个@Override方法和新SimpleDraw