0
我已經創建了一個自定義ImageButton,用於在簡單遊戲中更改對象的方向。當我點擊/觸摸它時,該按鈕沒有響應。 我試過setOnTouchListener和setOnClickListener,但沒有任何反應。Android簡單遊戲。 ImageButton不能點擊。位圖
的CustomButton:
public class CustomButton extends ImageButton {
private Bitmap bitmap;
public CustomButton(Context context, int resource) {
super(context);
setClickable(true);
bitmap = BitmapFactory.decodeResource(context.getResources(), resource);
}
public Bitmap getBitmap() {
return bitmap;
}
GameView:
public class GameView extends SurfaceView implements Runnable {
volatile boolean playing;
private Thread gameThread = null;
private CustomButton mLeftButton;
private Player player;
private Paint paint;
private Canvas canvas;
private Bitmap bitmap;
private SurfaceHolder surfaceHolder;
public GameView(Context context, int screenX, int screenY) {
super(context);
mLeftButton = new CustomButton(context, R.drawable.boom);
mLeftButton.setImageBitmap(mLeftButton.getBitmap());
mLeftButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(),"It worked",Toast.LENGTH_LONG).show();
}
});
player = new Player(context, screenX, screenY);
surfaceHolder = getHolder();
paint = new Paint();
}
@Override
public void run() {
while (playing) {
update();
draw();
control();
}
}
private void update() {
player.update();
}
private void draw() {
if (surfaceHolder.getSurface().isValid()) {
canvas = surfaceHolder.lockCanvas();
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(player.getBitmap(), player.getX(), player.getY(), paint);
canvas.drawBitmap(mLeftButton.getBitmap(), 20, 20, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
你在哪裏添加按鈕來查看。 –
該按鈕在繪製方法中「繪製爲位圖」(?)。 – TheEagle
您在畫布中繪製位圖而不添加按鈕來查看。 –