Вопрос об Android SurfaceView.Я хочу нарисовать круг в SurfaceView, но холст нулевой в методе SimpleDraw. Почему?№ 1, холст нулевой.№ 2, метод SimpleDraw запускается в ClickListener, верно, холст не нулевой, почему?
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);
}
}