0% found this document useful (0 votes)
19 views19 pages

Wk5 - MVC and Threads

Uploaded by

Sharon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views19 pages

Wk5 - MVC and Threads

Uploaded by

Sharon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Android MVC

Discipline of IT
James Cook University

1
This presentation focuses on…

1. Android Threads
2. MVC in Android
3. Graphics in Android

2
Handling background tasks

• Mobile Apps are typically multi-threaded


• The main thread is the GUI thread
• Then additional threads are used for handling
long-running background tasks

• Common issues:
• Updating a GUI object from a background
task will crash the App!
• Running task that take a long time on the
GUI thread slows the program down
Using Threads directly

• Use the Runnable interface, or subtype Thread

• Then, to force a GUI update to be handled by the


GUI thread use:
• Activity.runOnUiThread(Runnable)
• View.post(Runnable)
• View.postDelayed(Runnable, long)

• View.postInvalidate()
Why use MVC?

• In a mid-sized app, just putting all of your


code in the Activity class is bad design
– Becomes hard to understand and work on the
code
– Mistakes become more likely
– Harder to test individual parts of the app
– Harder to reuse parts of the app
Model

• You make an app to solve a problem


– E.g. sketch pad, calculator, …
• The “problem-domain” of the app is
represented by the model

• E.g. A game has:


– Players, Pieces, the game Board, …
View

• How the Model is presented to the user


– Visualising the model
– Generating touch events, click events

• Note: each view object (button, relative


layout, custom view) generates its own
events
– If a sub-view does not handle an event, then
the event “bubbles” up to the parent view
Controller

• Typically, each Activity is a Controller for its


associated view and model

• Controller can be used to create the model


and associate it with the view

• The events generated by a view are typically routed


into the Controller
– That’s because the controller knows about the
View and the Model and can send messages (e.g.
update requests) to both of them
“Wiring up”

• To allow the Controller to handle events from the


View, you assign event handlers
– Each View event (click, touch, key) has an
associated “listener” interfaces
– Implementing these listeners is then handled by
a Controller method
Things to note…
• A “good” MVC design has these characteristics:
– The model should not have direct links to the View
or Controller
– The View can have a direct link to the model
– The controller has direct links to the model and view

• The model can then be re-used in our apps


• E.g. you could place the model in a Service
component…
It’s a bad idea for the model to have direct knowledge
of the View

• There are a few solutions:


– Polling: the Controller regularly checks the model
to see if there are changes and tells the View

– Notification: The model has its own “listener”


interface
» The View or Controller can implement the
listener
Example: MyTouchDemo

• Pacman is the “model” - supports a listener interface

• MyView is the “view” (a custom View subtype)

• MainActivity is the “controller”


• Handles Pacman creation
• Handles touch events
Things to note…

• Activities can actually be coded so that they are a


combination of Controller and View logic

• E.g. activity handles View updates directly…


• This would of course not be traditional MVC…
View Layout

• Just before a view is displayed, it has time to


figure out how much screen space it needs

• This is why the constructor is needed in a


custom view subtype
View measurement

• A view figures out its space based on the


onMeasure() method
– Each view/sub-view implements this method
– The outer-most view asks its child views for their
sizes, so it can figure out its size
Canvas drawing

• Every view has an onDraw() method

• When invalidate() is called, this force the view


to run the code in its onDraw() method

• Android uses the “painter’s algorithm”


– Parent drawn first, then child on top (drawing
over the parent)
Canvas drawing

• The onDraw() method needs to be treated carefully


– The code needs to run fast
– Avoid creating objects inside onDraw()

• Useful classes
– Canvas (support draw and transformations)
– Paint
– Bitmap
– Drawable – you can setup drawable objects (i.e.
shapes)
Bling

• The Paint class has a number of powerful features


– Shader – e.g. linear gradients
– ShadowLayer
– ColorFilter

• Animation
– You can implement “tweening”
» Provide start, end, and speed to change visual
attirubtes
• Size, position, scale, color…
MVC – This is the way to make an app in Android

19

You might also like