Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Department of Electronic and Information Engineering

EIE 3109 Mobile Systems and Application Development

Assignment

Introduction:

This assignment is to develop a basic graphics and animation appin Android through the use of SurfaceView, Bitmap, Thread, and Arraylist, and to create simple animations onscreen and use TouchEvent for interaction.

Subject Intended Learning Outcomes to be assessed in this assignment:

Part

OC1

OC2

OC3

OC4

I-III

Yes

Yes

Yes

Yes

Table 1

*Refer to the Appendix A for Subject Intended Learning Outcomes and Rubrics*

A.           Open a new project with an Empty View Activity.

B.            Draw on a SurfaceView

1.           SurfaceView provides a drawing surface which you can control its format, draw objects, and display them the way you want.

Create a SurfaceView and draw animage on the SurfaceView.

Create Panel.java

a.           Create a Panel class extends SurfaceView implements

SurfaceHolder.Callback

public class Panel extends SurfaceView implements SurfaceHolder.Callback

b.           Add the following override methods. (press Control+oto select)

i.   protected void onDraw(Canvas) (Note: NOT draw(Canvas)) ii.  public void surfaceChanged(SurfaceHolder, intint, int)

iii. public void surfaceCreated(SurfaceHolder)

iv. public void surfaceDestroyed(SurfaceHolder)

c.           Add a constructor Panel(Context context) and add a SurfaceHolder in the constructor which provides usa canvas we can draw on.

public Panel(Context context) {

super(context);

getHolder().addCallback(this);

}

d.           Declare a Bitmap object as a class variable.

private Bitmap bmp;

e.           Initialize the Bitmap object in the constructor (you may copy the launcher   icon under mipmap folder to drawable folder or use your own graphic file).

bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

f.            In OnDraw Method, put the image (Bitmap object) on the canvas.

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

canvas.drawColor(Color.BLACK);

canvas.drawBitmap(bmp, 10, 10, null);

}

2.           Create aThread class to control the drawing.

Create GameThread.java

a.           Create GameThread class extends Thread.

public class GameThread extends Thread

b.           Declare a SurfaceHolder object and a Panel object for the thread to run and a Boolean variable to control the thread.

private SurfaceHolder surfaceHolder;

private Panel panel;

private boolean run = false;

c.           Add a constructor to receive the SurfaceHolder and Panel objects.

public GameThread(SurfaceHolder surfaceHolder, Panel panel) {

this.surfaceHolder = surfaceHolder;

this.panel = panel;

}

d.           Create a set method for run.

public void setRunning(boolean run) {

this.run = run;

}

e.           Add the override run() method. (Control + oto select)

@Override

public void run() {

super.run();

}

3.           In Panel.java, use the GameThread to draw on Canvas.

a.           Declare a GameThread object as a class variable.

private GameThread thread;

b.           Initialize the GameThread object in the constructor.

thread = new GameThread(getHolder(), this);

c.           Start the thread when the surface is created.

public void surfaceCreated(SurfaceHolder holder) {

thread.setRunning(true);

thread.start();

}

d.           Shutdown and wait for thread to finish.

public void surfaceDestroyed(SurfaceHolder holder) {

boolean retry = true;

thread.setRunning(false);

while(retry) {

try {

thread.join();

retry = false;

} catch (InterruptedException e) {

//keep trying here

}

}

}

4.           In GameThread.java

a.           Implement the run method.  Create a Canvas and lock it.  Then draw on the canvas.  Unlock and post it to the surfaceHolder.

public void run() {

super.run();

Canvas c;

while (run) {

c = null;

try {

c = surfaceHolder.lockCanvas();

synchronized (surfaceHolder) {

panel.onDraw(c);

}

} finally {

if(c != null) {

surfaceHolder.unlockCanvasAndPost(c);

}

}

}

}