我正在尝试开发我的第一个Android游戏。我的问题是,无论我为CharSprite矩形设置什么颜色,它始终显示为黑色。我尝试将绘制样式更改为FILL或FILL和STROKE或使用RGB值设置绘制。我不知道问题是什么。 canvas.drawColor
方法调用非常有效。
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
public class CharSprite {
private Paint paint;
private int xPos;
private int yPos;
private boolean goingDown;
public CharSprite(int xPos, int yPos) {
this.paint = new Paint(Color.RED);
this.goingDown = true;
this.xPos = xPos;
this.yPos = yPos;
}
public void draw(Canvas canvas) {
Rect rect = new Rect(xPos - 50, yPos -50, xPos+50, yPos+50);
canvas.drawRect(rect, this.paint);
}
}
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.method.Touch;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private MainThread thread;
private CharSprite charSprite;
public GameView(Context context) {
super(context);
getHolder().addCallback(this);
thread = new MainThread(getHolder(), this);
setFocusable(true);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
charSprite = new CharSprite(300, 300);
thread.setRunning(true);
thread.start();
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (retry) {
try {
thread.setRunning(false);
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
retry = false;
}
}
public void update() {
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
canvas.drawColor(Color.WHITE);
charSprite.draw(canvas);
}
}