• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Android Training Lecture 12
 

Android Training Lecture 12

on

  • 549 views

 

Statistics

Views

Total Views
549
Views on SlideShare
549
Embed Views
0

Actions

Likes
1
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Android Training Lecture 12 Android Training Lecture 12 Presentation Transcript

    • Android Programming Lesson 122D Graphics NGUYEN The Linh
    • Android ProgrammingContents 1 CustomView 2 SurfaceView 3 Exercise 12 2
    • Android Programming2D Graphics 2D Graphics 3
    • Android ProgrammingCustomView Creating a custom view 4
    • Android ProgrammingCustomView Creating a custom view 5
    • Android ProgrammingCustomView Override onDraw()  The most important step in drawing a custom view is to override the onDraw() method.  The parameter to onDraw() is a Canvas object that the view can use to draw itself.  The Canvas class defines methods for drawing text, lines, bitmaps, and many other graphics primitives. You can use these methods in onDraw() to create your custom user interface (UI). 6
    • Android ProgrammingCustomView Create Drawing Objects  The android.graphics framework divides drawing into two areas: • What to draw, handled by Canvas • How to draw, handled by Paint.  For instance, Canvas provides a method to draw a line, while Paint provides methods to define that lines color.  Canvas has a method to draw a rectangle, while Paint defines whether to fill that rectangle with a color or leave it empty. 7
    • Android ProgrammingCustomView Create Drawing Objects  Simply put, Canvas defines shapes that you can draw on the screen, while Paint defines the color, style, font, and so forth of each shape you draw.  Creating objects ahead of time is an important optimization. Views are redrawn very frequently, and many drawing objects require expensive initialization. Creating drawing objects within your onDraw() method significantly reduces performance and can make your UI appear sluggish. 8
    • Android ProgrammingCustomView Handle Layout Events  In order to properly draw your custom view, you need to know what size it is.  You should never make assumptions about the size of your view on the screen.  App needs to handle different screen sizes, multiple screen densities, and various aspect ratios in both portrait and landscape mode. 9
    • Android ProgrammingCustomView Handle Layout Events  In order to properly draw your custom view, you need to know what size it is.  You should never make assumptions about the size of your view on the screen.  App needs to handle different screen sizes, multiple screen densities, and various aspect ratios in both portrait and landscape mode. 10
    • Android ProgrammingCustomView Handle Layout Events  Although View has many methods for handling measurement, most of them do not need to be overridden. If your view doesnt need special control over its size, you only need to override one method: onSizeChanged().  onSizeChanged() is called when your view is first assigned a size, and again if the size of your view changes for any reason. 11
    • Android ProgrammingCustomView Draw!  Once you have your object creation and measuring code defined, you can implement onDraw().  Every view implements onDraw() differently, but there are some common operations that most views share: 12
    • Android ProgrammingCustomView Draw!  Draw text using drawText(). Specify the typeface by calling setTypeface(), and the text color by calling setColor().  Draw primitive shapes using drawRect(), drawOval(), and drawArc(). Change whether the shapes are filled, outlined, or both by calling setStyle(). 13
    • Android Programming2D Graphics Draw!  Draw more complex shapes using the Path class. Define a shape by adding lines and curves to a Path object, then draw the shape using drawPath(). Just as with primitive shapes, paths can be outlined, filled, or both, depending on the setStyle().  Define gradient fills by creating LinearGradient objects. Call setShader() to use your LinearGradient on filled shapes.  Draw bitmaps using drawBitmap(). 14
    • Android ProgrammingCustomView Example 12.1 15
    • Android Programming2D Graphics SurfaceView 16
    • Android ProgrammingSurfaceView Difference between SurfaceView and View?  Views are all drawn on the same GUI thread which is also used for all user interaction.  So if you need to update GUI rapidly or if the rendering takes too much time and affects user experience then use SurfaceView.  The main difference is that SurfaceView can be drawn on by background threads but Views cant. 17
    • Android ProgrammingSurfaceView Example 12.2 18
    • Android ProgrammingExercise 12 Example 12.2  Kill Ants 19
    • Android Programming