1 Replies - 694 Views - Last Post: 18 January 2013 - 11:05 PM

#1 Benzoate  Icon User is offline

  • D.I.C Head

Reputation: 51
  • View blog
  • Posts: 234
  • Joined: 29-February 12

Bitmap Drawing Problems

Posted 18 January 2013 - 10:41 AM

Good day, fellow coders.
I'm somewhat working on a tile engine for a game I'm working on but I'm having issues with my bitmaps not drawing/canvas not being drawn on. Therefore I came here for help because I'm not really sure what to do as I am mostly new to android.

My code is shown below. Also any and all help will be appreciated.

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

public class Update extends SurfaceView implements SurfaceHolder.Callback{

	private MainThread thread;
	private MapSetup mapSetup;
	Bitmap red, green, blue;
	public Update(Context context){
		red = BitmapFactory.decodeResource(getResources(), R.drawable.red);
		blue = BitmapFactory.decodeResource(getResources(), R.drawable.blue);
		green = BitmapFactory.decodeResource(getResources(), R.drawable.green);
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
	public void surfaceCreated(SurfaceHolder holder){
	public void surfaceDestroyed(SurfaceHolder holder){
		boolean retry = true;
				retry = false;
			}catch(InterruptedException e){
	public boolean onTouchEvent(MotionEvent event){
		return true;
	protected void onDraw(Canvas canvas){
		canvas.drawBitmap(red, 100, 100, null);
		for(int x = 0; x <= mapSetup.getXTiles(); x++){
			for(int y = 0; y <= mapSetup.getYTiles(); y++){
				if(mapSetup.tileIds[x][y] == 0){
					canvas.drawBitmap(red, 0, 0, null);
				if(mapSetup.tileIds[x][y] == 1){
					canvas.drawBitmap(green, 25, 25, null);
				if(mapSetup.tileIds[x][y] == 2){
					canvas.drawBitmap(blue, 50, 50, null);
	public void update(){

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class MainThread extends Thread{

	private SurfaceHolder surfaceHolder;
	private Update update;
	private boolean running;
	public void setRunning(Boolean running){
		this.running = running;
	public MainThread(SurfaceHolder surfaceHolder, Update update){
		this.surfaceHolder = surfaceHolder;
		this.update = update;
	public void run(){
		Canvas canvas;
			canvas = null;
				canvas = this.surfaceHolder.lockCanvas();
				if(canvas != null){

import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.app.Activity;

public class MainTileActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(new Update(this));
    public int getWidth(){
    	return getWindowManager().getDefaultDisplay().getWidth();
    public int getHeight(){
    	return getWindowManager().getDefaultDisplay().getHeight();

import java.util.Random;

public class MapSetup {
	private MainTileActivity mta;
	Random rand = new Random();
	int tileTypes = 2;
	String[] tileNames;
	int[][] tileIds;
	public void setTileNames(){
		for(int x = 0; x < tileTypes; x++){
			tileNames[x] = x+".png";
	public int getScreenWidth(){
		return mta.getWidth();
	public int getScreenHeight(){
		return mta.getHeight();
	public int getXTiles(){
		return getScreenWidth()/25;
	public int getYTiles(){
		return getScreenHeight()/25;
	public void setTiles(){
		for(int x = 0; x <= getXTiles(); x++){
			for(int y = 0; y <= getYTiles(); y++){
				int id = rand.nextInt(3);
				tileIds[x][y] = id;

Is This A Good Question/Topic? 0
  • +

Replies To: Bitmap Drawing Problems

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 875
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: Bitmap Drawing Problems

Posted 18 January 2013 - 11:05 PM

Have you ever done any drawing with Swing in standard JavaSE? If so, great. You do not override onDraw() in SurfaceView. at least I see now reason to mix drawing like that. I am not even going to go over your code, because your design seems off, based on what I know. I think you need to take a step back.

Before you even attempt to learn how to draw actively on a SurfaceView, you should know how to draw passively on a View. Drawing passively on a View is like drawing passively to a JPanel in Swing where you override paintComponent(Graphics g), and draw with its provided Graphics object. With a View you override onDraw(Canvas c), and draw with the Canvas object provided.

A SurfaceView works differently. You do not override onDraw. You grab a SurfaceHolder with getHolder(), grab the current Canvas with lockCanvas(), do your drawing, then call unlockCanvasAndPost() to update the SurfaceView. I do see that you are doing this in your Thread.

My advice is to extend View and override onDraw and use a CountdownTimer as a game loop, first. Get your app up and running the way you want, then worry about SurfaceView, if you even need to. I think it just complicates things, as most devices sold within the past two-three years are more than capable of rendering 99% of 2d games with a View and onDraw().
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1