42
Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Embed Size (px)

Citation preview

Page 1: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Exploring 2D Graphics:The Sudoku Example

Content taken from book:“Hello, Android” by Ed Burnette

Third Edition

Page 2: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Two-dimensional graphics

• Android provides a complete native two-dimensional graphics library in its android.graphics package.

• Android colors are represented with four numbers, one each for alpha, red, green, and blue (ARGB).

Page 3: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Colors

• Each component of the ARGB can have 256 possible values, or 8 bits, so a color is typically packed into a 32-bit integer.

• Alpha is a measure of transparency. The lowest value, 0, indicates the color is completely transparent.

• Values in the middle are used for translucent, or semitransparent, colors.

Page 4: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Color specification

int color = Color.BLUE; // solid blue

// Translucent purplecolor = Color.argb(127, 255, 0, 255);

<?xml version="1.0" encoding="utf-8"?><resources><color name="mycolor">#7fff00ff</color></resources>

color = getResources().getColor(R.color.mycolor);

Page 5: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Paint

• The Paint class holds the style, color, and other information needed to draw any graphics including bitmaps, text, and geometric shapes.

• cPaint.setColor(Color.LTGRAY);

Page 6: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Canvas

• The Canvas class represents a surface on which you draw.

• Initially canvases start off devoid of any content, like blank transparencies for an overhead projector.

• Methods on the Canvas class let you draw lines, rectangles, circles, or other arbitrary graphics on the surface.

Page 7: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Example activity called Graphics

• The display screen is taken up by an Activity, which hosts a View, which in turn hosts a Canvas.

• You get an opportunity to draw on that canvas by overriding the View.onDraw( ) method.

Page 8: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Path

• The Path class holds a set of vector-drawing commands such as lines, rectangles, and curves.

Page 9: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Drawable

• A Drawable class is used for a visual element like a bitmap or solid color that is intended for display only.

• You can combine drawables with other graphics, or you can use them in user interface widgets (for example, as the background for a button or view).

Page 10: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Drawable Forms (1)

• Bitmap: A PNG or JPEG image.• NinePatch: A stretchable PNG image, so

named because originally it divided the image into nine sections. These are used for the

• background of resizable bitmap buttons.• Shape: Vector-drawing commands, based on

Path.

Page 11: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Drawable Forms (2)

• Layers: A container for child drawables that draw on top of each other in a certain order.

• States: A container that shows one of its child drawables based on its state (a bit mask). One use is to set various selection and focus states for buttons.

• Levels: A container that shows only one of its child drawables based on its level (a range of integers). This could be used for a battery or signal strength gauge.

Page 12: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Drawable Forms (3)

• Scale: A container for one child drawable that modifies its size based on the current level. One use might be a zoomable picture viewer.

• Drawables are often defined in XML

Page 13: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

A gradient background defined in XML

Page 14: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Adding Graphics to Sudoku

• Starting the Game• Modify Sudoku.java

private void startGame(int i) {Log.d(TAG, "clicked on " + i);Intent intent = new Intent(this, Game.class);intent.putExtra(Game.KEY_DIFFICULTY, i);startActivity(intent);}

Page 15: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Game.java

Page 16: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Modify Manifest and String files

• AndroidManifest.xml

• res/values/strings.xml

Page 17: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

PuzzleView class

Page 18: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Drawing the Board

• Res/values/colors.xml

• PuzzleView.java

Page 19: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Draw the board

Page 20: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Drawing the Numbers

Page 21: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Handling Input

• Android phones come in many shapes and sizes and have a variety of input methods.

• They might have a keyboard, a D-pad, a touch screen, a trackball, or some combination of these.

• A good Android program, therefore, needs to be ready to support whatever input hardware is available, just like it needs to be ready to support any screen resolution.

Page 22: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Defining and Updating the Selection

• This code shows the player which tile is currently selected

• We use the selection rectangle calculated earlier in onSizeChanged( ) to draw an alpha-blended color on top of the selected tile

Page 23: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Mofify PuzzleView.java

• Move the selection by overriding the onKey-Down( ) method@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {Log.d(TAG, "onKeyDown: keycode=" + keyCode + ", event="+ event);switch (keyCode) {case KeyEvent.KEYCODE_DPAD_UP:select(selX, selY - 1);break;case KeyEvent.KEYCODE_DPAD_DOWN:select(selX, selY + 1);break;case KeyEvent.KEYCODE_DPAD_LEFT:select(selX - 1, selY);break;case KeyEvent.KEYCODE_DPAD_RIGHT:select(selX + 1, selY);break;default:return super.onKeyDown(keyCode, event);}return true;}

Page 24: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

directional pad (D-pad)

• you use the invalidate( ) method to mark rectangles as dirty.

• The window manager will combine all the dirty rectangles at some point in the future and call onDraw( ) again for you.

• The dirty rectangles become the clip region, so screen updates are optimized to only those areas that change.

Page 25: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Entering Numbers

• To handle keyboard input, we just add a few more cases to the onKey-Down( ) method for the numbers 0 through 9 (0 or space means erase the number).

Page 26: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

D-pad

Page 27: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

a call to setSelectedTile( ) to changethe number on a tile

Page 28: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Adding Hints

Page 29: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Hints

• There are three states for zero, one, and two possible moves.

• If there are zero moves, that means the player has done something wrong and needs to backtrack.

Page 30: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Animation

• PuzzleView.java• This loads and runs a resource called

R.anim.shake, defined in res/anim/• shake.xml, that shakes the screen for 1,000

milliseconds (1 second) by 10 pixels from side to side.

cycle_7.xml

Page 31: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Keypad

• The keypad is handy for phones that don’t have keyboards.

• It displays a grid of the numbers 1 through 9 in an activity that appears on top of the puzzle.

• The whole purpose of the keypad dialog box is to return a number selected by the player.

Page 32: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Creating the Keypad

UI layout from res/layout/keypad.xml

Page 33: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Keypad class

Page 34: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

findViews in Keypad.java

Page 35: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

setListeners() in Keypad.java

Page 36: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

onKeyDown() in Keypad.java

Page 37: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

isValid and returnResult() in Keypad.java

Page 38: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Game.Java

Page 39: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition

Implementing the Game Logic

• In Game.java

Page 40: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition
Page 41: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition
Page 42: Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition