我正在制作一个简单的绘画应用程序,这是我第一次使用画布。我的布局 xml 中已经有 SurfaceView,并且 id 是正确的。无论如何,这是我的代码:
package com.example.paint;
import android.app.Activity;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class Paint extends Activity {
/** Called when the activity is first created. */
SurfaceView v;
SurfaceHolder holder;
Canvas c;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
v = (SurfaceView) findViewById(R.id.svDraw);
holder = v.getHolder();
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!holder.getSurface().isValid())
continue;
c = holder.lockCanvas();
c.drawARGB(255, 0, 255, 0);
holder.unlockCanvasAndPost(c);
}
}
}
谢谢。