An Introduction to Android

quidnuncoceanΚινητά – Ασύρματες Τεχνολογίες

10 Δεκ 2013 (πριν από 3 χρόνια και 6 μήνες)

71 εμφανίσεις

An Introduction to Android
Richard Leggett - Valis Interactive
Thursday, 27 May 2010
Milton
Keynes
Thursday, 27 May 2010
Cupcake, Donut, Eclair, Froyo... Gingerbread
1.5, 1.6, 2.0/2.1, 2.2... and beyond.
Thursday, 27 May 2010
Why?
Thursday, 27 May 2010
Official HTC Dev Phones
ADP1/
G1/Dream
HVGA
528MHz
192MB RAM
ADP2/
Magic
HVGA
528MHz
288MB RAM
WVGA
1GHz
512MB RAM
Thursday, 27 May 2010
Getting Started
Set up Eclipse with ADT (
Android Dev Tool
)
Downloading the
SDK
, updating the SDKs.
Quick overview of the SDK folder
Create AVMs - naming conventions
Run the emulator
Create a project
http://developer.android.com
http://www.eclipse.org/downloads/
Thursday, 27 May 2010
Application Structure
MyMapApplication
MenuActivity
MapActivity
View
View
View
Manifest
Bitmaps
Values
Layouts
Resources
RAW
Thursday, 27 May 2010
Activity Lifecycle
void onCreate(Bundle savedInstanceState)
void onStart()
void onRestart()
void onResume()
void onPause()
void onStop()
void onDestroy()
http://developer.android.com/guide/topics/fundamentals.html#lcycles
Thursday, 27 May 2010
Game Run-through
Thursday, 27 May 2010
Application Types
Along with
Activities
, we also have
Services
, which run in the
background and
Broadcast
Receivers
, which run long enough
to start something else up.
Thursday, 27 May 2010
Intents/Actions
Android is fully customisable,
through the system of Intents any
application can register to handle an
intent, like

"share photo"
or
"receive
call"
.
Think *nix piping.
You specify these in the Application
manifest

XML.
Thursday, 27 May 2010
Android App Building 101
Giving applications a face...
Layouts
Controls
Code
Thursday, 27 May 2010
Layouts
Use XML files to define your
GUIs, use code, or both...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="6dip">

<ImageView
android:id="@+id/icon"

android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"

android:src="@drawable/icon" />
<LinearLayout
android:orientation="vertical"...
Thursday, 27 May 2010
Hello World – Push the Button
Introducing...
setContentView(R.layout.
main
);

Button button = (Button)findViewById(R.id.
Button01
);
button.setOnClickListener(
new
View.OnClickListener() {
public

void
onClick(View v) {
Toast.makeText(OnClickApp.
this
,
"Hello World!"
,
Toast.
LENGTH_LONG
).show();
}
});
Demo
LinearLayout
Button
OnClickListener
Toasting!
Thursday, 27 May 2010
Layouts
Gravity

(internal)
Layout Gravity/Width/Height
(external)
Weight

("percent-based" layout)
Demo (linear_layout_7.xml)
Thursday, 27 May 2010
Resources
Types of resource:
"drawable"
- bitmaps, animations (frame-based
XML), styles (XML)
"layout"
- XML files that contain composited
components or screens
"raw"
e.g. an MP3, video or bitmap you do not want
to process
"values"
e.g. localised text, colours, numbers,
configuration (XML)
Demo (Drinking Game)
Thursday, 27 May 2010
Referencing Resources
Generate a resource ID with "@+":
@+id/logo
ADT generates a static class
"R"
which lists all IDs.
Reference a resource in XML with "@type/":
@drawable/logo
@layout/mylayout
Reference a resource in code:
setContentView(R.layout.mylayout);
Demo (Drinking Game)
Thursday, 27 May 2010
Skinning Controls
Themes
- Apply to Application/Activity,
reference Styles
Styles
- Apply to types of Controls or
individual Controls
Complete overhaul
- see battery_status.xml
alias
Demo (Drinking Game)
Thursday, 27 May 2010
NinePatch Bitmaps
NinePatch
allows you to create bitmaps
which define stretchable regions.
Demo (draw9patch tool, XML selector Style)
Thursday, 27 May 2010
Screen Resolutions
320x240 – QVGA
One or two budget phones
320x480 – HVGA
Most existing non-widescreen phones (HTC
G1/Magic)

480x800 - WVGA
Most newer models
(Nexus One, Desire, Galaxy, X10)
Thursday, 27 May 2010
Low (LDPI)
- 120 dpi -
Budget/Just the HTC Tattoo
Medium ADP1/2
- 160dpi -
Most phones before now, e.g.
HTC G1/Magic <- Default in SDK
High
- 240dpi -
Nexus One, Desire, Droid, Galaxy
Density
has no meaning to an image file, only to the device
(paper or screen) that is displaying it.
Your monitor is most likely
96-120dpi
, which is why things
look sharper/smaller on the phone, depending on screen
size, i.e. iPhone and iPad look about the same as normal,
but Nexus One looks extremely crisp by jamming 384000
pixels into just 3.5 inches.
Screen Densities
Thursday, 27 May 2010
Auto Scaling
By default, the Android compiler will attempt to
auto-scale
graphics for HDPI devices
to make them appear the same
size.
Or you can use the following application tag:
<supports-screens
android:largeScreens="false"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true" />
This will allow us to use certain words within our folders to
tell the resource manager what assets to use, e.g. special
HDPI images.
HDPI
bitmaps should be
1.5x bigger
than MDPI to give the
same physical size (240/160 = 1.5)
Thursday, 27 May 2010
Tips: Resolution Independence
Prefer
wrap_content
,
fill_parent
and the
dip

