SurfaceView 画图残影问题

hgzlpmg 2014-08-24 09:43:08

我是让一个小圆球从左上角移动到右下角,就出现了这样的情况,小球经过的地方有阴影。
代码:
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

}

}

还请大神指点
...全文
524 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
asia_deng 2014-08-24
  • 打赏
  • 举报
回复
onDraw();不仅会被TestThread 的run调用,还会被这个View的draw调用再绘一次
hgzlpmg 2014-08-24
  • 打赏
  • 举报
回复
自己支持一下啊啊啊

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