crash course in android development. 2 content installing the adt hardware and os requirements ...

53
Crash Course in Android Development

Upload: cornelius-pearson

Post on 22-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Crash Course in AndroidDevelopment

2

Content Installing the ADT

Hardware and OS requirements Java ADT Bundle

Eclipse Project Setup Drawing in Android Animation in Android

3

John Casey

Dr John Casey Email: [email protected] Room 3008, building 183 Phone: 815-4321 ext. 6003

4

My Experience Oracle Certified Java Programmer (OCJP) Programmer

P2P, Sockets, Distributed Systems, Information Retrieval Motor Registry System – State of Tasmania Fines and Infringement Notices Database – State of

Tasmania LifeLink - Births, Deaths and Marriages State of NSW Mobile Research Projects …

Educator Deakin University, Melbourne Unitec

5

Android Gremlins

SDK is very complicated There are 4-5 separate components to

install and configure and something can go wrong at each stage

The Android SDK has lots of bugs Android can have weird interactions with

the OS and Hardware Many give up after failing to install and

configure Java, Android and Eclipse

6

First Steps Work out

whether you are running a 32-bit or 64-bit OS

Control Panel->System and Security->System

7

Do you have Java Installed? http://www.oracle.com/technetwork/java/

javase/downloads/index.html

8

Download 32-bit OR 64-bit Version

9

Java already installed? Drop to a Command Prompt

10

Download the ADT Bundle https://developer.android.com/sdk/index.

html The number of bits must match! for OS,

Java and ADT Bundle

11

Download the ADT Bundle Make a cup of tea / coffee while the ADT

Bundle downloads

385 MB later .... Extract the ADT Bundle.

12

Extract the ADT Bundle Extract files to c:\android-adt

13

Open the SDK Manager

14

Update the SDK

Be careful not to download the Google Glass packages Android 4.3.1 API 18 is a good stable version of Android

15

More Coffee ...

16

Run Eclipse

17

Update Eclipse

18

Android Developer Toolkit Lots of steps where things can go wrong Can alternatively build your own

environment OR you can also use Android Studio Beta

Follow the steps outlined above though and you can’t go too far wrong

Haven’t even started coding yet :D

19

Android Need to Know Nice to know a general purpose

programming language like Java / C# Nice to know a bit about Object Oriented

programming Variables + Functions Inheritance and Subclassing

Functions and parameters A bit about XML Have a lot of patience – Lots of bugs +

Gotchas

20

Starting Eclipse Keep the

default workspace:

C:\Program Files\Eclipse Android\Workspace

API19 only available on lab machines

21

New Android Project Use the wizard

... File ->New ->

Other -> Android ->Android Project

22

Match up the version of Android with the version that you downloaded!

Minimum version should also point to Android 18

23

24

25

26

27

28

Android Projects

Main class file is the Activity

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

}

29

Android Structure Source Code folder Generated Code

folder Resource folder

Images Layouts Menus Strings

AndroidManifest.xml

Android Manifest<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.test“ android:versionCode="1“ android:versionName="1.0" >

<uses-sdk android:minSdkVersion="18“ android:targetSdkVersion="18" />

<application

android:allowBackup="true“ android:icon="@drawable/ic_launcher"

android:label="@string/app_name“ android:theme="@style/AppTheme" >

<activity

android:name=".MainActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

31

Android Manifest Defines configuration of the project

using XML Defines project resources

Images and Icons Layouts Menus Hardware needed Etc.

Defines the main activity for the Android Project31

32

First Android Project – Stick Man We will hard code everything to make it

easier Won’t be touching anything in the

resources directory

33

Stick Man and House Create a new View class

34

Stickman and House Extend / inherit

from android.view.View Set the package Class name Superclass

Tick create constructors from super class.

35

Stickman and Housepackage com.example.test;

import android.content.Context;

import android.util.AttributeSet;

import android.view.View;

public class StickmanHouse extends View {

public StickmanHouse(Context context) {

super(context);

}

public StickmanHouse(Context context, AttributeSet attrs) {

super(context, attrs);

}

public StickmanHouse(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

}

36

Drawing in Android Can draw various graphical primitives in

Android using a View classes onDraw(Canvas canvas) method

By default onDraw(Canvas canvas) does not do anything...

37

Drawing in Android The Canvas co-ordinate

system starts at the top-left hand corner of the screen

Paint objects are used to control the size, style and colour of the shapes drawn by the Canvas object

paint.setColor(Color.RED);canvas.drawCircle(left, top, 10, paint);

38

Stickman and House By default the StickmanHouse view does not draw anything The super classes onDraw(Canvas canvas) method does not

do anything You will need to import android.graphics.*; Implement the onDraw method as follows

@Override

protected void onDraw(Canvas canvas) {Paint paint = new Paint();

paint.setColor(Color.RED);

canvas.drawCircle(50, 50, 15, paint);

}

39

Link StickmanView to MainActivitypackage com.example.test;

import android.app.Activity;

import android.os.Bundle;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(new StickmanHouse(getApplicationContext()));

}

}

40

Setting up the Emulator Choose the Java perspective Window -> Open Perspective -> Java

41

Setting up the Emulator Press the Android Virtual Device button

It looks like a little mobile phone

42

Setting up the Emulator Press the new button

43

Create new Virtual Device Set the following

parameters Keep the device

screen smaller – larger devices are very slow

Select an x86 CPU Press OK

44

Start the new Virtual Device Takes

about 5 – 10 minutes to start depending on your hardware

45

Emulator

46

Running your App – Right Click Project

47

Running your App Right Click

Project Select Run As

Android Application

The application will run on your emulator (eventually) ...

48

Activity Build a stickman figure in teams of 2

people, using canvas methods such as: canvas.drawRect(25, 125, 150, 250, paint); canvas.drawLine(200,125,200,200,paint); canvas.drawCircle(200, 115, 15, paint); canvas.drawArc(new RectF(200-10,115-

8,200+10,115+8 ), 0, 180, true, paint);

49

What have we done? A lot on project setup

Create project Create emulator

A bit on object oriented programming

A bit on object creation / allocation

A lot on drawing objects using the canvas and paint objects

Basics of Java language

50

Bouncing Ball Animation Declare global

integer variables Use global vars

to specify co-ordinates of the circle

Use invalidate() method to force a screen redraw

Animation Codeint x = 0;int y = 0;

@Overrideprotected void onDraw(Canvas canvas) {

Paint paint = new Paint();

paint.setColor(Color.RED);canvas.drawCircle(x,y, 15, paint);

x = x +1;y = y +1;

invalidate();}

We need some guard conditions otherwise the circle will cont. to move to the right forever.

Boundary Guardsint x = 150;

int y = 0;

int deltax = 1;

int deltay = 1;

@Override

protected void onDraw(Canvas canvas) {

Paint paint = new Paint();

paint.setColor(Color.RED);

canvas.drawCircle(x,y, 15, paint);

if(x > getWidth())

{

deltax = -1;

}

x = x +deltax;

y = y +deltay;

invalidate();

}

New delta variables to control movement

New guard conditions to reflect ball movement on right hand side

Your job implement guard conditions for the top, bottom and left sides

53

Summary Basic animation

Global variables Math operators Assignments operators If statements

Challenge get more circles on screen using arrays

Get them to collide with each other If you want to learn more take the Mobile

class over Summer / Sem 1, 2015.