0

我在学习 Android 编程时遇到了一些问题。我得到了以下内容:我向其中添加了一个名为“GameView”的新类扩展了 SurfaceView。这个“GameView”类包含“GameThread”类的一个对象,这是我创建的一个线程类。所以现在我的问题是,这个 Thread 类一直在调用我 SurfaceView 的 onDraw() 方法。现在我让 SurfaceView 在屏幕上绘制一个位图,并且在每次调用 onDraw() 时它应该向右移动 10 个像素。但是现在我的问题是,第一个图像仍然在它的位置,并且在右侧出现了另一个 10 像素的图像。它看起来像这样:

https://docs.google.com/file/d/0B7rVL5jhBMejN2VwTWlmaG1XemM/edit?usp=sharing

现在,我如何才能在没有这种克隆图像痕迹的情况下创建平滑移动的 Sprite?

我的代码:主类:

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.RelativeLayout;

    public class MainActivity extends Activity {    

  RelativeLayout rLayout;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rLayout = (RelativeLayout)findViewById(R.id.all);
    rLayout.addView(new GameView(this));

  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
  }

    }

和 GameView 类

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;

    public class GameView extends SurfaceView {

    private Bitmap bmp;
    private SurfaceHolder holder;
    private GameThread gameThread;
    private int x = 11;

    public GameView(Context context) {
    super(context);
    gameThread = new GameThread(this);
    holder = getHolder();
    holder.addCallback(new SurfaceHolder.Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {

        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            gameThread.start();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }
    });
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    }

    @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(bmp, x, 10, null);
    x += 1;
    }



    } 

和游戏循环类:

    import android.graphics.Canvas;

    public class GameThread implements Runnable{
    private GameView gameView;
    private boolean isRunning = false;

    public GameThread(GameView gameView) {
    this.gameView = gameView;
    }

    public void start() {
    isRunning = true;
    new Thread(this, "Game Thread").start();
    }

    @Override
    public void run() {
    while(isRunning) {
        Canvas c = null;
        try {
            c = gameView.getHolder().lockCanvas();
            synchronized (gameView.getHolder()) {
                gameView.onDraw(c);
            }
        } finally {
            if(c != null) {
                gameView.getHolder().unlockCanvasAndPost(c);
            }
        }
    }
    }   
    }

我希望有人可以帮助我,谢谢!

4

1 回答 1

0

Add the following before you draw the sprite:

canvas.drawRGB(0, 0, 0)

You need to reset the background of the canvas before you draw the next frame.

You can also try canvas.drawARGB(100, 0, 0, 0). This will draw a translucent background which will allow some of the previously draw bitmaps to show through. It looks like the sprite is leaving a faint trail behind it.

于 2013-04-27T14:32:40.577 回答