Skip to content

Instantly share code, notes, and snippets.

@clamytoe
Last active August 29, 2015 14:27
Show Gist options
  • Save clamytoe/51343bfafc7eb2580acd to your computer and use it in GitHub Desktop.
Save clamytoe/51343bfafc7eb2580acd to your computer and use it in GitHub Desktop.
HKUSTx: COMP107x Introduction to Mobile Application Development using Android
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise: Shooting Game with SurfaceView\n",
"---\n",
"## Objectives\n",
"\n",
"In this exercise you will continue to learn more about 2D graphics support in Android using a SurfaceView, and the use of the SurfaceHolder.Callback methods and canvas. At the end of this exercise you will be able to:\n",
"\n",
"* Define custom views in Android by subclassing the SurfaceView class and use them in your UI\n",
"* Demonstrate the use of a background thread to offload the update of the canvas from the main UI thread\n",
"* Update the canvas in the background thread and then post the change to the UI\n",
"\n",
"## Introduction\n",
"\n",
"This project further demonstrates the use of a SurfaceView class and the update of the canvas in a background thread in order to create 2D graphics in Android for implementing a simple 2D shooting game.\n",
"\n",
"## Textual Instructions\n",
"---\n",
"### Step 1: Examining the Project Files\n",
"\n",
"1. Download the [ShootingGameSurfaceView.zip](http://w02.hkvu.hk/edX/COMP107x/w4/ShootingGameSurfaceView.zip) file, unzip it and import the project into Android Studio.\n",
"2. Open the **DrawView.java** file and follow along the explanation in the video on how the DrawView class is updated to use the SurfaceView.\n",
"3. Check on the implementation of the various SurfaceHolder callback methods: *SurfaceCreated*, *SurfaceChanged* and *SurfaceDestroyed*.\n",
"\n",
"---\n",
"### Step 2: Updating the DrawView\n",
"\n",
"1. As per the instructions in the video, update the run() method of the DrawViewThread class as shown below.\n",
"\n",
" Canvas canvas = null;\n",
"\n",
" while (threadIsRunning) {\n",
"\n",
" try {\n",
" canvas = surfaceHolder.lockCanvas(null);\n",
"\n",
" synchronized(surfaceHolder){\n",
" drawGameBoard(canvas);\n",
" }\n",
" sleep(30);\n",
" } catch (InterruptedException e) {\n",
" e.printStackTrace();\n",
" }\n",
" finally {\n",
" if (canvas != null)\n",
" surfaceHolder.unlockCanvasAndPost(canvas);\n",
" }\n",
" }\n",
"\n",
"2. Run the application to see the result."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment