10

I am trying to draw a ball to my screen using 3 classes. I have read a little about this and I found a code snippet that works using the 3 classes on one page, Playing with graphics in Android

I altered the code so that I have a ball that is moving and shifts direction when hitting the wall like the picture below (this is using the code in the link).

moving ball screenshot

Now I like to separate the classes into 3 different pages for not making everything so crowded, everything is set up the same way.

Here are the 3 classes I have.

  1. BallActivity.java
  2. Ball.java
  3. BallThread.java

package com.brick.breaker;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;


public class BallActivity extends Activity {

private Ball ball;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    ball = new Ball(this);
    setContentView(ball);
}

@Override
protected void onPause() {

    super.onPause();

    setContentView(null);
    ball = null;

    finish();
}

}

package com.brick.breaker;

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 Ball extends SurfaceView implements SurfaceHolder.Callback {

private BallThread ballThread = null;

private Bitmap bitmap;

private float x, y;
private float vx, vy;

public Ball(Context context) {
    super(context);

    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ball);

    x = 50.0f;
    y = 50.0f;

    vx = 10.0f;
    vy = 10.0f;

    getHolder().addCallback(this);
    ballThread = new BallThread(getHolder(), this);
}

protected void onDraw(Canvas canvas) {

    update(canvas);

    canvas.drawBitmap(bitmap, x, y, null);
}

public void update(Canvas canvas) {

    checkCollisions(canvas);

    x += vx;
    y += vy;
}

public void checkCollisions(Canvas canvas) {

    if(x - vx < 0) {

        vx = Math.abs(vx);

    } else if(x + vx > canvas.getWidth() - getBitmapWidth()) {

        vx = -Math.abs(vx);
    }

    if(y - vy < 0) {

        vy = Math.abs(vy);

    } else if(y + vy > canvas.getHeight() - getBitmapHeight()) {

        vy = -Math.abs(vy);
    }
}

public int getBitmapWidth() {

    if(bitmap != null) {

        return bitmap.getWidth();

    } else {

        return 0;
    }
}

public int getBitmapHeight() {

    if(bitmap != null) {

        return bitmap.getHeight();

    } else {

        return 0;
    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {

}

public void surfaceCreated(SurfaceHolder holder) {

    ballThread.setRunnable(true);
    ballThread.start();

}

public void surfaceDestroyed(SurfaceHolder holder) {

    boolean retry = true;
    ballThread.setRunnable(false);

    while(retry) {

        try {

            ballThread.join();
            retry = false;

        } catch(InterruptedException ie) {

            //Try again and again and again
        }

        break;
    }

    ballThread = null;

}

}

package com.brick.breaker;

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

public class BallThread extends Thread {

private SurfaceHolder sh;
private Ball ball;

private Canvas canvas;

private boolean run = false;

public BallThread(SurfaceHolder _holder,Ball _ball) {

    sh = _holder;
    ball = _ball;
}

public void setRunnable(boolean _run) {

    run = _run;
}

public void run() {

    while(run) {

        canvas = null;

        try {

            canvas = sh.lockCanvas(null);

            synchronized(sh) {

                ball.onDraw(canvas);
            }

        } finally {

            if(canvas != null) {

                sh.unlockCanvasAndPost(canvas);
            }

        }

    }
}

public Canvas getCanvas() {

    if(canvas != null) {

        return canvas;

    } else {

        return null;
    }
}
}

Here is a picture that shows the outcome of these classes.

enter image description here

I've tried to figure this out but since I am pretty new to Android development I thought I could ask for help.

Does any one know what is causing the ball to be draw like that? The code is pretty much the same as the one in the link and I have tried to experiment to find a solution but no luck.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Morten Høgseth
  • 338
  • 2
  • 4
  • 17

5 Answers5

15

well , as you can see on the image , you only drew the ball . instead , you need to re-drew a black background (or whatever that you wish) before each time you draw the ball.

alternatively , you can draw a black area only on the previous position , but you might have problems with it later , when you use more objects.

here's a nice sample, similar to what you do

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Yepp that fixed my problem, on the onDraw method i added the line canvas.drawColor(Color.BLACK); before i drew the ball so the screen would be filled with black removing the unwanted green ball flicks. Thx alot=) – Morten Høgseth Jun 10 '12 at 12:50
  • btw , for a much smoother and faster animation , i would recommend using opengl . the canvas is only for simple stuff , especially for now that most of the devices still don't have the GPU helping with the graphical stuff , since most are still on "old" android version (gingerbread) . – android developer Jun 10 '12 at 13:42
  • excellent advice=) I will mess around with the above code then create a new project and do the same logic but using openGL. Thx for the tip:) – Morten Høgseth Jun 11 '12 at 18:36
  • some people prefer using third party libraries that use opengl for animations and games , such as libgdx (which can work on the desktop too!) and andengine . – android developer Jun 12 '12 at 10:28
  • also , starting with honeycomb , another alternative to opengl is renderscript : http://android-developers.blogspot.co.il/2011/02/introducing-renderscript.html http://developer.android.com/guide/topics/renderscript/index.html – android developer Jun 12 '12 at 23:14
1

A quick look and I would have to say you are just drawing on the same surface and never requesting your surfaceview to redraw itself. at the end of the finally block, in the IF Statement use: postInvalidate(); That should cause the surface view to redraw itself.

Darshana
  • 2,462
  • 6
  • 28
  • 54
Sorceri
  • 466
  • 2
  • 4
1

put this

public void onDraw(Canvas canvas){
   canvas.drawColor(Color.BLACK);

.....

}
wdog
  • 612
  • 6
  • 7
0

See how i have done the pendulum simulation at http://som-itsolutions.blogspot.in/2012/06/android-graphics-and-animation-pendulum.html

You can clone the source code of this project from https://github.com/sommukhopadhyay/pendulumsimulation

somenath mukhopadhyay
  • 1,548
  • 1
  • 16
  • 18
-1

[edit]The answer was wrong, but the comment was helpful so I'll leave this answer up:

Not the question you asked, but there is a problem in your code. In Android you are only allowed to write to the screen in the UI thread. This is the thread that runs all the Activity callbacks, etc. By writing to the screen from BallThread you are risking many odd failures in your program.

Dale Wilson
  • 9,166
  • 3
  • 34
  • 52