
我是让一个小圆球从左上角移动到右下角,就出现了这样的情况,小球经过的地方有阴影。
代码:
public class TestThread extends Thread{
boolean flag = true;
TestDemo testDemo;
SurfaceHolder surfaceHolder;
public TestThread(TestDemo testDemo,SurfaceHolder surfaceHolder){
this.testDemo = testDemo;
this.surfaceHolder = surfaceHolder;
}
@Override
public void run(){
while(flag){
Canvas c = null;
try {
c = surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
testDemo.onDraw(c);
}
} finally {
if(c!=null){
surfaceHolder.unlockCanvasAndPost(c);
}
}
try {
Thread.sleep(200);
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
SurfaceView:
public class TestDemo extends SurfaceView implements SurfaceHolder.Callback{
Bitmap bg;
Bitmap bullet;
SurfaceHolder holder;
Paint paint;
float x = 0;
float y = 0;
TestThread testThread;
public TestDemo(Context context) {
super(context);
holder = this.getHolder();
holder.addCallback(this);
}
public void onDraw(Canvas c){
c.drawBitmap(bg, 0, 0, paint);
c.drawBitmap(bullet, x+=10, y+=10, paint);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
paint = new Paint();
testThread = new TestThread(this,holder);
bg = BitmapFactory.decodeResource( getResources(), R.drawable.bg );
bullet = BitmapFactory.decodeResource( getResources(), R.drawable.bullet);
testThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
还请大神指点