Quantcast
Channel: Duckout » Android
Viewing all articles
Browse latest Browse all 3

Android Moving Object

$
0
0

Here I will show you how to handle a moving object in Android.
The object we are talking about is a circle which moves from the left side to the right side and back – all the time.

Android Moving Circle

First of all we have to know how Android manages UI elements.

View and ViewGroup
“All user interface elements in an Android app are built using View and ViewGroup objects.”

Canvas
We are drawing our graphic directly to a canvas so the graphic is not handled by the system. We use the onDraw() method to have control of our animation.

SurfaceView
“The SurfaceView is a special subclass of View that offers a dedicated drawing surface within the View hierarchy. The aim is to offer this drawing surface to an application’s secondary thread, so that the application isn’t required to wait until the system’s View hierarchy is ready to draw.”

Ok enough theory. I recommend to read the API Guides of Android, they are pretty good!

Activity Class:
[code]package de.duckout.movingcircle;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MovingCircle extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//This is the only line you need to add.
//We place our UI with the setContentView method
setContentView(new Circle(this, 50, 50, 25));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_moving_circle, menu);
return true;
}
}
[/code]
Create two more classes.
One should be called Circle, the other should be called MainThread.

Circle:
As we use a SurfaceView we should also implement the SurfaceHolder.Callback interface. This will notify us with information about the underlying surface (created, changed, destroyed).

method description:
onDraw(): as the name says, it’s used to draw :-D we use the canvas standard method to draw a circle.
moveCircle(): Changes the position of the current circle.
clearCircle(): This is very important to remove the circle drawn before by drawing it again in white.
[code]package de.duckout.movingcircle;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Circle extends SurfaceView implements SurfaceHolder.Callback {

private float x;
private float y;
private int radius;
private Paint paint;
private MainThread thread;
private boolean movingRight = true;

public Circle(Context context, float x, float y, int radius) {
super(context);

this.x = x;
this.y = y;
this.radius = radius;

// Tell the SurfaceHolder ( -> getHolder() ) to receive SurfaceHolder
// callbacks
getHolder().addCallback(this);
this.thread = new MainThread(getHolder(), this);

paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLUE);
}

// Specify wheater to move right or left
public void moveCircle() {

if (this.movingRight) {
this.x++;
} else {
this.x–;
}

if (this.x == (getWidth() – this.radius)) {
this.movingRight = false;
} else if (this.x == (0 + this.radius)) {
this.movingRight = true;
}

}

public void onDraw(Canvas canvas) {
canvas.drawCircle(this.x, this.y, this.radius, this.paint);
}

public void clearCircle(Canvas canvas) {
canvas.drawColor(Color.WHITE);
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
}
[/code]
MainThread

This Thread class allows us to manage the move of the Circle.

“In order to draw to the Surface Canvas from within your second thread, you must pass the thread your SurfaceHandler and retrieve the Canvas with lockCanvas()”
[code]package de.duckout.movingcircle;

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

public class MainThread extends Thread {

private SurfaceHolder surfaceHolder;
private Circle circle;

public MainThread(SurfaceHolder surfaceHolder, Circle circle) {
this.surfaceHolder = surfaceHolder;
this.circle = circle;
}

public void run() {
Canvas canvas = null;

while (true) {

try {
canvas = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
circle.clearCircle(canvas);
circle.moveCircle();
circle.onDraw(canvas);
}
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}

}
}
}
[/code]
So the process of the Android app looks like this:

1. Your Android app is drawing a circle
2. Your Android removes the circle again
3. goes a step right or left
4. Begins with step 1.

That’s why it is “moving”. This is also the basic idea of a main loop in a game.

Thanks for reading.

Der Beitrag Android Moving Object erschien zuerst auf Duckout.


Viewing all articles
Browse latest Browse all 3

Trending Articles