unit to px in XML layout files
Avoid
FrameLayout
(prefer Linear/Relative)
Use density and/or
resolution specific
resources
(demo)
http://developer.android.com/guide/practices/screens_support.html
Thursday, 27 May 2010
Switching Activities
You can launch another
Activity

(with flags) and it is
pushed

onto
the
"activity stack"
.
Calling
finish()
removes an Activity
from the stack.
Intent intent =
new
Intent(
this
, Activity2.
class
);
startActivity(intent);


finish();
Demo
Thursday, 27 May 2010
Graphics – Check out the APIDemos
Controls
- ImageView or
control.background = …
Canvas
– View.draw()
OpenGL ES
- SurfaceView
(secondary Thread)
Demo (Compass)
Thursday, 27 May 2010
Animation
Android builds animation right into
the base View class:
RotateAnimation anim =
new
RotateAnimation(0, 360,
Animation.
RELATIVE_TO_SELF
, 0.5f,
Animation.
RELATIVE_TO_SELF
, 0.5f);
anim.setDuration(500);
anim.setInterpolator(
new

AccelerateDecelerateInterpolator());
button.startAnimation(anim);
Demo
Thursday, 27 May 2010
Animation
Other types of Animation include:
TranslateAnimation
ScaleAnimation
AlphaAnimation (fade)
AnimationSet (combine animations)
...and custom animations
Types of Interpolator include:
AnticipateOvershootInterpolator - starts backwards then
overshoots and finishes on end value
CycleInterpolator - repeats the animation, e.g. a sine wave
BounceInterpolator
Thursday, 27 May 2010
Timing - Timers
Timer timer =
new
Timer();
timer.scheduleAtFixedRate(
new
TimerTask() {
public

void
run() {

handler
.post(
new
Runnable() {

public

void
run() {
timerTick();
}
});
}
}
, 800L, 250L);
//delay, frequency
Demo
Thursday, 27 May 2010
Timing - Handlers
A
Handler
allows you to send and process
Message
and
Runnable objects
associated with
a thread's
MessageQueue
.
Each Handler instance is associated with a
single thread and that thread's message queue.
When you create a
new Handler
, it is
bound to
the thread
/ message queue of the thread that is
creating it -- from that point on, it will
deliver
messages
and
runnables
to that message
queue and execute them as they come out of
the message queue.
Thursday, 27 May 2010
Timing - Handlers
Handler send/postDelayed()
mHandler
=
new
Handler() {
public

void
handleMessage(Message msg) {

super
.handleMessage(msg);

if
(msg.
what
==
TIMER_MSG
) {
sendTimerMessage();
}
}
};
sendTimerMessage();
private

void
sendTimerMessage() {
timerTick();
mHandler
.sendEmptyMessageDelayed(
TIMER_MSG
, 250L);
}
Why is it better?
http://developer.android.com/resources/articles/timed-ui-updates.html
Demo
Thursday, 27 May 2010
Threads
A
second Thread
allows you to run
code
without
choking the app,
causing a
Force Quit
.
e.g. Perform high FPS
drawing
or
number crunching
in another
Thread, leaving the
Activity
thread
free
to respond to the OS.
Thursday, 27 May 2010
Threads
Handler objects allow you to
"post"

messages to execute code
across
Threads
.
This lets you deal with events at
your own pace,
without blocking
the
UI.
Thursday, 27 May 2010
Tiers
You don't have to write
Java
to
make use of all this.
Android lets you expose Java
classes to JavaScript for
HTML/JS

apps, or
C-routines
to Java
Activities.
Build apps in whatever tech you
prefer and use underlying layers to
provide
key functionality
.
Thursday, 27 May 2010
Game Engines
Android supports
OpenGL
ES 1.1
and 2.0 for high performance
graphics
.
Rokon
and
Cocos2D
both provide
you with easy to use Sprites, input
handling and an
engine
to draw
these at a consistent FPS.
http://rokonandroid.com
http://code.google.com/p/cocos2d-android/
Demo
Thursday, 27 May 2010
Threads/Handlers
private
Handler
mHandler
;
// in another thread that is drawing graphics at 30FPS...
mHandler
.post(
new
Runnable() {

public

void
run() {
gameOver();
}
});
R o k o n u s e s t h i s t o i n f o r m y o u r
g a m e A c t i v i t y o f
"e n g i n e e v e n t s"
l i k e
c o l l i s i o n s a n d d r a w p h a s e s.
Thursday, 27 May 2010
Tip: Testing
Test on a
REAL
WVGA (HDPI) fast
phones
and HVGA (MDPI) slow
phone as well as the emulator...
It's not as bad as people make out if
you stick to the recommended
methods but... (Droid 24bit PNG)
Thursday, 27 May 2010
Tip: Catch Null Pointer Exceptions
Open
DEBUG
perspective and click
the
"J!" icon
.
Thursday, 27 May 2010
Tip: Don't Create Objects
Object allocation
in mobile ==
garbage collection
==
death
of FPS.
Avoid creating objects while the
game is in the swing of things.
Thursday, 27 May 2010
Questions
Fire away...
Thursday, 27 May 2010