After looking thruw a lot of information, i didn't found a simple way of drawing a (resource) bitmap, and let the bitmap move over the screen. Man, for a simple moving picture, i looked in the api demo's at more then 100 lines of complex code..
The first thought was, that this was more complexer then doing this in other platforms.
But its not.
So i have put together a little example on how to do this, with a few lines of code.
I use 1 class for the ball objects, and for moving the balls over the screen.
ColorBall.java
This class holds the Bitmap, and the position on the screen.
Also he takes care that the ball is not going of the screen. (who knows where he will end up..)
Using java Syntax Highlighting
- public void moveBall(int goX, int goY) {
- // check the borders, and set the direction if a border has reached
- if (coordX > 270){
- goRight = false;
- }
- if (coordX < 0){
- goRight = true;
- }
- if (coordY > 400){
- goDown = false;
- }
- if (coordY < 0){
- goDown = true;
- }
- // move the x and y
- if (goRight){
- coordX += goX;
- }else
- {
- coordX -= goX;
- }
- if (goDown){
- coordY += goY;
- }else
- {
- coordY -= goY;
- }
- }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
Also there is a View to draw the balls on. (can't do without)
DrawView.java
Using java Syntax Highlighting
- package eas.org;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.view.View;
- public class DrawView extends View {
- private ColorBall colorball1, colorball2, colorball3, colorball4, colorball5;
- public DrawView(Context context) {
- super(context);
- setFocusable(true); //not yet necessary, but you never know what the future brings
- // declare each ball with the ColorBall class
- colorball1 = new ColorBall(context,R.drawable.bol_groen);
- colorball2 = new ColorBall(context,R.drawable.bol_rood);
- colorball3 = new ColorBall(context,R.drawable.bol_blauw);
- colorball4 = new ColorBall(context,R.drawable.bol_geel);
- colorball5 = new ColorBall(context,R.drawable.bol_paars);
- }
- @Override protected void onDraw(Canvas canvas) {
- //canvas.drawColor(0xFFCCCCCC); //if you want another background color
- // move the balls at every canvas draw
- colorball1.moveBall(5,3);
- colorball2.moveBall(3,4);
- colorball3.moveBall(2,2);
- colorball4.moveBall(4,5);
- colorball5.moveBall(5,1);
- //draw the balls on the canvas
- canvas.drawBitmap(colorball1.getBitmap(), colorball1.getX(), colorball1.getY(), null);
- canvas.drawBitmap(colorball2.getBitmap(), colorball2.getX(), colorball2.getY(), null);
- canvas.drawBitmap(colorball3.getBitmap(), colorball3.getX(), colorball3.getY(), null);
- canvas.drawBitmap(colorball4.getBitmap(), colorball4.getX(), colorball4.getY(), null);
- canvas.drawBitmap(colorball5.getBitmap(), colorball5.getX(), colorball5.getY(), null);
- // refresh the canvas
- invalidate();
- }
- }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Plus some balls to experiment further with this.
This example is written simple and straight forward, but i hope this helps others who just started.
Cheers




