An Introduction to Android Application Programming

stuckwarmersMobile - sans fil

14 déc. 2013 (il y a 3 années et 6 mois)

215 vue(s)

Hands
-
On
-
Android



An Introduction to Android Application Programming

Session 4

Intents

Custom Views

2D Graphics

12/14/2013

1

Hands
-
On
-
Android, Loeb & Angrave 2010

Intent

Pass messages, requests and events between activities within
your application or across the entire system.

Easy to use


Intent intent= new Intent(


"android.intent.action.VIEW",

Uri.parse(“http://www.facebook.com”));

startActivity(intent);


Hardware events (incoming call, bluetooth, camera click…)
broadcast intents.

You can broadcast intent and receive them too.



12/14/2013

Hands
-
On
-
Tutorial, Loeb & Angrave 2010

2

Custom Views

1.
Extend the View class (or subclass)


public void onDraw(Canvas canvas) {


super.onDraw(canvas);


canvas.drawBitmap


canvas.drawArc(…)


canvas.drawRect(…)

}


2. Advanced: Use SurfaceView (see LunarLander demo)




12/14/2013

Hands
-
On
-
Tutorial, Loeb & Angrave 2010

3

Canvas Example Calls

canvas.scale(float,float)

canvas.rotate(float degrees, float x, float y)

canvas.restore() / canvas.save()


canvas.drawBitmap(Bitmap b, float left, float top, Paint p)

canvas.drawLines(float[] pts, Paint p);


pts[ even ] = x values


but no

canvas.draw(drawable)

why?


12/14/2013

Hands
-
On
-
Tutorial, Loeb & Angrave 2010

4

Drawables


drawable.setBounds( float,float,float,float)

drawable.draw(canvas)

12/14/2013

Hands
-
On
-
Tutorial, Loeb & Angrave 2010

5

2D Graphics


Main concepts

Color:

Alpha (Opaqueness), Red, Green, Blue




int solidblue = Color.argb(255,0,0,255)

Paint : brush, style, fill, paint, font, font metrics….

Bitmap: Mutuable and Immutable versions. Boiler plate code to
read in bitmaps. Be careful with huge images.

Paths: Fun! Shape Text, create clipping shapes.

Canvas: A way to draw to the screen or bitmap. Supports 2D
Transforms(scale,translate,rotate).

Drawables: Things that know how to draw to a canvas.


view.invalidate()


the view it needs t be redrawn();




12/14/2013

Hands
-
On
-
Tutorial, Loeb & Angrave 2010

6

Path


Simple Vector Graphics

grayPaint = new Paint();

grayPaint.setColor(Color.LTGRAY);


path= new Path();

path.addCircle(150, 150, 100, Direction.CW);

path.
many other geometric calls available


canvas.drawPath(path, grayPaint);


//drawTextOnPath (
String

text,
Path

path, float hOffset, float vOffset,
Paint

paint)

canvas.drawTextOnPath(“…”, path, 0, 20, greenPaint);

12/14/2013

Hands
-
On
-
Tutorial, Loeb & Angrave 2010

7

Device Independent Pixels

Tip: Use dp units “Device Indepdent Pixel”


1dp = 1/160
th

inch


Use Relative Layout



drawable = getDrawable(R.id.rabbit);


drawable width/height != image. png dimensions on disk !?


12/14/2013

Hands
-
On
-
Tutorial, Loeb & Angrave 2010

8