Part 4

0
0
2573 days ago, 745 views
PowerPoint PPT Presentation
Android hues are spoken to with four number, one each for alpha, ... In Android, the showcase screen is taken up by an Activity, which has a ...

Presentation Transcript

Slide 1

Section 4 Exploring 2D Graphics "Hi, Android"

Slide 2

1. Taking in the Basics Color class - Android hues are spoken to with four number, one each for alpha, red, green, and blue (ARGB). - Alpha is a measure of straightforwardness. (the most minimal esteem is 0) - utilizing static constants - utilizing static processing plant strategies - characterizing in a XML asset document

Slide 3

1. Taking in the Basics Paint class - It holds the style, shading, and other data expected to draw any representation including bitmaps, content, and geometric shapes. Canvas class - the Canvas class speaks to a surface on which you draw. - In Android, the show screen is taken up by an Activity , which has a View , which thusly has a Canvas . - To draw on that canvas by abrogating the View.onDraw() technique. - The main parameter to onDraw() is a canvas on which you can draw. Have HOST ACTIVITY View Canvas

Slide 4

1. Taking in the Basics Path class - It holds an arrangement of vector-drawing orders, for example, lines, rectangles, and bends.

Slide 5

1. Taking in the Basics Drawable class - In Android, a Drawable class is utilized for a visual component like a bitmap or strong shading that is proposed for show as it were. - You can join drawables with different design, or you can utilize them in UI gadgets (for instance, as the foundation for a catch or view). - Drawables can take an assortment of structures: * Bitmap : A PNG or JPEG picture. * NinePatch : A stretchable PNG picture. These are utilized for the foundation of resizable bitmap catches. * Shape : Vector-drawing summons, in view of Path. * Layers : A compartment for kid drawables that draw on top of each other in certain z-arrange. * States : A holder that shows one of its youngster drawables in view of its express (a bit veil). * Levels : A compartment that shows one and only of its tyke drawables in view of its level (a scope of whole numbers). * Scale : A holder for one kid drawable that adjusts its size in light of the present level.

Slide 6

1. Taking in the Basics - Drawables are frequently characterized in XML.

Slide 7

2. Adding Graphics to Sudoku Starting the Game - The extraData zone is a guide of key/esteem parts that will be passed along to the purpose. - The keys are strings, and the qualities can be any primitive sort, cluster of primitives, Bundle, or a subclass of Serializable or Parcelabe. [Sudoku.java]

Slide 8

2. Adding Graphics to Sudoku Defining the Game Class [Game.java]

Slide 9

2. Adding Graphics to Sudoku - The calculateUsedTiles() strategy utilizes the standards of Sudoku to make sense of, for every tile in the 9x9 lattice, which numbers are not legitimate for the tile be- - cause they show up somewhere else in the flat or vertical course or in the 3x3 subgrid. - This is a movement, so we have to enlist it in AndroidManifest.xml . [AndroidManifest.xml] [Strings.xml]

Slide 10

2. Adding Graphics to Sudoku Defining the PuzzleView Class [PuzzleView.java] - In the constructor we keep a reference to the Game class and set the alternative to permit client contribution to the view. - Inside PuzzleView, we have to imple-ment the onSizeChanged() technique. - This is called after the view is made and Android knows how huge everything is.

Slide 11

2. Adding Graphics to Sudoku - We utilize onSizeChanged() to figure the measure of every tile on the screen. - selRect is a rectangle we'll utilize later to monitor the choice cursor. [PuzzleView.java]

Slide 12

2. Adding Graphics to Sudoku Drawing The Board - Android calls a view's onDraw() strategy each time any part of the view should be upgraded. To streamline things, onDraw() imagines that you're reproducing the whole screen sans preparation. - truly, you might draw just a little segment of the view as characterized by the canvas' clasp rectangle. Android deals with doing the cut-out for you. [Colors.xml]

Slide 13

2. Adding Graphics to Sudoku - The principal parameter is the Canvas on which to draw. - In this code, we're simply drawing a foundation for the bewilder utilizing the puzzle_background shading. [PuzzleView.java]

Slide 14

2. Adding Graphics to Sudoku - Now how about we add the code to draw the matrix lines for the board. [PuzzleView.java]

Slide 15

2. Adding Graphics to Sudoku - The code utilizes three distinct hues for the matrix lines. - The request in which the lines are drawn is vital, since lines drawn later will be drawn over the highest point of prior lines.

Slide 16

2. Adding Graphics to Sudoku Drawing the Numbers - The accompanying code draws the bewilder numbers on top of the tiles. - The precarious part here is getting every number situated and estimated so it goes in the correct focal point of its tile. [PuzzleView.java]

Slide 17

2. Adding Graphics to Sudoku - We call the getTileString() strategy to discover what numbers to show. - To figure the span of numbers, we set the text style stature to ¾ the tallness of the tile, and we set the angle proportion to be the same as the tile's viewpoint proportion. - We can't utilize outright pixel or point sizes since we need the program to work at any determination. - For the y bearing, we need to alter the beginning position descending a little so that the midpoint of the tile will be the midpoint of the number in-stead of its gauge. - We utilize the illustrations library's FontMetrics class to tell how much vertical space the letter will take altogether, and afterward we separate that into equal parts to get the conformity.

Slide 18

3. Taking care of Input - One contrast in Android writing computer programs is that Android telephones come in numerous shapes and sizes and have an assortment of info strategies. - They may have a console, a D-cushion, a touch screen, a trackball, or some mix of these. - A great Android program, in this manner, should be prepared to bolster what-ever input equipment is accessible, much the same as it should be prepared to bolster any screen determination. Characterizing and Updating the determination - First will actualize a little cursor that demonstrates the player which tile is right now chosen. - The chose tile is the one that will be altered when the player enters a number. - This code will attract the determination onDraw().

Slide 19

3. Taking care of Input [PuzzleView.java] - We utilize the determination rectangle figured before in onSizeChanged() to draw an alpha-mixed shading on top of the chose tile.

Slide 20

3. Taking care of Input - Next we give an approach to move the determination by abrogating the onKey-Down() strategy. [PuzzleView.java] - If the client has a directional cushion (D-cushion) and the press the up, down, left, or right catch, we call select() to move the sele-ction cursor in that course.

Slide 21

3. Taking care of Input - Inside the select() strategy, we figure the new x and y directions of the choice and afterward utilize getRect() again to compute the new determination rectangle. [PuzzleView.java] private void getRect( int x, int y, rect) { rect.set((int) (x * width), (int) (y * tallness), (int) (x * width + width), (int) (y * stature + stature)); }

Slide 22

3. Taking care of Input - Notice the two calls to nullify( ). The first tells Android that the range secured by the old choice rectangle (on the left of the figure) should be redrawn. - The second nullify( ) call says that the new choice zone (on the privilege of the figure) should be redrawn as well. We don't really draw anything here.

Slide 23

3. Taking care of Input - This is a critical point: never call any attracting capacities aside from the onDraw( ) technique. - Instead, you utilize the refute( ) strategy to stamp rectangles as filthy. The window chief will join all the messy rectangles sooner or later and call onDraw( ) again for you. - The messy rectangles turn into the clasp district, so screen redesigns are upgraded to just those territories that change.

Slide 24

3. Taking care of Input Entering Numbers - To handle console input, we simply add a couple of more cases to the onKey-Down( ) technique for the numbers 0 through 9 (0 or space implies eradicate the number). - To bolster the D-cushion, we check for the Enter or focus D-cushion catch in onKeyDown( ) and have it appear a keypad that gives the client select which a chance to number to put. [PuzzleView.java]

Slide 25

3. Taking care of Input - For touch, we supersede the onTouchEvent( ) technique and demonstrate a similar keypad, which will be characterized later. [PuzzleView.java]

Slide 26

3. Taking care of Input - Ultimately, all streets will lead back to a call to setSelectedTile( ) to change the number on a tile. - Note the call to negate( ) without any parameters. That denote the entire screen as filthy, which abuses my own recommendation prior! - However, for this situation, it's fundamental in light of the fact that any new numbers included or expelled may change the indications that we are going to actualize in the following area. [PuzzleView.java]

Slide 27

3. Taking care of Input Adding Hints - Add this to onDraw( ) before drawing the choice. - We utilize three states for zero, one, and two conceivable moves. On the off chance that there are zero moves, that implies the player has accomplished something incorrectly and necessities to backtrack. [PuzzleView.java]

Slide 28

3. Taking care of Input Shaking Things Up - What if the client tries to enter a clearly invalid number, for example, a number that as of now shows up in the 3x3 square? - Just for the sake of entertainment, we should make the screen squirm forward and backward when they do that. - First we add a call to the invalid number case in setSelectedTile( ). [PuzzleView.java]

Slide 29

3. Taking care of Input - This heaps and runs an asset called R.anim.shake, defi

SPONSORS