package my.test1;import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;public class test1Activity extends Activity {
/** Called when the activity is first created. */
private GameView mGameView=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(new GameThread()).start();
System.out.println("Activity.Oncreate");
}
class GameThread implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Thread.run");
while (!Thread.currentThread().isInterrupted()){
try{
Thread.sleep(100);
}
catch(InterruptedException e){
Thread.currentThread().interrupt();
}
System.out.println("postInvalidate 上方");
mGameView.postInvalidate(); }
}
}
public class GameView extends View{ public GameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
} private Paint mPaint = null;
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
System.out.println("onDraw");
mPaint = new Paint();
canvas.drawColor(Color.BLUE);
mPaint.setColor(Color.BLACK);
canvas.drawCircle(40, 70, 30, mPaint);
}
}
}
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;public class test1Activity extends Activity {
/** Called when the activity is first created. */
private GameView mGameView=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(new GameThread()).start();
System.out.println("Activity.Oncreate");
}
class GameThread implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Thread.run");
while (!Thread.currentThread().isInterrupted()){
try{
Thread.sleep(100);
}
catch(InterruptedException e){
Thread.currentThread().interrupt();
}
System.out.println("postInvalidate 上方");
mGameView.postInvalidate(); }
}
}
public class GameView extends View{ public GameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
} private Paint mPaint = null;
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
System.out.println("onDraw");
mPaint = new Paint();
canvas.drawColor(Color.BLUE);
mPaint.setColor(Color.BLACK);
canvas.drawCircle(40, 70, 30, mPaint);
}
}
}
这句你应该设置你的GameView
谢谢您,我试过了,是可以的。可还是有异常。我还想问一下,如果我既要绘图,还要引用 XML 中的布局设置该怎么做。
引用时需要加上package名,例如<my.test1.GameView ...></my.test1.GameView>