Android プログラミングの学習に問題があります。私は次のことを得ました: SurfaceView を拡張する "GameView" と呼ばれる新しいクラスを追加した RelativeLayout。この「GameView」クラスには、私が作成した Thread クラスである「GameThread」クラスのオブジェクトが含まれています。だから今私の問題は、この Thread クラスが私の SurfaceView の onDraw() メソッドを呼び出し続けることです。ここで、SurfaceView にビットマップを画面に描画させ、onDraw() 呼び出しごとに右に 10 ピクセル移動する必要があります。しかし、今私の問題は、最初の画像がその位置に残り、10ピクセル右に別の画像が表示されることです。次のようになります。
https://docs.google.com/file/d/0B7rVL5jhBMejN2VwTWlmaG1XemM/edit?usp=sharing
クローン画像の痕跡を残さずに、スムーズに動くスプライトを作成するにはどうすればよいでしょうか?
私のコード:メインクラス:
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);
}
}
}
}
}
誰かが私を助けてくれることを願っています、ありがとう!