Iam beginner in android game development and i’m currently having some trouble on drawing pixels on the screen using bitmap and canvas.
Here is my code:
package com.example.arkanoid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.TextView;
import android.graphics.*;
public class MainActivity extends Activity {
Bitmap bitmap;
Canvas canvas;
int lastx=0, lasty=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bitmap = Bitmap.createBitmap(400,400,Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
bitmap.prepareToDraw();
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void drawPixel(int x, int y) {
if(bitmap.isMutable()) {
bitmap.setPixel(x,y,Color.rgb(100,100,100));
}
canvas.drawBitmap(bitmap,0,0,null);
}
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
TextView texto = (TextView)findViewById(R.id.textView1);
texto.setText("X: "+x+", Y: "+y);
if(lastx !=x || lasty !=y){
lastx=x;
lasty=y;
drawPixel(x,y);
}
return false;
}
}
When the function drawPixel(int x, int y) is called multiple times the application stops running:

And no pixels are drawn, so what is the error here? thanks for your time.
I forgot the LOG from LogCat, sorry:
according for the logcat you uploaded, you need to check if
x & yare between the bitmap size.